shiny - accessing the plot and data.frame returned by a function

...
I have a function that returns a list with a plot and a data.frame and I am trying to figure out to access both the plot and the data.frame as reactives without having to run the function twice. This basic app shows the functionality I want, but it requires that the function be run twice, once to get the plot and once to get the data.frame. I have tried

l <- reactiveValues(values = summarize_eruptions(input$bins))

and then trying to get the values with
output$plot <- renderPlot({l$values$p})

but this only returns a blank plot it seems.

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)


summarize_eruptions <- function(x) {
    plot <- qplot(faithful$eruptions, bins = x)
    dat <- head(faithful, 5)
    return(list(p = plot, d = dat))
}

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

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot"),
           tableOutput('dat')
           
        )
    )
)

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

    output$distPlot <- renderPlot({
        summarize_eruptions(x = input$bins)$p
    })
    
    output$dat <- renderTable({
        summarize_eruptions(x = input$bins)$d
    })
}

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

Is this what you are after?

library(shiny)
library(ggplot2)


summarize_eruptions <- function(x) {
  plot <- qplot(faithful$eruptions, bins = x)
  dat <- head(faithful, 5)
  return(list(p = plot, d = dat))
}

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot"),
      tableOutput('dat')
      
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  values <- reactive(summarize_eruptions(input$bins))
  
  output$distPlot <- renderPlot({
    values()$p
  })
  
  output$dat <- renderTable({
    values()$d
  })
}

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

That's it! Thank you!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.