【问题标题】:Calculate date-time interval in minutes on R shiny reactive table在 R 闪亮的反应表上计算日期时间间隔(以分钟为单位)
【发布时间】:2019-11-22 21:42:27
【问题描述】:

如何计算 R 闪亮反应表中 2 个日期时间列之间的间隔?

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)

错误:没有将“字符”强制转换为“NA”的方法或默认值

【问题讨论】:

标签: r shiny


【解决方案1】:

问题与代码本身有关,而不是因为它是反应式的:这在 reactive 之外也不起作用。

difftime(Sys.time() - 123, Sys.time(), units = "mins")
#> Time difference of -2.050001 mins

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-16
    • 2011-11-29
    • 2020-01-02
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多