R shiny dateRangeInput update based on input file

2019-07-28 00:26发布

I'm trying to make a shiny app that would take user .tsv file as input, take a look in one defined column to decide on time range and then let the user select subset from this time range.

I am able to load in the user file, find the minimum and maximum date in the file.

I thought that it will be possible to use this information to set the min/max and start/end values in the dateRangeInput().

Yet the fields are blank even after file input is loaded. I cannot have these dates set in global.R or anywhere else, since this will change with each uploaded file.

User file simplified example:

V1  V2  V3
7753    7 Jan 14    09:50:00
7754    7 Jan 14    09:55:00
8366    9 Jan 14    12:55:00
8471    9 Jan 14    21:40:00
8472    9 Jan 14    21:45:00
8552    10 Jan 14   04:25:00
8553    10 Jan 14   04:30:00

(real one has more columns, but that is unimportant here)

server.R (please excuse the probably very over-complicated way of getting the min/max, I will use min() and max() next time :) ):

library(shiny)
lct <- Sys.getlocale("LC_TIME") #this bit here is to have the correct locale
Sys.setlocale("LC_TIME", "C")
options(shiny.maxRequestSize = 30 * 1024 ^ 2) #file-size limit
#loading in the file from user
shinyServer(function(input, output) {
  myData <- reactive({
    inF <- input$inFile
    if (is.null(inF))
      return(NULL)
    data <- read.delim(inF$datapath, header = FALSE)
    data
  })
 #getting the minimum date appearing in the file 
  output$datRangeMin <- reactive({
    inF <- input$inFile
    if (is.null(inF))
      return(NULL)
    insidedatRangeMin <- head(c(sort(unique(as.Date(myData()$V2,format = "%d %b %y")))), n=1)
    insidedatRangeMin
  })

   #getting the maximum date appearing in the file 
  output$datRangeMax <- reactive({
    inF <- input$inFile
    if (is.null(inF))
      return(NULL)
    insidedatRangeMax <- tail(c(sort(unique(as.Date(myData()$V2,format = "%d %b %y")))), n=1)
    insidedatRangeMax
  })


})

ui.R:

library(shiny)


shinyUI(fluidPage(

  fileInput("inFile", "Upload monitor file:"),

  dateRangeInput('expDateRange', label = "Choose experiment time-frame:",
                 start =  'datRangeMin', end =  'datRangeMax', 
                 min =  'datRangeMin', max =  'datRangeMax',
                 separator = " - ", format = "yyyy-mm-dd",
                 language = 'cz', weekstart = 1
  ),
    mainPanel(
      verbatimTextOutput('datRangeMin'),
      verbatimTextOutput('datRangeMax')


    )
  )
)

Thank you very much for any hint.

标签: r date range shiny
1条回答
Evening l夕情丶
2楼-- · 2019-07-28 01:17

Since updateDateRangeInput can only update the selected values and the labels (ref: here), you can solve this with a renderUI:

ui.R

library(shiny)

shinyUI(
  fluidPage(
    fileInput("inFile", "Upload monitor file:"),
    uiOutput("dates")
  )
)

server.R

library(shiny)
lct <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
options(shiny.maxRequestSize = 30 * 1024 ^ 2) 

shinyServer(function(input, output) {
  myData <- reactive({
    validate(
      need(input$inFile, "Select a file to load")
    )
    inF <- input$inFile
    read.delim(inF$datapath, header = FALSE)
  })

  output$dates <- renderUI({
    dates <- as.Date(myData()$V2, format = "%d %b %y")
    minval <- min(dates)
    maxval <- max(dates)
    dateRangeInput('expDateRange', label = "Choose experiment time-frame:",
                   start = minval, end = maxval, 
                   min = minval, max = maxval,
                   separator = " - ", format = "yyyy-mm-dd",
                   language = 'cz', weekstart = 1
    )
  })
})
查看更多
登录 后发表回答