代码之家  ›  专栏  ›  技术社区  ›  sgdata

DT十进制时间-时钟时间转换

  •  1
  • sgdata  · 技术社区  · 7 年前

    我现在在shinny中使用DT来显示十进制时间。

    library(shiny)
    library(DT)
    library(dplyr)
    library(magrittr)
    
      # server-side processing
    
      base<-as.POSIXct('2000-01-01 00:00:00 EST')
    
      mtcars2 = mtcars[, 5:6]
      mtcars2 %<>% 
        mutate(drat = base+(3600*drat),
               wt = base+(3600*wt))
    
        DT::datatable(mtcars2,
            extensions = 'Buttons',
            options = list(
              scrollX = TRUE,
              scrollY = TRUE,
              pageLength = 10,
              dom = 'Blfrtip',
              buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
            )
      ) %>% 
      formatDate( 1:2,method = 'toLocaleString', params = list('en-US',year = NULL))
    

    我一直在考虑使用 toTimeString 或类似方法 formatDate() 第节 DT's functions . 我该如何将十进制时间“2.3”小时转换为“2:18”?我确实找到了 this question 在此之后,我将DT升级到最新的开发版本,包括 params= 但一直未能找到只为HH:MM选择的必要组合。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Yihui Xie    7 年前

    我认为你不可能通过 formatDate() ,因为我在JavaScript中没有看到任何支持抑制任何时间组件的日期转换方法。例如,如果你阅读 the documentation of toLocalString year 组件是 numeric 2-digit null 来抑制它。

    HH:MM 格式是在将数据传递给 DT::datatable() format(..., '%H:%M') .