Home > Software design >  what is the issue with my shiny code that is not spitting out the ggplot plot?
what is the issue with my shiny code that is not spitting out the ggplot plot?

Time:02-04

I am new to making shiny apps. Essentially I am trying to make a shiny app that does the following:

  1. ui allows you to load a df from your local computer
  2. the df is read and then the user can select two other inputs from drop down menu -column names from the dataframe to plot a ggplot
  3. gives the output ggplot

Here is what is happening

  1. the df is read in fine
  2. the dropdown select menu input in the ui work fine
  3. the ggplot is just not responsive

Here is my code:

library(shiny)
library(readxl)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  titlePanel("Upload Excel Sheet"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose Excel Sheet",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")),
     selectInput("x", "X-axis", choices = ""),
     selectInput("y", "Y-axis", choices = "")
     ),
    mainPanel(
      plotOutput("plot1")
    )
  )
)

server <- function(input, output,session) {
  data <- reactive({
    inFile <- input$file1
        if (is.null(inFile))
      return(NULL)
        read_excel(inFile$datapath)
  })
  observe({
    updateSelectInput(session, "x", choices = names(data()))
    updateSelectInput(session, "y", choices = names(data()))
  })
  
  output$plot1<- renderPlot({
    data()%>%ggplot(aes(x=input$x, y=input$y)) geom_bar(stat="identity"))
  })
}

shinyApp(ui = ui, server = server)

What I have tried:

  1. I have replaced the plot output (renderPlot) with a table output (renderTable) and see that the code "reads" the df and spits out the table.
  2. I have pulled the df in a separate piece of code (read_excel) into R and then run the ggplot command on it and it seems to be working fine.

I feel like I am missing something obvious here, and perhaps something critically basic. If possible please try the code on any excel sheet in your local dir.

CodePudding user response:

Several things need to be adjusted here.

  1. There is an extra close-paren with geom_bar(stat="identity")), removed.
  2. ggplot2 aesthetics need to be either non-standard evaluation symbols (not workable here), or we need to use a technique described here: shiny app with a failing 'validate' need

    where we would normally see

    shiny app with successful 'validate' need

    (where the plot is peeking out on the bottom).

  • Related