r - 在延迟表达式评估中,R Shiny 使用变量的变化值

标签 r shiny

这是问题的简单演示:

library(shiny)

ui <- fluidPage(
    textOutput("Text1"),
    textOutput("Text2")
)

server <- function(input, output) {
    for(i in 1:2) {
        id <- paste0("Text", i)
        output[[id]] <- renderText(paste0("This is text #", i)) # Problem!
    }
}

shinyApp(ui, server)

该程序产生输出

这是文字#2
这是文字#2

而不是#1和#2。

显然,Shiny 存储了传递给 renderText() 的表达式。在标记为 的行中# 问题! ,并在 for 循环完成后评估它们。表达式取决于变量 i , 及其最终值 i = 2用于评估这两个表达式。

如何在仍然使用循环的同时产生正确的输出(如何强制 Shiny 在不同的表达式中使用 i 的不同值)?在我的代码中,循环限制是动态的,我不能用几个静态调用替换循环。

最佳答案

为什么 for -loop 不起作用,请检查此示例的输出:

library(shiny)

ui <- fluidPage(
  textOutput("Text1"),
  textOutput("Text2")
)

server <- function(input, output) {
  for(i in 1:3) {
    id <- paste0("Text", i)
    output[[id]] <- renderText(paste0("This is text #", i)) # Problem!
  }
  i=10 # we set i to 10.

}

shinyApp(ui, server)

如您所见,所有 renderText元素使用 i 的最后一个(全局)值。 lapply 中的情况并非如此。 ,其中将参数传递给函数,但该参数未在全局环境中定义。

所以你可以使用 lapply而不是 for -循环,像这样:
library(shiny)

ui <- fluidPage(
  textOutput("Text1"),
  textOutput("Text2")
)

server <- function(input, output) {

  lapply(1:2,function(i){
    id <- paste0("Text", i)
    output[[id]] <- renderText(paste0("This is text #", i)) # Problem!
  })

}

shinyApp(ui, server)

输出:

enter image description here

如果您还希望 ui 具有响应性,可以使用 renderUIuiOutput ,例如如下:
library(shiny)

ui <- fluidPage(
  numericInput("number_of_text","Number of text",min=1,max=10,value=3),
  uiOutput('my_text')
)

server <- function(input, output) {


  reactive_text <- reactive({
    all_text <- lapply(1:input$number_of_text,function(i){
      id <- paste0("Text", i)
      renderText(paste0("This is text #", i)) # Problem!
    })
   # do.call(all_text,tagList)
  })

  output$my_text <- renderUI({
    do.call(fluidRow, reactive_text())
  })

}

shinyApp(ui, server)

输出:

enter image description here

希望这可以帮助!

关于r - 在延迟表达式评估中,R Shiny 使用变量的变化值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48135448/

相关文章:

r - 如何在数据集中创建没有纬度和经度的 R Shiny 交互式 map

r - 使用鼠标点击 Shiny 的交互式绘图

r - Shiny 的 slider 限制释放鼠标左键的 react

r - 在 R 中的 geosphere 包中,为什么方位角不是 0-360 度?

r - 在browser()中完成和继续之间有什么区别?

r - 在 Y 轴上绘制 2 个变量,在 R 中使用 ggvis

html - R shiny - 使用 titlePanel 在浏览器窗口中添加 Logo

r - EC2 上 Shiny 的应用程序 - 错误 : rgl. open() 失败

r - np 包结果的偏回归图

R ggplot - 错误 stat_bin 需要连续的 x 变量