Rafael Sierra
Rafael Sierra

Reputation: 117

Need to use same input for multiple outputs in Shiny

I'm trying to code an app with tabs in Shiny that makes reference to the same input from a text box.

Input:

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

Output:

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"))
                          ),

Rendering:

output$sh1o <- renderText(input$sh1)

As I have learn, Shiny wont allow an input to be used more than once.

Is there any way to make this work?

Can the same input get assigned to a temp variable and then to the output?

Upvotes: 5

Views: 4529

Answers (1)

NicE
NicE

Reputation: 21425

Shiny allows input to be used as many times as you want, but you can't use the same outputId for output elements. You could rename your textOutput outputIds by adding the name of the tab first to make them unique.

Here's an example:

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)

Upvotes: 7

Related Questions