I've been playing with shiny modules and finally got some bits and pieces working. However, I've been totally thrown by an issue testing individual modules.
What I usually do is turn each module into a small app to test how it works. This particular module takes additional variables, but I don't seem to be able to insert some test vars into the test app as I would usually. Unfortunately, this fails.
Is there a standard way of dealing with this?
Many thanks
histogram_ui <- function(id) {
tagList(
plotOutput(NS(id, "hist"))
)
}
histogram_server <- function(id, var, bin) {
moduleServer(id, function(input, output, session) {
data <- reactive(mtcars[[var()]])
#debug
observeEvent(var(), {
print(var())
})
output$hist <- renderPlot({
hist(data(), breaks = bin(), main = var())
})
})
}
#testing----
ui_t <- fluidPage(
histogram_ui("test")
)
server_t <- function(input, output, session) {
histogram_server("test", var = "mpg", bin = 10)
}
options(shiny.reactlog=TRUE) #ctrl F3 to bring up
shinyApp(ui_t, server_t)
CodePudding user response:
Try this
histogram_ui <- function(id) {
tagList(
plotOutput(NS(id, "hist"))
)
}
histogram_server <- function(id, var, bin) {
moduleServer(id, function(input, output, session) {
observeEvent(c(var(), bin()), {
print(var())
})
output$hist <- renderPlot({
hist(mtcars[[var()]], breaks = bin(), main = var())
})
})
}
#testing----
ui_t <- fluidPage(
selectInput("myvar","Choose",choices = colnames(mtcars)),
sliderInput("bins","Number of Bins", min=1, max=10, value=5),
histogram_ui("test")
)
server_t <- function(input, output, session) {
histogram_server("test", var = reactive(input$myvar), bin = reactive(input$bins))
}
options(shiny.reactlog=TRUE) #ctrl F3 to bring up
shinyApp(ui_t, server_t)