ESt*_*ark 3 variables r global-variables shiny
我想将用户选择保存为全局环境中的字符向量,以便在进一步分析中用作输入(my_choices下拉菜单) 如何保存选择的内容?
例子:
library("shiny")
library("shinyWidgets")
my_choices <- c(
"2018Q1","2018Q2","2018Q3", "2018Q4", "2019Q1", "2019Q2")
ui <- fluidPage(
pickerInput(
inputId = "id",
label = "SELECT PERIOD:",
choices = my_choices,
selected = NULL,
multiple = TRUE,
options = list(
`actions-box` = TRUE, size = 15, `selected-text-format` = "count > 3"
),
choicesOpt = list(
content = stringr::str_trunc(my_choices, width = 75)
)
),
verbatimTextOutput(outputId = "res")
)
server <- function(input, output, session) {
output$res <- renderPrint(input$id)
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
尝试使用双向箭头进行全局赋值:
observe({
your_global_variable <<- input$id
})
Run Code Online (Sandbox Code Playgroud)
或者使用assign()函数:
observe({
assign(
x = "your_global_variable",
value = input$id,
envir = .GlobalEnv
)
})
Run Code Online (Sandbox Code Playgroud)