I am struggling with a workflow to pass a reactive dataset and function to a module in shiny. I have made a simple version of what I mean below; the app simply prints the mean mpg for a each value of cyl.
library(shiny)
# Module
textToolUI <- function(id){
ns <- NS(id)
textOutput(ns("text"))
}
textTool <- function(input, output, session, value){
output$text <- renderText({paste(value)})
}
# App
ui <- basicPage(
selectInput("carbSelect", "Carburetor Selector", choices = c(1,2,3,4)),
textToolUI("text1")
)
server <- function(input, output, session){
data <- reactive(filter(mtcars, carb == input$carbSelect))
myfunc <- function(x){return(mean(x))}
callModule(textTool, "text1", value = myfunc(data$mpg)) # This throws up the "object of type closure not subsettable" error
# Using data()$mpg means it is not reactive
}
shinyApp(ui = ui, server = server)
The problem arises due to the fact that both the dataset and function (myfunc) need to sit outside the module. In my actual app there are multiple different datasets and functions used.
I assume the problem here is that the function is evaluated before the reactive dataset and hence I need a different work flow but I can't think of a suitable alternative.