I am new to making shiny apps. Essentially I am trying to make a shiny app that does the following:
- ui allows you to load a df from your local computer
- 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
- gives the output ggplot
Here is what is happening
- the df is read in fine
- the dropdown select menu input in the ui work fine
- 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:
- 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.
- 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.