library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$h2("Virtual Select"),
fluidRow(
column(
width = 4,
virtualSelectInput(
inputId = "single",
label = "Single select :",
choices = month.name,
search = TRUE
),
virtualSelectInput(
inputId = "multiple",
label = "Multiple select:",
choices = setNames(month.abb, month.name),
multiple = TRUE
),
virtualSelectInput(
inputId = "onclose",
label = "Update value on close:",
choices = setNames(month.abb, month.name),
multiple = TRUE,
updateOn = "close"
)
),
column(
width = 4,
tags$b("Single select :"),
verbatimTextOutput("res_single"),
tags$b("Is virtual select open ?"),
verbatimTextOutput(outputId = "res_single_open"),
tags$br(),
tags$b("Multiple select :"),
verbatimTextOutput("res_multiple"),
tags$b("Is virtual select open ?"),
verbatimTextOutput(outputId = "res_multiple_open"),
tags$br(),
tags$b("Update on close :"),
verbatimTextOutput("res_onclose"),
tags$b("Is virtual select open ?"),
verbatimTextOutput(outputId = "res_onclose_open")
)
)
)
server <- function(input, output, session) {
output$res_single <- renderPrint(input$single)
output$res_single_open <- renderPrint(input$single_open)
output$res_multiple <- renderPrint(input$multiple)
output$res_multiple_open <- renderPrint(input$multiple_open)
output$res_onclose <- renderPrint(input$onclose)
output$res_onclose_open <- renderPrint(input$onclose_open)
}
if (interactive())
shinyApp(ui, server)
# labelRenderer example ----
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$head(
tags$script(HTML("
function colorText(data) {
let text = `${data.label}`;
return text;
}"
)),
),
tags$h1("Custom LabelRenderer"),
br(),
fluidRow(
column(
width = 6,
virtualSelectInput(
inputId = "search",
label = "Color picker",
choices = c("red", "blue", "green", "#cbf752"),
width = "100%",
keepAlwaysOpen = TRUE,
labelRenderer = "colorText",
allowNewOption = TRUE
)
)
)
)
server <- function(input, output, session) {}
if (interactive())
shinyApp(ui, server)
# onServerSearch example ----
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$head(
tags$script(HTML(r"(
// Main function that is called
function searchLabel(searchValue, virtualSelect) {
// Words to search for - split by a space
const searchWords = searchValue.split(/[\s]/);
// Update visibility
const found = virtualSelect.options.map(opt => {
opt.isVisible = searchWords.every(word => opt.label.includes(word));
return opt;
});
virtualSelect.setServerOptions(found);
}
)"
)),
),
tags$h1("Custom onServerSearch"),
br(),
fluidRow(
column(
width = 6,
virtualSelectInput(
inputId = "search",
label = "Better search",
choices = c("This is some random long text",
"This text is long and looks differently",
"Writing this text is a pure love",
"I love writing!"
),
width = "100%",
keepAlwaysOpen = TRUE,
search = TRUE,
autoSelectFirstOption = FALSE,
onServerSearch = "searchLabel"
)
)
)
)
server <- function(input, output, session) {}
if (interactive())
shinyApp(ui, server)
Run the code above in your browser using DataLab