Shiny DT slice date/time values

120 views
Skip to first unread message

Scott Goley

unread,
Sep 7, 2017, 10:09:32 AM9/7/17
to Shiny - Web Framework for R
Using the following script, I'm trying to cut the time values down to just HH:MM values. I noticed that DataTables originally in JS has the slice() method available for this kind of selection. However, so far I've been unable to implement this through functions / options currently available through DT including the params mentioned here


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 = 'toLocaleTimeString', params = list('en-US',hour = 'numeric', hour12 = 'true', minute = 'numeric'))


Final rows would have the format HH:MM for all values and drop seconds, AM/PM, and TZ. 

Thanks!

Greg L

unread,
Sep 9, 2017, 3:46:51 PM9/9/17
to Shiny - Web Framework for R
There are 2 minor issues with your formatDate params:

- the JSON serializer in DT uses R's built-in TRUE/FALSE logicals, not 'true' / 'false' strings

formatDate(1:2, method = 'toLocaleTimeString',
           
params = list('en-US', list(hour = 'numeric', minute = 'numeric', hour12 = FALSE)))


Btw, that DataTable slice looks like it clones the DataTable, probably not what you were thinking

Scott Goley

unread,
Sep 13, 2017, 10:05:25 AM9/13/17
to Shiny - Web Framework for R
Yes, sorry - that must have been from a copy / paste from one of my older versions but I am using R logicals and I do have the param options in a sub list. 

Can you confirm that even with those options specified, it still populates seconds, AM/PM, and Time Zone?

And yes, slice from DataTables isn't what I meant exactly. Something more like String.prototype.slice()

Thanks Greg. 

Greg L

unread,
Sep 13, 2017, 11:13:04 AM9/13/17
to Shiny - Web Framework for R
Are you running this in RStudio or a Shiny app? I just realized the RStudio Viewer (at least mine, v1.0.136) doesn't support toLocaleTimeString's locale or options arguments. Works fine in Chrome though.

If it's not supported where you're running this, then you could fall back to something like String.prototype.slice(). See the DataTables docs for how to use columns.render.

DT::datatable(mtcars2,
              extensions
= 'Buttons',
              options
= list(
                scrollX
= TRUE,
                scrollY
= TRUE,
                pageLength
= 10,
                dom
= 'Blfrtip',

                buttons
= c('copy', 'csv', 'excel', 'pdf', 'print'),
                columnDefs
= list(
                  list
(targets = c(1, 2), render = JS("
                    function(data) {
                      return data.slice(-9, -4);
                    }"
))
               
)
             
)
)

Reply all
Reply to author
Forward
0 new messages