R shiny modules with reactive input not working

Hi, I am learning how to use modules in R shiny, and converting the code I have for a large complex R shiny app to use modules to reduce the amount of code needed.

Below is the UI module I have written,


model_priors_plot_UI <- function(id) {
   ns <- NS(id)   
  
   plotOutput(outputId =  ns("model_priors_plot")) 
}

And below is the corresponding attempt at the server module,

model_priors_plot_server <- function(id, mod) {  # "mod" is the rstan file which is reactive. 
  moduleServer(
    id,
    function(input, output, session) {
     output$model_priors_plot <-  renderPlot({ # can just refer to the plot name ("model_priors_plot") without using "ns" here in the server function. 
                                                params <- rstan::extract({mod()})
                                                
                                                se <- params$Se
                                                sp <- params$Sp
                                                corr <- params$Omega[, 1, 2]
                                                sd_se <- params$sigma[, 1]
                                                sd_sp <- params$sigma[, 2]
                                                
                                                n_samps <- 4000
                                                
                                                data <- tibble(
                                                  Samples = c(se, sp, corr, sd_se, sd_sp), 
                                                  Parameter = c(rep("Sensitivity", n_samps), 
                                                                rep("Specificity", n_samps), 
                                                                rep("Between-study correlation", n_samps),
                                                                rep("Between-study SD for sensitivity", n_samps), 
                                                                rep("Between-study SD for specificity", n_samps)))
                                                
                                                g <-    ggplot(data = data, aes(x=Samples)) + 
                                                  geom_density(alpha = 0.50) + 
                                                  theme_bw() +
                                                  theme(legend.position = "none") +
                                                  facet_wrap( ~ Parameter, scales = "free") + 
                                                  xlab(" ") + 
                                                  ylab(" ")
                                               g
                                               return(g)
      })
    })
}

Note that the input mod is reactive, and is created in the R shiny app using eventReactive like follows:

  draws_priorsonly <- eventReactive(input$MA_run_prior_model, {
    # dataset
    X <- data()

    # draw samples
    rstan::sampling(
      object = model_priorsonly,
      data =  list(   MA_prior_mean_sens_mu = input$MA_prior_mean_sens_mu,
                      MA_prior_mean_sens_sd = input$MA_prior_mean_sens_sd,
                      MA_prior_mean_spec_mu = input$MA_prior_mean_spec_mu,
                      MA_prior_mean_spec_sd = input$MA_prior_mean_spec_sd,
                      MA_prior_SD_sens_sd = input$MA_prior_SD_sens_sd,
                      MA_prior_SD_spec_sd = input$MA_prior_SD_spec_sd),
      chains = 4,
      iter = 2000,
      warmup = 1000,
      control=list(adapt_delta=0.95,
                   max_treedepth = 10),
      seed= 123
    )
  })

I call the UI module function model_priors_plot_UI in the app like this:


 model_priors_plot_UI(id = "model_priors_plot_id")

and I call the server module function model_priors_plot_server in the app like this:

model_priors_plot_server(id = "model_priors_plot_id", 
                         mod = draws_priorsonly)

When I run the app get the error:

error in evaluating the argument 'object' in selecting a method for function 'extract': 

Any suggestions would be greatly appreciated.

Thanks.

before input$MA_run_prior_model takes a value,
draw_priorsonly wont contain an object that extract could run on, but its passed as a param to initialise your module.
Its a use case for req()

1 Like

Thanks Nir, so would I pot the req(mod) in the server module function ( model_priors_plot_server)?

params <- rstan::extract({
                        req(mod()) 
                        })
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.