Home > Net >  How to retrieve loop-generated variables across multiple outputs in shiny
How to retrieve loop-generated variables across multiple outputs in shiny

Time:04-26

Using iris data, I am running a loop within an r shiny reactive expression to generate the below variable species_table content that I want to access in a subsequent output function. I have followed guidance in this link but when invoking the species_function function later on, I get an error message saying species_table cannot be found. How can I retrieve the species_table content in the output function to display in the ui?

library(shiny)
data(iris)

ui <- fluidPage(
  
  mainPanel(
    fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
    fluidRow(tableOutput("result")))

)

server <- function(input, output) {
  
  species_list <- reactiveValues(a = "setosa", b = "versicolor", c = "virginica")  
  
  species_function <- reactive({  
    species_table <- list()
    for (i in reactiveValuesToList(species_list)){
      local({
        j <- i
        species_table[[reactive({j})()]] <<- iris[iris$Species==reactive({j})(),]  
      })
    }
    
    print(species_table[[input$portfolio]])
    
    return(list(species_table[["setosa"]], species_table[["versicolor"]], species_table[["viginica"]]))
    
  })
  
  output$result <- renderTable({
    
    species_table2 <- list()
    
    for (p in reactiveValuesToList(species_list)){
      local({
        q <- p
        species_table2[[reactive({q})()]] <<- species_function()[[species_table]][[reactive({q})()]][1:5, c("Sepal.Length", "Sepal.Width")]       
      })
    }
    
    print(species_table2[[input$portfolio]])
    return(species_table2[[input$portfolio]])
    
  })
  
}

shinyApp(ui = ui, server = server)

CodePudding user response:

Your species_table is not available outside the reactive where it is defined. Instead, return a named list, and you get your desired output. Try this

library(shiny)
data(iris)

ui <- fluidPage(
  
  mainPanel(
    fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
    fluidRow(tableOutput("result"))
    )
    
)

server <- function(input, output) {
  
  species_list <- reactiveValues(a = "setosa", b = "versicolor", c = "virginica") 
  
  species_function <- reactive({
    
    species_table <- list()
    for (i in reactiveValuesToList(species_list)){
      local({
        j <- i
        species_table[[j]] <<- iris[iris$Species==j,]
      })
    }

    #print(rv$species_table[[input$portfolio]])

    return(list("setosa" = species_table[["setosa"]],"versicolor" = species_table[["versicolor"]],"virginica" = species_table[["virginica"]]))
    
  })
  
  output$result <- renderTable({
    input$portfolio
    species_table2 <- list()

    for (p in reactiveValuesToList(species_list)){
      local({
        q <- p
        species_table2[[reactive({q})()]] <<- species_function()[[reactive({q})()]][1:5, c("Sepal.Length", "Sepal.Width")]
      })
    }

    print(species_table2[[input$portfolio]])
    return(species_table2[[input$portfolio]])

  })
  
}

shinyApp(ui = ui, server = server)
  • Related