I find myself going in circles. I can dynamiclaly generate a set of radiobuttons based on a variable set of questions. But I cannot figure out how to read the selected values and return them from the module. Actually returning them would likely be easy if I could just figure out how to make a list of the selections.
Example files can be found on my GitHub account: Working example
I have the same issue. I don't understand how to access the output of the shiny module. The documentation says to return a reactive expression. I thought I was. In this example I tried to follow an example from the documentation.
Ultimately, I want to make a module that takes in a data frame that determines the choices in a set of filters. It takes in a second df parameter that allows the user to select the data from the input and return filtered rows I will plot.
I added an observeEvent around the module so it would run when the selected data set is ready. In my app it will be from a database query. I had to use the observeEvent because the module only runs once and is not responding to a changing input data parameter.
#***********************************************
# Example from
# https://shiny.rstudio.com/articles/modules.html
#******************************************************
columnChooserUI <- function(id) {
ns <- NS(id)
uiOutput(ns("controls"))
}
columnChooser <- function(input, output, session, data) {
output$controls <- renderUI({
ns <- session$ns
selectInput(ns("col"), "Columns", names(data), multiple = TRUE)
})
return(reactive({
validate(need(input$col, FALSE))
data[,input$col]
}))
}
#end example
#---------------------------------------------------------------------------------
#*********************************************************************
# me trying to use the example in an app:
ui <- fluidPage(
fileInput('datafile',label=NULL),
textOutput('dataset_rows'),
hr(),
columnChooserUI("chooser"),
uiOutput('column_selected')
)
server <- function(input, output, session) {
dataset<-reactive({
if(is.null(input$datafile))(return(NULL))
return(read.csv(input$datafile$datapath,colClasses = 'character',header=T))
})
output$dataset_rows<-renderPrint({
if(is.null(dataset()))(return(NULL))
nrow(dataset())
})
#Call Module, return a reactive on selected column in upload file
observeEvent(dataset(),{
data_column <- callModule(columnChooser, "chooser",data=dataset())
})
output$column_selected<-renderUI({
#I thought this should render the text based on the choice made from the module, but doesn't seem to work ??
#browser()
return(h2(colnames(data_column())))
})
}
shinyApp(ui, server)