R闪亮的dateRangeInput基于输入文件更新

时间:2016-02-24 14:53:58

标签: r date range shiny

我正在尝试创建一个将用户.tsv文件作为输入的闪亮应用, 查看一个已定义的列以确定时间范围,然后让用户从此时间范围中选择子集。

我可以加载用户文件,在文件中找到最小和最大日期。

我认为可以使用此信息设置dateRangeInput()中的最小值/最大值和开始/结束值。

即使加载了文件输入,字段仍为空白。我不能在global.R或其他任何地方设置这些日期,因为这会随着每个上传的文件而改变。

用户文件简化示例:

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

(真正的列有更多列,但这在这里并不重要)

server.R(请原谅可能非常复杂的获取最小值/最大值的方法,下次我会使用min()max() :)):

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')


    )
  )
)

非常感谢您的任何提示。

1 个答案:

答案 0 :(得分:1)

由于updateDateRangeInput只能更新所选的值和标签(参考:here),您可以使用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
    )
  })
})