jeudi 24 mars 2022

R UDF outputs structure for Shiny plotly outputs and DT outputs

I have a UDF in R, which I use to output various, sometimes heavy, plotly graphs. What I have been doing so far is to return them as a list

global.R

myGraphingFunction <- function(inputs){

A <- ggplotly(ggplot(...))
B <- ggplotly(ggplot(...))
C <- ggplotly(ggplot(...))
return(list(Plot1 = A, Plot2 = B, Plot3 = C))
} 

I then go on the index out what plot I need based on where I am in my shiny interface.

This works fine for me, but with heavy dataload, this takes my shiny app many minutes to evaluate. I am wondering if it would be far better if instead of evaluating all the graphs with the UDF, I parameterize which graph output I would need with some extra arguments, and then in the server function call the function multiple times to get all the graphs separately, something like:

global.R

myGraphingFunction <- function(inputs, graphSelectionInputs){

if(graphSelectionInputs == "a"){
plot <- ggplotly(ggplot(...))
} else if(graphSelectionInputs == "b"){
plot <- ggplotly(ggplot(...))
} else {
plot <- ggplotly(ggplot(...))
}

return(plot)
} 

server.R

output$plot1 <- renderPlotly({myGraphingFunction(input, "a")})
output$plot2 <- renderPlotly({myGraphingFunction(input, "b")})
output$plot3 <- renderPlotly({myGraphingFunction(input, "c")})

Bear in mind that I shall always need to invoke each of the graph outputs of the function. That being the case, is there an added value in the aforementioned parametrization? Also, would the differences between the two approaches behave the same for DT outputs? I could not find any literature on the matter myself, I think because this is a very specific use case. I should appreciate any input from users experienced with these sorts of design practices.

Aucun commentaire:

Enregistrer un commentaire