在R中将用户输入(Shiny应用程序)保存到全局变量中

5
我希望能够将用户选择保存为字符向量,以便在后续分析中用作输入(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)
1个回答

8

尝试使用双头箭头进行全局赋值:

observe({
   your_global_variable <<- input$id
})

或者使用assign()函数:

observe({
   assign(
      x = "your_global_variable", 
      value = input$id, 
      envir = .GlobalEnv
   )
})

1
有没有办法使这个变量在页面刷新时保持不变? - ivanacorovic
你找到解决方案了吗? - coding

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接