'Access a shiny output slot, but not by using "$"

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?



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rick Tearle