将文本输入存储为 R shiny 中的变量
Store text input as variable in R shiny
我想获取用户的输入并将其存储为将在绘图函数中使用的变量。我的代码:
ui <- fluidPage(
mainPanel(
plotlyOutput("plot", width = '100%'),
br(),
textAreaInput("list", "Input List", ""),
actionButton("submit", "Submit", icon = icon("refresh"), style="float:right")
))
server <- function(input, output, session) {
my_text <<- renderText({
req(input$submit)
return(isolate(input$list))
my_text ->> subv
})
bindEvent(my_text,
output$plot <- renderPlotly({
#my very long plot code goes here which takes subv as input. This part has been tested outside of shiny and I know works.
}
我试图将文本存储在 sub
v 变量中,因为它将指示 renderPlotly
将生成什么。当我点击提交时,什么也没有发生,变量只在会话结束后创建。在我的环境中新创建的 subv
变量不显示输入的文本,而是将 subv
列为空函数,即 subv function(...)
您可以在下面找到您想要实现的工作原型以及有关问题所在的一些信息
首先,我们需要一个 textOutput
来显示我们的文本。我知道这对于实际用例可能不是必需的,但对于此答案的演示目的很重要。
接下来,我们不需要通过<<-
或->>
将变量设置为global
。这通常不是好的做法。相反,我们应该将结果存储在 reactive
中。另请参阅 reactiveVals
(但当应用程序变得复杂时,这更难遵循)。
由于我们只需要在单击提交时获取值,因此我们应该在单击 submit
时使用绑定到 运行 的事件。这在本质上类似于 eventReactive
.
最后,我们可以使用 bindCache
将我们的结果缓存到输入列表中。
ui <- fluidPage(
mainPanel(
plotlyOutput("plot", width = '100%'),
br(),
textAreaInput("list", "Input List", ""),
actionButton("submit", "Submit", icon = icon("refresh"),
style="float:right"),
textOutput("hello_out")
))
server <- function(input, output, session) {
my_text <- reactive({
input$list
}) %>%
shiny::bindCache(input$list
) %>%
shiny::bindEvent(input$submit)
output$hello_out <- renderText({
my_text()
})
}
shinyApp(ui, server)
我想获取用户的输入并将其存储为将在绘图函数中使用的变量。我的代码:
ui <- fluidPage(
mainPanel(
plotlyOutput("plot", width = '100%'),
br(),
textAreaInput("list", "Input List", ""),
actionButton("submit", "Submit", icon = icon("refresh"), style="float:right")
))
server <- function(input, output, session) {
my_text <<- renderText({
req(input$submit)
return(isolate(input$list))
my_text ->> subv
})
bindEvent(my_text,
output$plot <- renderPlotly({
#my very long plot code goes here which takes subv as input. This part has been tested outside of shiny and I know works.
}
我试图将文本存储在 sub
v 变量中,因为它将指示 renderPlotly
将生成什么。当我点击提交时,什么也没有发生,变量只在会话结束后创建。在我的环境中新创建的 subv
变量不显示输入的文本,而是将 subv
列为空函数,即 subv function(...)
您可以在下面找到您想要实现的工作原型以及有关问题所在的一些信息
首先,我们需要一个 textOutput
来显示我们的文本。我知道这对于实际用例可能不是必需的,但对于此答案的演示目的很重要。
接下来,我们不需要通过<<-
或->>
将变量设置为global
。这通常不是好的做法。相反,我们应该将结果存储在 reactive
中。另请参阅 reactiveVals
(但当应用程序变得复杂时,这更难遵循)。
由于我们只需要在单击提交时获取值,因此我们应该在单击 submit
时使用绑定到 运行 的事件。这在本质上类似于 eventReactive
.
最后,我们可以使用 bindCache
将我们的结果缓存到输入列表中。
ui <- fluidPage(
mainPanel(
plotlyOutput("plot", width = '100%'),
br(),
textAreaInput("list", "Input List", ""),
actionButton("submit", "Submit", icon = icon("refresh"),
style="float:right"),
textOutput("hello_out")
))
server <- function(input, output, session) {
my_text <- reactive({
input$list
}) %>%
shiny::bindCache(input$list
) %>%
shiny::bindEvent(input$submit)
output$hello_out <- renderText({
my_text()
})
}
shinyApp(ui, server)