Web app not outputing the graphs

ggplot2
shiny

#1

Hi! I am currently writing my first web app in R and my code below runs without any errors but does not output the graphs, only the selectInput part of the code.

I am trying to achieve that the app outputs bar plot if user chooses to plot age group on the x axis. If the user chooses to plot year on the x axis then to plot a line graph. The app does not error out and actually runs but does not output any graphs. However it does plot the graphs when I run those lines separately.

Here is the code:

library(shiny)
library(ggplot2)

setwd("C:\\Users\\Lenovoi7\\Shrewsbury School\\IT\\Coursework")

who<-data.frame(read.csv("who.csv",  stringsAsFactors = TRUE))

ui<-fluidPage(
  
  sidebarLayout(
    
    
    sidebarPanel(
    
      
      selectInput(
        
        inputId = "X",
        label = "x-axis variable", 
        choices = c("Year" = "year", 
                    "Age group" = "age_group"),
        selected = "age_group",
        multiple = FALSE )
      
    ),
    
    mainPanel(
      
      plotOutput(outputId = "barplot"),
      
      plotOutput(outputId = "linegraph")
      
    )
  )
)

server <- function(input, output) {
  
  output$barplot <- renderPlot({
    validate(need(input$x=="age_group", message=FALSE))
    
    ggplot(data=who, aes(x=age)) + geom_bar(aes(weights=suicides_no), position="dodge")
  })
  
  output$linegraph <- renderPlot({
    validate(need(input$x=="year", message=FALSE))
    
    ggplot(data=who, aes(x=year)) + geom_line()
  })
  
}
shinyApp(ui = ui, server = server)

I would appreciate any help. Thank you!


#2

I think your inputID of the selectInput is upper-case X while you validate on lower-case x (input$x).


#3

That works perfect. Thanks!


closed #4

This topic was automatically closed 21 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.