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.