Problem with filter dates in shiny

I am trying to make a shiny app in which when choosing two dates it gives you the difference of another variable included in the same data frame, but the final result always gives me an error message in the app, which is: Error: comparison (3) is possible only for atomic and list types .

The app code is the following:

if (interactive()) {
  
  base <- readxl::read_excel("/Users/jorge/Downloads/salidacapitales.xlsx")
  base$Fecha <-as.Date(base$Fecha,"%m/%d/%Y") 
  shinyApp(
    
    ui = basicPage(
      dateInput("fecha1", "Ingresa una fecha inicial", value = "2020-12-01"),
      dateInput("fecha2", "Ingresa una fecha final", value = "2021-05-07"),
      tableOutput("output")
    ),
    server = function(input, output){
      dato1 <- reactive({base %>% 
                          dplyr::filter(Fecha == as.character(input$fecha1))})
      dato2 <- reactive({base %>% 
                          dplyr::filter(Fecha == as.character(input$fecha2))})
      x <- reactive({dato1$`Total en dólares` - dato2$`Total en dólares`})
      output$output <- renderPrint({if( x < 0 ){
        print(paste("Hubo una entrada de capitales por",abs(x),"millones de dólares"))  
      } else {
        print(paste("Hubo una salida de capitales por",abs(x),"millones de dólares"))   
      } })
    }
  )
}

Anyone have any idea what my mistake could be?

Thanks for help me

Can you provide a reproducible example of your dataset base?

Yup, the database is like this:

exampledata <- data.frame(fecha = seq(as.Date("1980-01-01"), as.Date("2020-05-31"), by = "quarter"),
                          bonos = sample(c(100:500), size = 162))
1 Like

The reactive outputs need to have() after them, e.g. dato1() and x().

I have edited this so it is the same as the example data that you provided:


library(shiny)
base <- data.frame(Fecha = seq(as.Date("1980-01-01"), as.Date("2020-05-31"), by = "quarter"),
                   bonos = sample(c(100:500), size = 162))

base$Fecha <-as.Date(base$Fecha,"%m/%d/%Y") 
shinyApp(
  
  ui = basicPage(
    dateInput("fecha1", "Ingresa una fecha inicial", value = "2020-04-01"),
    dateInput("fecha2", "Ingresa una fecha final", value = "2020-01-01"),
    tableOutput("output")
  ),
  server = function(input, output){
    dato1 <- reactive({base %>% 
        dplyr::filter(Fecha == as.Date(input$fecha1))})
    dato2 <- reactive({base %>% 
        dplyr::filter(Fecha == as.Date(input$fecha2))})
    
    x <- reactive({dato1()$bonos - dato2()$bonos})
    
    output$output <- renderPrint({if( x() < 0 ){
      print(paste("Hubo una entrada de capitales por", abs(x()),"millones de dólares"))
    } else {
      print(paste("Hubo una salida de capitales por", abs(x()),"millones de dólares"))
    } })
  }
)

Omg, it works. Thanks a lot!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.