简体   繁体   中英

Calculate date-time interval in minutes on R shiny reactive table

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”

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM