Closed GitHunter0 closed 2 years ago
@GitHunter0, you need to pass .data
as a data.frame
to choices
.
library(shiny)
library(purrr)
.data <- data.frame(var_name=c("A","B"), var_label = c("A_lab","B_lab"))
ui <- fluidPage(
selectizeInput(
inputId = "in_selectize",
label = "select variable",
choices = NULL,
multiple = TRUE
),
textOutput("out_selectize")
)
server <- function(input, output, session) {
shiny::updateSelectizeInput(
inputId='in_selectize',
label = "updated choices",
choices = .data,
options = list(
valueField = 'var_name',
labelField = 'var_name',
render = I("{
option: function(item, escape) {
return '<div>'
+ '<strong>' + escape(item.var_name) + '</strong>'
+ '<br>'
+ escape(item.var_label)
+ '</div>';
}
}")
),
server = TRUE
)
output$out_selectize <- shiny::renderText(input$in_selectize)
}
shiny::shinyApp(ui, server)
@etiennebr , thanks man! That detail solved the issue.
@GitHunter0, you need to pass
.data
as adata.frame
tochoices
.library(shiny) library(purrr) .data <- data.frame(var_name=c("A","B"), var_label = c("A_lab","B_lab")) ui <- fluidPage( selectizeInput( inputId = "in_selectize", label = "select variable", choices = NULL, multiple = TRUE ), textOutput("out_selectize") ) server <- function(input, output, session) { shiny::updateSelectizeInput( inputId='in_selectize', label = "updated choices", choices = .data, options = list( valueField = 'var_name', labelField = 'var_name', render = I("{ option: function(item, escape) { return '<div>' + '<strong>' + escape(item.var_name) + '</strong>' + '<br>' + escape(item.var_label) + '</div>'; } }") ), server = TRUE ) output$out_selectize <- shiny::renderText(input$in_selectize) } shiny::shinyApp(ui, server)
I know this is old, but I wanted to point out the Shiny docs make no reference to a dataframe being a required argument to choices when server=TRUE. It makes mention of passing a list. Maybe I'm being nitpicky but shouldn't the docs note this?
Bray.
I think the key insight from @etiennebr is that the initial UI has choices = NULL
, so @GitHunter0 had to include choices = .data
in the updateSelectizeInput()
call to populate the choices. I don't think there's a requirement that choices
be a dataframe when server = TRUE
.
Dropdown do not appear when server = TRUE if using render option.
MWE: