getting an error when passing variable date from date selector into shiny app server code

Hello,
I am trying to make this shiny app to get the date from the selector (ui) and pass it to the r code (server) but it is not working and outputting an error.

It seems the problem is getting input$date into the filter correctly.For instance if I type filter(DATE>='2020-05-01') it works.

Bellow is is a snippet:


ui <- fluidRow(box(width = 4,dateInput("date",format = "dd/mm/yyyy",language = "pt-BR", label = h3("DATE"),value = Sys.Date()))
),
fluidRow(box(width = 12,height = 10,plotOutput(outputId = "Plot"))
))))

server <- function(input,output){
output$Plot <- renderPlot({
data %>% filter(DATE>=input$date) %>% filter(!wday(DATE) %in% c(1,7)) %>% mutate(sdavg=rollmeanr(SALES,k=5,fill=NA)) %>% ggplot(.,aes(x=DATE)) + geom_line(aes(y=SALES,color="#003366"))+ geom_line(aes(y=sdavg,color="#ff3300"))+ theme(axis.text.x = element_text(angle = 60),legend.position = "none")
})
}

I get the error: Aesthetics must be either length 1 or the same as the data (1): y, x when I use  `filter(PEDDTEMIS>=input$date)`

What is the correct way to pass `input$date` to the code ?

Thanks

Welcome to community.rstudio! :slight_smile:

Could you write a reprex for this? (e.g. see: Chapter 5 Workflow | Mastering Shiny)

I was trying to help you with this, but am having trouble reproducing your problem without data or rollmeanr:

My attempt at reproducing:

library(shiny)
library(shinydashboard)
library(dplyr)

ui <- fluidPage(
  fluidRow(
    box(
      width = 4,
      dateInput("date",
                format = "dd/mm/yyyy",
                label = h3("DATE"),
                value = Sys.Date()
      )
    )
  ),
  fluidRow(
    box(
      width = 12,
      height = 10,
      plotOutput(outputId = "Plot")
    ))
)

server <- function(input, output, session) {
  output$Plot <- renderPlot({
    data %>%
      filter(DATE >= input$date) %>%
      filter(!wday(DATE) %in% c(1, 7)) %>%
      mutate(sdavg = rollmeanr(SALES, k = 5, fill = NA)) %>%
      ggplot(., aes(x = DATE)) +
      geom_line(aes(y = SALES, color = "#003366")) +
      geom_line(aes(y = sdavg, color = "#ff3300")) +
      theme(axis.text.x = element_text(angle = 60), legend.position = "none")
  })
}

shinyApp(ui, server)

1 Like

Hi Julian,

After a bit of work I did find a way out the error. The pieces of code bellow did the fix:

filter(DATE >= as.Date(input$date))

The error was somewhere along the pipe on ggplot

ggplot(.,aes(x=PEDDTEMIS,y=value,col=variable)) + geom_line(stat="identity")

Now the charts are displaying right when I choose the dates.

Thank you for your reply and attention.

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.