r - Saving user input (Shiny app) to a global variable in R
问题描述
I want to save user selection as a character vector in my global environment for use in further analysis as input (my_choices
dropdown) How can I save what was selected?
Example:
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)
解决方案
尝试使用双头箭头进行全局赋值:
observe({
your_global_variable <<- input$id
})
或使用以下assign()
功能:
observe({
assign(
x = "your_global_variable",
value = input$id,
envir = .GlobalEnv
)
})
推荐阅读
- apache-spark - 为什么 RDD to JSON 会移除数据的实际排序?
- javascript - 如何解析对象并将其转换为数组
- javascript - 无法通过 javascript 切换 model.IList 复选框列表组的类
- python-3.x - 使用 Python3 的 AppEngine StdEnv:如何接收传入的电子邮件
- node.js - require(path.resolve(filePath)) 导入语句
- python - 如何在python中找到平均值
- javascript - 使用 CSS 或 JS 定位单词中的字母以更改颜色?
- javascript - 如何在同一个按钮上显示 0 和 1?
- javascript - 使用 javascript 和 html 应用多个视口
- java - 如何将我的响应正文时间戳格式更改为 unix 时间戳?