如何使用输入$ tabs在Shiny应用程序中引用输出名称

时间:2018-03-06 16:22:30

标签: r shiny

以下作品

library(shiny)   

ui <- (basicPage( 

 tabsetPanel(id = "tabs",
          tabPanel("Tab A", value = "A",
                   "This is Tab A content",
                   textOutput("tabA")),
          tabPanel("Tab B", value = "B",
                   "Here's some content for tab B.",
                   textOutput("tabB")))
))

server <- function(input, output, session) {

  output$tabA <- renderText({paste0("You are viewing tab ", input$tabs)})
  output$tabB <- renderText({paste0("You are viewing tab ", input$tabs)})
}

shinyApp(ui = ui, server = server)

我想根据选择的选项卡选择输出的名称。

output[["tabA"]] <- renderText({paste0("You are viewing tab ", input$tabs)})

以上情况很好但下面的内容确实无效,因为它需要反应性上下文。

 output[[paste0("tab", input$tabs)]] <- renderText({paste0("You are viewing tab ", input$tabs)})

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:1)

这样的东西?

library(shiny)   

ui <- (basicPage( 
  tabsetPanel(id = "tabs",
              tabPanel("Tab A", value = "A","This is Tab A content",textOutput("tabA")),
              tabPanel("Tab B", value = "B","Here's some content for tab B.",textOutput("tabB")))
))

server <- function(input, output, session) {

  observe({
    output[[paste0("tab", input$tabs)]] <- renderText({paste0("You are viewing tab ", input$tabs)})
  })
}

shinyApp(ui = ui, server = server)