How can I visualize the diffrent time intervals by choosing the diffrent modes of transport in my Shiny bar plot?

Viewed 56

How can I make my Barchart interactive? I want to see the arrival time by choosing the different modes of transport. I am able to run the app, however, it is impossible to make it interactive.

The variable Arrival are time intervals. The variable Mode are modes of transport. The variable Answer means Yes/No.

This is a part of my dataframe:

> head(Data_Long)
    Id               Arrival    Mode Answer
1   54 6:00 p.m. - 6:30 p.m. On foot    Yes
2  132 5:30 p.m. - 6:00 p.m. On foot    Yes
3  600 5:00 p.m. - 5:30 p.m. On foot    Yes
4  620 4:00 p.m. - 5:00 p.m. On foot    Yes
5  951 5:30 p.m. - 6:00 p.m. On foot    Yes
6 1011 4:00 p.m. - 5:00 p.m. On foot    Yes

This is my code:

library(shiny)
library(ggplot2)
library(dplyr)
library(tidyverse)
library(readxl)

#Read in Excel
Survey <- read_excel("04_Participant.xlsx")

Data <- subset(Survey, select = c(1, 4:14))

colnames(Data) <- c(
  'Id',
  'Arrival',
  'On foot',
  'Bicycle',
  'Bicycle (Yélo)',
  'Motorcycle/scooter',
  'Scooter (trotinette)',
  'Bus',
  'Train',
  'Car',
  'Carpool',
  'Car (Yélo)'
)


Data_Long <- Data %>% 
  gather(key="Mode",
         value = "Answer",
         c(-Id, -`Arrival`))

Data_Long <- subset(Data_Long, Answer=="Yes")

Data_Long <- as.data.frame(Data_Long)

Data_Long %>% 
  ggplot(aes(x=Arrival))+
  geom_histogram(stat="count")


# ui.R ----
ui <- fluidPage(
  titlePanel("Arrival times"),  # Add a title panel
  sidebarLayout(  
    position = "right",
                  
    sidebarPanel(h3("Inputs for histogram"),
                 selectInput("Mode", "Select mode", choices = c("On foot"="On foot",
                                                                "Bicycle" = "Bicycle",
                                                                "Bicycle (Yélo)"="Bicycle (Yélo)",
                                                                "Motorcycle/scooter"="Motorcycle/scooter",
                                                                "Scooter (trotinette)"="Scooter (trotinette)",
                                                                "Bus"="Bus",
                                                                "Train" = "Train",
                                                                "Car" = "Car",
                                                                "Carpool"="Carpool",
                                                                "Car (Yélo)"= "Car (Yélo)"),
                                                                  selected = "Car"),
                 br(),
                 
                 
                 ),
    
    # Inside the sidebarLayout, add a sidebarPanel
    mainPanel(
      plotOutput("myhist")
      #tableOutput("mytable"),
      #textOutput("mytext")
      
    )  
  )
)

# server.R ----
server <- function(input, output) {
  
  output$myhist <- renderPlot(
  
    Data_Long %>% 
      ggplot(aes(x=Arrival))
    + geom_histogram(stat="count",group=input$Mode, data = Data_Long[Data_Long$Mode==input$Mode])
    

  )
  
}

# Run the app ----
shinyApp(ui = ui, server = server)

I get the following error message:

Warning: Error in [.data.frame: undefined columns selected
  [No stack trace available]

This is a picture of the shiny app when I run it: Shiny app

1 Answers

Please try this:

output$myhist <- renderPlot(
    Data_Long %>% 
      ggplot(aes(x=Arrival)) +
    geom_histogram(stat="count",group=input$Mode, data = Data_Long[Data_Long$Mode==input$Mode, ])
    
    
  )
  1. In ggplot chain + sign has to be used on the end of each line (element of chain), not on the beginning.
  2. I just learned that when you subset data.frame object using [ and do not include comma and there is no observation you are looking for in the data frame, then you will get 0 columns and 2 rows:
> Data_Long[Data_Long$Mode == "Car"]
data frame with 0 columns and 2 rows

But when you use comma, then you will get 0 rows, but all columns:

> Data_Long[Data_Long$Mode == "Car", ]
[1] id      Arrival Mode    Answer 
<0 rows> (or 0-length row.names)

And this is a reason why you are getting Error: undefined columns selected - there is no such column as Arrival if you subset without comma and searching for an observation which do not exists ('Car' does not exist in your data.frame used as an example - you can choose in your app 'On foot' and you should see a plot instead of error). Or you can use comma as I did above.

Related