Home > other >  R Shiny App Datetime Range Input rather than only Date
R Shiny App Datetime Range Input rather than only Date

Time:07-08

I'm working on building a Shiny App for work and currently have it up and running using a dateRangeInput() for pulling data. I'm looking for an alternative to this though so that users can be more specific with the range, so they can also choose time as an input as well. So basically my input looks like the following:

dateRangeInput('dates', 'Timeframe To Pull')

and is referenced in my server as

starddate = input$dates[1]
enddate = input$dates[2]

But this is restrictive, as some of their data cannot overlap and will switch profiles in the middle of the day (unscheduled). So having an input for datetime range rather than just date range would be huge. I know that there's a package called shinyTime out there, but it only works with times, not datetimes. So I'm looking to see if there's something cleaner out there.

Thanks in advance for any help!

CodePudding user response:

I've just done a package for you :-)

remotes::install_github("stla/DateTimeRangePicker")

The following example is included in the doc.

library(DateTimeRangePicker)
library(shiny)

ui <- fluidPage(
  br(),
  sidebarLayout(
    sidebarPanel(
      width = 5,
      tags$fieldset(
        tags$legend("Click to change time"),
        dtrpickerInput(
          "dtrpicker",
          style = paste0(
            "background-color: chartreuse; ",
            "box-shadow: 0 30px 40px 0 rgba(16, 36, 94, 0.2);"
          )
        )
      )
    ),
    mainPanel(
      width = 7,
      verbatimTextOutput("dtrpicker")
    )
  )
)

server <- function(input, output){
  output[["dtrpicker"]] <- renderPrint({
    input[["dtrpicker"]]
  })
}

shinyApp(ui, server)

enter image description here

Note that there's no clock for the second date. Perhaps it's a bug of the JavaScript library, I don't know.

  • Related