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:
