I'm building a Shiny module with renderUI
where I refer to IDs produced inside the module server function:
library(shiny)
library(DT)
library(dplyr)
module_ui <- function(id){
ns <- shiny::NS(id)
shiny::tagList(
fluidPage(uiOutput('test_ui'))
)
}
module <- function(input, output, session) {
ns <- session$ns
output$test_ui <- renderUI({
shiny::fluidPage(
shiny::selectizeInput(
inputId = ns('plot_vars'),
label = 'Choose variables to plot',
choices = colnames(mtcars),
selected = colnames(select(mtcars, mpg, wt)),
multiple = TRUE
),
verbatimTextOutput(ns('text')),
DT::dataTableOutput(ns('d_plot'))
)
})
output$text <- renderText({
input$plot_vars
})
output$d_plot <- DT::renderDataTable({
input_data <- mtcars[, input$plot_vars]
DT::datatable(input_data)
})
}
ui <- module_ui('XXX')
server <- callModule(module, 'XXX')
shinyApp(ui, server)
Despite applying ns()
religiously throughout, I'm getting the following error when running server module function:
Error in session$makeScope(id) : attempt to apply non-function
Can't see what's causing the problem here, any hints would be very much appreciated!
First, you have to use ns
in module_ui
:
module_ui <- function(id){
ns <- NS(id)
tagList(
fluidPage(uiOutput(ns('test_ui')))
)
}
You have to define ui
and server
like this:
ui <- fluidPage(
module_ui('XXX')
)
server <- function(input, output, session){
callModule(module, 'XXX')
}
Something else: set drop = FALSE
when you select some columns of a dataframe:
input_data <- mtcars[, input$plot_vars, drop = FALSE]
otherwise you get a vector if you select only one column, with drop=FALSE
you get a dataframe with a single column.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.