0

How to calculate the interval between 2 date-time columns in reactive table in R shiny?

library(shiny)
library(rhandsontable)
library(lubridate)

DF = data.frame(baseline = c(as.POSIXct("2019-07-10 14:00:00")), time = c(as.POSIXct("2019-07-10 17:30:00")),interval = c(""), period = c(""))
numberofrows <- nrow(DF)

ui <- basicPage(mainPanel(rHandsontableOutput("hotable1")))

server <- shinyServer(function(input, output, session) {

    previous <- reactive({DF})

    MyChanges <- reactive({
        if(is.null(input$hotable1)){return(previous())}
        else if(!identical(previous(),input$hotable1)){

            mytable <- as.data.frame(hot_to_r(input$hotable1))

            mytable <- mytable[1:numberofrows,]

            # test cases
            mytable[,1][is.na(mytable[,1])] <- as.POSIXct("2019-07-10 14:00:00")
            mytable[,2][is.na(mytable[,2])] <- as.POSIXct("2019-07-10 17:30:00")
            mytable[,3] <- as.interval(mytable[,1], mytable[,2], unit="minutes")
            mytable[,4] <- as.period(mytable[,3], unit="minute")
            mytable
        }
    })
    output$hotable1 <- renderRHandsontable({rhandsontable(MyChanges())})
})


shinyApp(ui, server)

Error: no method or default for coercing “character” to “NA”

  • one issue is use of as.POSIXct for the date/time value in your data frame, this may not be an acceptable format with rhandsontable: https://stackoverflow.com/questions/44812595/r-shiny-updating-rhandsontable-from-another-rhandsontable – Ben Jul 13 '19 at 20:34

1 Answers1

0

The problem has to do with the code itself not the fact that it is reactive: this does not work outside of a reactive either.

difftime(Sys.time() - 123, Sys.time(), units = "mins")
#> Time difference of -2.050001 mins
JohnCoene
  • 2,107
  • 1
  • 14
  • 31