Im trying to create a shiny app where a ggplot is the main plot output, and I have gotten it to the point where the users can filter by date using dateRangeInput, as well as select the X and Y axes with selectVarInput. However, in the main ggplot output I would like the plot to have a color aesthetic to separate the different responses based on date. Here is what I have so far, but an error message is returned saying "Aesthetics must be either length 1 or the same as the data (10): colour". Any help would be greatly appreciated!
library(tidyverse)
library(shiny)
sampledf <- tibble(Name = c("John Smith"), `Test 1` = c("Test 1"), `Date` = lubridate::as_date(c("2020-04-22","2020-04-22", "2020-04-22", "2020-04-24", "2020-04-24", "2020-04-24", "2020-04-24", "2020-04-26", "2020-04-26", "2020-04-26")), `Result 1` = rnorm(1:10), `Result 2` = rnorm(1:10), `Result 3` = rnorm(1:10))
# Define UI for application
ui <- navbarPage(
"Title",
tabPanel(
"Tab 1",
sidebarPanel(
h4("Inputs"),
selectInput(
"Name_Select",
label = "Select Name",
choices = sampledf$Name,
selected = TRUE
),
dateRangeInput(
"dates",
label = "Dates",
start = min(sampledf$Date),
end = max(sampledf$Date)
),
varSelectInput("X_Axis",
label = "Select Variable 1",
data = sampledf,
),
varSelectInput("Y_Axis",
label = "Select Variable 2",
data = sampledf,
),
)
),
mainPanel(plotOutput("plot")),
tabPanel("Tab2")
)
# Define server logic
server <- function(input, output) {
output$plot <- renderPlot({
Data %>%
filter(sampledf$Date >= input$dates[1], sampledf$Date <= input$dates[2]) %>%
ggplot(mapping = (aes_string(x = input$X_Axis, y = input$Y_Axis))) +
geom_line(color = input$date) +
geom_point(color = input$date)
})
}
# Run the application
shinyApp(ui = ui, server = server)