代码之家  ›  专栏  ›  技术社区  ›  Rafael Sierra

需要在Shiny中为多个输出使用相同的输入

  •  4
  • Rafael Sierra  · 技术社区  · 10 年前

    我正在尝试用Shiny中的标签来编写一个应用程序,它引用文本框中的相同输入。

    输入:

    column(2, textInput(inputId = "sh1", label = "Stakeholder #1's name"))

    输出:

    tabPanel("#1 vs #2",
                              fluidRow(
                                  column(3),
                                  column(2, textOutput(outputId = "sh1o")),
                                  column(2, "vs"),
                                  column(2, textOutput(outputId = "sh2o"))
                              ),
    
    tabPanel("#1 vs #3",
                              fluidRow(
                                  column(3),
                                  column(2, textOutput(outputId = "sh1o")),
                                  column(2, "vs"),
                                  column(2, textOutput(outputId = "sh3o"))
                              ),
    

    致使:

    output$sh1o <- renderText(input$sh1)

    据我所知,Shiny不会允许输入被多次使用。

    有什么办法使这项工作成功吗?

    是否可以将相同的输入分配给临时变量,然后再分配给输出?

    1 回复  |  直到 10 年前
        1
  •  6
  •   NicE    10 年前

    Shiny允许输入使用任意次数,但不能使用相同的输入 outputId 用于输出元件。您可以重命名 textOutput 输出Id 首先添加选项卡的名称,使其唯一。

    下面是一个示例:

    library(shiny)
    ui<-shinyUI(pageWithSidebar(
            headerPanel("Test"),
            sidebarPanel(textInput(inputId = "sh1", label = "Stakeholder #1's name")),
                    mainPanel(
            tabsetPanel(
                    tabPanel("#1 vs #2",
                             fluidRow(
                                     column(3),
                                     column(2, textOutput(outputId = "tab1_sh1o")),
                                     column(2, "vs"),
                                     column(2, textOutput(outputId = "tab1_sh2o"))
                             )),
    
                             tabPanel("#1 vs #3",
                                      fluidRow(
                                              column(3),
                                              column(2, textOutput(outputId = "tab2_sh1o")),
                                              column(2, "vs"),
                                              column(2, textOutput(outputId = "tab2_sh3o"))
                                      )
            )
    ))))
    
    server <- function(input,output,session){
            output$tab1_sh1o <- renderText(input$sh1)
            output$tab1_sh2o <- renderText(input$sh1)
            output$tab2_sh1o <- renderText(input$sh1)
            output$tab2_sh3o <- renderText(input$sh1)
    }
    shinyApp(ui,server)