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.
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.
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
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With