1

I have the following flexdashboard written in R markdown.

---
title: "Untitled"
runtime: shiny
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

    ```{r global, include=FALSE}
    library(shiny)
    
    source("./date-arrows.R")
    ```

Input {.sidebar}
====================================

    ```{r input}
    dateRangeInput(inputId = "date_range", 
                   label = "Select Date Range", 
                   start = as.POSIXct("2022-01-01", "UTC"), 
                   end = as.POSIXct("2022-01-07", "UTC"), 
                   min = as.POSIXct("2022-01-01", "UTC"),
                   max = as.POSIXct("2022-01-31", "UTC"))
    dateArrowUI("date-arrows")
    dateArrowServer("date-arrows")
    ```

And I have the following date-arrows.R file in the same directory.

library(shiny)

dateArrowUI <- function(id){
  ns <- NS(id)
  tagList(
    fillRow(
      actionButton(ns("rightArrow"), " ", icon = icon("caret-right"))
    )
  )
}

dateArrowServer <- function(id){
  moduleServer(id, function(input, output, session){
    observeEvent(input$rightArrow, {
      min_date <- as.POSIXct("2022-01-01", "UTC")
      max_date <- as.POSIXct("2022-01-31", "UTC")
      
      start <- input$date_range[1]
      end <- input$date_range[2]
      interval <- difftime(end, start)
      
      if (!(end + interval) > as.Date(max_date)){
        updateDateRangeInput(session, 
                             inputId = "date_range", 
                             start = as.POSIXct(start + interval, "UTC"),
                             end = as.POSIXct(end + interval, "UTC"))
      }
    })
  })
}

When I try to use the button to move the dates in the dateRangeInput I get the following message.

Warning in `<observer:observeEvent(input$rightArrow)>`(...) :
  Incompatible methods ("Ops.difftime", "Ops.Date") for ">"
Warning: Error in if: argument is of length zero
  3: <Anonymous>
  1: rmarkdown::run

The error message is straightforward. However, I don't get why it is happening. Everything runs smoothly when I put the code directly in the dashboard file.

---
title: "Untitled"
runtime: shiny
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

    ```{r global, include=FALSE}
    library(shiny)
    
    source("./modules/date-arrows.R")
    ```

Input {.sidebar}
====================================

    ```{r input}
    dateRangeInput(inputId = "date_range", 
                   label = "Select Date Range", 
                   start = as.POSIXct("2022-01-01", "UTC"), 
                   end = as.POSIXct("2022-01-07", "UTC"), 
                   min = as.POSIXct("2022-01-01", "UTC"),
                   max = as.POSIXct("2022-01-31", "UTC"))
    fillRow(
      actionButton("rightArrow", "Move interval", icon = icon("caret-right"))
    )
    
    observeEvent(input$rightArrow, {
      min_date <- as.POSIXct("2022-01-01", "UTC")
      max_date <- as.POSIXct("2022-01-31", "UTC")
      
      start <- input$date_range[1]
      end <- input$date_range[2]
      interval <- difftime(end, start)
      
      if (!(end + interval) > as.Date(max_date)){
        updateDateRangeInput(session, 
                             inputId = "date_range", 
                             start = as.POSIXct(start + interval, "UTC"),
                             end = as.POSIXct(end + interval, "UTC"))
      }
    })
    ```

Carlos Davila
  • 129
  • 2
  • 9

0 Answers0