-1

I want to access a shiny output slot eg output$data, but I want to do this using a variable, so that can assign outputs programatically. For example:

library(shiny)

ui <- fluidPage(
    sidebarPanel(
        selectInput(inputId = "species_in",
                    label = "Species:",
                    choices = c("none", "Human", "Mouse"),
                    width = "40%"),
    ), 
    
    mainPanel(
        textOutput("species_out_1"),
        textOutput("species_out_2"),
        textOutput("species_out_3"),
    )
)
    
server <- function(input, output, session){
    
    output$species_out_1 <- renderText({
        input$species_in
    })

    output$species_out_2 <- renderText({
        input[["species_in"]]
    })
    
    x <- "species_out_3"
    output[[x]] <- renderText({
        input$species_in
    })
    
}

shinyApp(ui,server)

Is this possible?

  • 2
    Yes. That should work. Is there any reason you don't think that works? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that can be used to test and verify possible solutions. – MrFlick Mar 28 '22 at 01:19

1 Answers1

0

Yes, this works, so there is an error elsewhere in my current app.

See code below that demonstrates equivalence of $ and [[]].


ui <- fluidPage(
    sidebarPanel(
        selectInput(inputId = "species_in",
                    label = "Species:",
                    choices = c("none", "Human", "Mouse"),
                    width = "40%"),
    ), 
    
    mainPanel(
        textOutput("species_out_1"),
        textOutput("species_out_2"),
        textOutput("species_out_3"),
    )
)
    
server <- function(input, output, session){
    
    output$species_out_1 <- renderText({
        input$species_in
    })

    output$species_out_2 <- renderText({
        input[["species_in"]]
    })
    
    x <- "species_out_3"
    output[[x]] <- renderText({
        input$species_in
    })
    
}

shinyApp(ui,server)