Mastering Shiny chapter 2 exercise 5

need help with Mastering Shiny Chapter 2 exercise 5 found here https://mastering-shiny.org/basic-app.html... here is the problem.......
The following app is very similar to one you’ve seen earlier in the chapter: you select a dataset from a package (this time we’re using the ggplot2 package) and the app prints out a summary and plot of the data. It also follows good practice and makes use of reactive expressions to avoid redundancy of code. However there are three bugs in the code provided below. Can you find and fix them?

library(ggplot2)
datasets <- data(package = "ggplot2")$results[, "Item"]

ui <- fluidPage(
  selectInput("dataset", "Dataset", choices = datasets),
  verbatimTextOutput("summary"),
  tableOutput("plot")
)

server <- function(input, output, session) {
  dataset <- reactive({
    get(input$dataset, "package:ggplot2")
  })
  output$summmry <- renderPrint({
    summary(dataset())
  })
  output$plot <- renderPlot({
    plot(dataset)
  })
}

I see that the tableOutput needs to be plotOutput, and that summmry needs to be summary, but I can't find the 3rd error that is in the code. I have been messing with this for about a week, any help will be appreciated. I have my potential solution on my github https://github.com/Danieldavid521/Mastering_Shiny_solutions/blob/master/Mastering_Shiny_Chapter_2-5_solutions.R

I think it is in the plot(dataset) statement. Compare that to summary(dataset()).

I have tried that, but when I render the app, the only thing I get is the selectInput, but no summary or plot... in my example, I get an "cannot coerce type 'closure' to vector of type 'double'" error, and I looked into that, and I think a closure is reactive functionality. This is the 1st chapter in the book, so I don't believe the solution should be that intensive, I can do this exact thing with another dataset, but not the ggplot2 datasets, hence https://github.com/Danieldavid521/Mastering_Shiny_solutions/blob/master/Mastering_Shiny_Chapter_2-5_solutions2.R

1 Like

This works for some of the included data sets. Plotting the entire data set bogs down my system, so I chose to plot only the 3rd and 4th columns. Obviously, that cannot be a good choice in every case but it does show the basic functionality of the app.

library(shiny)

library(ggplot2)
datasets <- data(package = "ggplot2")$results[, "Item"]
ui <- fluidPage(
  selectInput("dataset", "Dataset", choices = datasets, selected= "mpg"),
  verbatimTextOutput("summary"),
  plotOutput("plot")
)

server <- function(input, output, session) {
   dataset <- reactive({
      get(input$dataset, "package:ggplot2")
   })
   output$summary <- renderPrint({
     summary(dataset())
   })
   output$plot <- renderPlot({
     plot(dataset()[[3]], dataset()[[4]])
   })
}

shinyApp(ui = ui, server = server)

Thank you, this will have to do. I guess I never considered the fact the dataset was not practical for the said example. Thank you

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