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]
