Home > Blockchain >  R Shiny Reactive Plot from List of Lists
R Shiny Reactive Plot from List of Lists

Time:05-06

I'm building a basic R Shiny app, and have a list that contains individual lists, each storing a dataframe and a value. I've included sample data to demonstrate my list of lists. Within my app I am trying to have one select option (a dropdown menu that says "List 1", List 2", etc) and then have the main panel in the app display a boxplot of the dataframe (x and y) and a text output of the value stored in the list that was selected.

I'm having trouble with the ability to make the outputs (both plot and text) reactive to the input and display data from the selected list.

I've put my code of what I have so far below.

## Example Data
list_a <- list(df = data.frame(x = rnorm(n = 10, mean = 5, sd = 2),
                             y = rnorm(n = 10, mean = 7, sd = 3)),
             value = "a")
list_b <- list(df = data.frame(x = rnorm(n = 10, mean = 20, sd = 5),
                             y = rnorm(n = 10, mean = 13, sd = 7)),
             value = "b")
list_c <- list(df = data.frame(x = rnorm(n = 10, mean = 12, sd = 4),
                             y = rnorm(n = 10, mean = 10, sd = 4)),
             value = "c")

mylist <- list(list_a, list_b, list_c)

## Packages
library(tidyverse)
library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Shiny App"),

## Panel with selectInput dropdown and output options
    pageWithSidebar(
        headerPanel('Data'),
        sidebarPanel(
            selectInput('data', 'Dataset',
                        choices = c("1" = list_a, "2" = list_b, "3" = list_c)),
        ),
        mainPanel(
            plotOutput('plot1'),
            textOutput('text1')
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    ## Boxplot with 'DF' from selected list
        output$plot1 <- renderPlot({
        reactivedata <- boxplot(input$data)
        boxplot(reactivedata$df)
    })

    ## Text output from 'value' stored in list
    output$text1 <- renderText({
        reactivetext <- print(input$data)
        print(reactivetext$value)
    })


}

# Run the application
shinyApp(ui = ui, server = server)

CodePudding user response:

Main issue with your code that you used your raw lists for the choices argument. Additionally I added a reactive to pick the right list according to the user's input:

set.seed(123)

library(tidyverse)
library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Shiny App"),

  ## Panel with selectInput dropdown and output options
  pageWithSidebar(
    headerPanel("Data"),
    sidebarPanel(
      selectInput("data", "Dataset",
        choices = c("list_a" = 1, "list_b" = 2, "list_c" = 3)
      ),
    ),
    mainPanel(
      plotOutput("plot1"),
      textOutput("text1")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  reactivedata <- reactive({
    mylist[[as.integer(input$data)]]  
  })
  
  ## Boxplot with 'DF' from selected list
  output$plot1 <- renderPlot({
    boxplot(reactivedata()$df)
  })

  ## Text output from 'value' stored in list
  output$text1 <- renderText({
    print(reactivedata()$value)
  })
}

# Run the application
shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:4502
#> [1] "a"

  • Related