Home > front end >  Changing the data frame of a ggplot graph from a dropdown menu in Shiny
Changing the data frame of a ggplot graph from a dropdown menu in Shiny

Time:01-30

I am trying to change the data frame that ggplot uses to render a graph given a selection of a drop down menu.

The dropdown menu works but the graph will not render.

for reference 'school' is a set of strings with the names of teams.

library(shiny)
library(shinydashboard)
library(ggplot2)

ui <- dashboardPage(
  dashboardHeader(title = "NCAA - Lorenz"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("teams", tabName = "teams"),
      menuItem("Correlation", tabName = "Correlation"))),
  dashboardBody(
    tabItems(
      tabItem("teams", 
              box(plotOutput("GINI_plot"), width = 4),
              box(
                selectInput("TEAM","TEAM:",
                            c(teams)), width = 4)),
      tabItem("Correlation", 
              box(plotOutput("correlation_plot"), width = 4),
              box(
                selectInput("STATISTIC","STATISTIC:",
                            c("DRtg","Pts","ast")), width = 4))
    )
  )
)

server <- function(input, output){
  
  df <- reactive({
    get(input)
  })
  
  output$GINI_plot <- renderPlot({
    
    ggplot(data = df, aes(CUMSUM,CUMperpts))   
      geom_abline(intercept = 0, slope = 1)
    
  })
  
  output$correlation_plot <- renderPlot({
    plot(schdata[[input$STATISTIC]], schdata$gini, ylab = "Gini Coef.", xlab = "STATISTIC")
  })

}

shinyApp(ui, server)

the box in the dash board reads "Error: [object Object]" This is the error output in rstudio

Warning: Error in ggplot: `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`
  175: <Anonymous>
  174: signalCondition
  173: signal_abort
  172: rlang::abort
  171: cli::cli_abort
  170: ggplot.function
  168: renderPlot [/home/thrash-libre/R-projects/application.R#35]
  166: func
  126: drawPlot
  112: <reactive:plotObj>
   96: drawReactive
   83: renderFunc
   82: output$GINI_plot
    1: runApp
Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

Anything helps .

The ggplot command should just read the dataframe variable and output a graph.

I have also tried to select from a list of dataframe but that didnt work either

CodePudding user response:

Error in ggplot: `data` cannot be a function.

gives the hint: your reactive df is a function which yet has to return the dataframe. Feed the results into you ggplot like so:

ggplot(data = df(), ...)

Note the trailing parentheses which have the function df executed and return the data.

CodePudding user response:

If you assign the variable within the "renderPlot" function, you can call it within the function as a data frame.

This method is preferable since I could call a the data frame when calculating the GINI index within the Annotate function.

server <- function(input, output){
  
  output$GINI_plot <- renderPlot({
    
    datful <- fdata %>% filter(School == input$TEAM)
    
    ggplot(data = datful, aes(CUMSUM,CUMperpts))   
      geom_abline(intercept = 0, slope = 1) 
      geom_smooth() 
      xlab("Cumulative Sum of Points") 
      ylab("Cumulative Sum") 
      theme_minimal() 
      labs(title = paste(input$TEAM,"Lorenz Curve")) 
      annotate("text", x=.25, y=.82,size = 5, label = paste("GINI coeff:",signif(Gini(datful$PTS),3)))
  })
}
  • Related