sliderInput in shiny — cannot set it correctly in

2019-08-12 02:25发布

问题:

This is the first time that I'm building an app and I would like to know what is the function or argument that I'm missing in my code. I want to display a normal bar graph, which filters dynamically the 'companies' that I would like to see displayed, the performance variable (e.g. 'profits') and a date range.

I believe my error is a result of imprecisions in shiny calls, because what I want is fairly simple. You could sort of obtain it by running (but with no possibility of interaction as provided by shiny) the following code:

library(ggplot2)

sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                        300,1000,630,
                                        410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))

    ggplot(data = sample_data, aes(x=Year, y = Profits_MM, 
    fill=as.factor(Company_Name))) + geom_col(position="dodge")

But I only mention it so one might have an idea of what I want as an output. The code that i've set with shiny is the following actually:

rm(list=ls()); gc()

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


sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                        300,1000,630,
                                        410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))


# UI
ui <- fluidPage(

  sidebarLayout(

    # Input(s)
    sidebarPanel(

      checkboxGroupInput(inputId = "sel_com",
                         label = "Company Selection:",
                         choices = c("Company 1","Company 2","Company 3"),
                         selected = "Company 1"),


      selectInput(inputId = "y", 
                  label = "Performance Variable",
                  choices = c("Profits (in Millions)" = "Profits_MM", 
                              "Sales (in Millions)" = "Sales_MM"),
                  selected = "Profits_MM"),


      sliderInput("year","Year Selection:",
                  min=2016,
                  max=2018,
                  value=c(2017,2018),
                  step=1)


    ),

    # Output(s)
    mainPanel(
      plotOutput(outputId = "barplot")
    )
  )
)

# Server
server <- function(input, output, session) {

  companies_sel <- reactive({

    req(input$sel_com)

    filter(sample_data, Company_Name %in% input$sel_com)

  })

  year_sample <- reactive({

    req(input$year)

    if(length(input$year)>1){

      Years = seq(input$year[1],input$year[2])

      filter(companies_sel(), Year %in% Years)

    }  

    if(length(input$year==1)){

      filter(companies_sel(), Year %in% input$year)

    }

  })


  output$barplot <- renderPlot({

    ggplot(data = year_sample(), aes_string(x=input$year, y = input$y, fill=as.factor(input$sel_com))) +

      geom_col(position="dodge")

  })

}

shinyApp(ui = ui, server = server)

I'm able to obtain some output, but only for one company at a time and without changing the range size. Maybe I do not understand the use of the observeEvent function, which might play a part in this. So how do I make the inputSlider -- and maybe other options -- interact adequately with a ggplot2 graph?

Here it is an example of the error in the ouptut:

回答1:

Try this code:

   library(ggplot2)

sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                      300,1000,630,
                                      410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))

ggplot(data = sample_data, aes(x=Year, y = Profits_MM, 
                               fill=as.factor(Company_Name))) + geom_col(position="dodge")






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


sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                      300,1000,630,
                                      410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))


# UI
ui <- fluidPage(

  sidebarLayout(

    # Input(s)
    sidebarPanel(

      checkboxGroupInput(inputId = "sel_com",
                         label = "Company Selection:",
                         choices = c("Company 1","Company 2","Company 3"),
                         selected = "Company 1"),


      selectInput(inputId = "y", 
                  label = "Performance Variable",
                  choices = c("Profits (in Millions)" = "Profits_MM", 
                              "Sales (in Millions)" = "Sales_MM"),
                  selected = "Profits_MM"),


      sliderInput("year","Year Selection:",
                  min=2016,
                  max=2018,
                  value=c(2017,2018),
                  step=1)


    ),

    # Output(s)
    mainPanel(
      plotOutput(outputId = "barplot")
    )
  )
)

# Server
server <- function(input, output, session) {

  companies_sel <- reactive({

    req(input$sel_com)

    sample_data_gg<-filter(sample_data, Company_Name %in% input$sel_com)
  #  print(sample_data_gg)
    sample_data_gg

  })

  year_sample <- reactive({

    req(input$year)
    sample_data_gg <- sample_data
    if(length(input$year)>1){

      Years = seq(input$year[1],input$year[2])

      sample_data_gg<-filter(companies_sel(), Year %in% Years)

    }  

    if(length(input$year==1)){

      sample_data_gg<-filter(companies_sel(), Year %in% input$year)

    }
  #  print(sample_data_gg)
    sample_data_gg
  })


  output$barplot <- renderPlot({
    sample_data_gg <- year_sample()
    y <- input$y

    ggplot(data = sample_data_gg, aes(x=Year, y =get( y ), fill=Company_Name)) +

      geom_col(position="dodge")

  })

}

shinyApp(ui = ui, server = server)

I prefer storing year_sample() first so I do not have to use input or aes_string in my ggplot. Also, I prefer get in other to reference a column by its string name.

Best!



标签: r ggplot2 shiny