简体   繁体   中英

R shiny reactive x axis ggplot

Say I have two different datasets with sales by age or region. Based on them, I can prepare bar charts as follows:

age <- data.frame("year" = c(2019, 2020), "age" = c("<30", "30-40", ">40"), "sales" = c(100, 150, 200, 250, 300, 350)) 

geo <- data.frame("year" = c(2019, 2020), "geo" = c("Europe", "Asia", "America"), "sales" = c(70, 120, 170, 220, 270, 320)) 



ggplot(age, aes(fill=as.factor(year), x=age, y=sales))+
  geom_bar(position="dodge", stat = "identity")

ggplot(geo, aes(fill=as.factor(year), x=geo, y=sales))+
  geom_bar(position="dodge", stat = "identity")

Now, I want to use them in the reactive Shiny dashboard, where user can choose the dataset to plot. I try the following code:


# Define UI 
ui <- fluidPage(
  
  # App title ----
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input:  ----
      selectInput("indicator", "Choose dataset",
                  c("age", "geo")),
      
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput("plot1")
      
    )
  )
)

# Define server logic 
server <- function(input, output) {
  
  
  datasetInput <- reactive({
    if (input$Indicator == "age"){
      ds1 <-age
    }
    else if (input$Indicator == "geo"){
      ds1 <-geo
    }
    return(ds1)
  })
  
  
  
  output$plot <- renderPlot({ 
      ggplot(datasetInput(), aes(fill=as.factor(year), x=input$indicator, y=sales))+
      geom_bar(position="dodge", stat = "identity")
    
  })
  
}

# Create Shiny app ----
shinyApp(ui, server)

But I get a blank dashboard, nothing happens. What is wrong with the code?

Two mistakes, both of which have been, already, pointed out by @stefan. This answer is just verbose implementation of what @stefan has already said. Nothing new.

  1. input$Indicator and input$indicator are two different things. R is case-sensitive. Keep it either indicator at all places or Indicator at all places. Do not mix. In this answer I kept indicator at all places.

  2. Output ID in the mainPanel(plotOutput()) is "plot1" chosen by you. HTML ID generated is plot1. However downstream in server you are referring output$plot..."plot" id does not exist so it can not attach the output to correct element in the HTML. Again keep plot1 at both places or plot at both places. In this answer I kept plot1 at both the places.

library(shiny)
library(ggplot2)
age <- data.frame("year" = c(2019, 2020), "age" = c("<30", "30-40", ">40"), "sales" = c(100, 150, 200, 250, 300, 350)) 
geo <- data.frame("year" = c(2019, 2020), "geo" = c("Europe", "Asia", "America"), "sales" = c(70, 120, 170, 220, 270, 320)) 
# Define UI 
ui <- fluidPage(
  
  # App title ----
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input:  ----
      selectInput("indicator", "Choose dataset",
                  choices = c("age", "geo"))),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput("plot1")
      
    )
  )
)


# Define server logic 
server <- function(input, output) {
  
  
  datasetInput <- reactive({
    if (input$indicator == "age"){
      ds1 <-age
    }
    else if (input$indicator == "geo"){
      ds1 <-geo
    }
    return(ds1)
  })
  
  
  
  output$plot1 <- renderPlot({ 
    ggplot(datasetInput(), aes(fill=as.factor(year), x=input$indicator, y=sales))+
      geom_bar(position="dodge", stat = "identity")
    
  })
  
}

# Create Shiny app ----
shinyApp(ui, server)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM