Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interactive shiny global date picker

Tags:

r

shiny

I'm working with R in an environment that doesn't have a good date picker and I'm trying to pop up a date picker using R to fill in the gap.

Most R date pickers require UI libraries like GTK. Shiny's doesn't. All I'd like to do is pop up a date picker, let the user choose the date and then end the shiny session and pass the date back to continue the R script.

Here's what I have so far, but no success. I'm trying to assign the date to a global variable x.

library("shiny")
## Only run examples in interactive R sessions
if (interactive()) {

  ui <- fluidPage(
    # Default value is the date in client's time zone
    dateInput("date2", "Date:"),
    verbatimTextOutput("date2")

  )

  shinyApp(ui, server = function(input, output) {
  reactive(x <<- input$date2)

  })
}

But the variable x doesn't show up in the global env.

like image 522
variable Avatar asked Dec 02 '25 06:12

variable


2 Answers

The issue here is that you're never calling that reactive object. In server, you'd have to do something like:

shinyApp(ui, server = function(input, output) {
  observeEvent(input$date2, {x <<- input$date2})
})

This way, a change in input$date2 will trigger the global assignment of x.

like image 148
bk18 Avatar answered Dec 04 '25 01:12

bk18


You could also do something like this, where you assign the value to the global env when the session ends.

library("shiny")
## Only run examples in interactive R sessions

ui <- fluidPage(
  # Default value is the date in client's time zone
  dateInput("date2", "Date:"),
  verbatimTextOutput("date2")

)

shinyApp(ui, server = function(input, output, session) {
  session$onSessionEnded(function() {
    assign("x", isolate(dat()), pos =  .GlobalEnv)
  })
  dat <- reactive({
    req(input$date2); 
    x <- input$date2
    x
    })
})
like image 32
SeGa Avatar answered Dec 03 '25 23:12

SeGa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!