R闪亮的dateRangeInput基于输入文件的更新 [英] R shiny dateRangeInput update based on input file
问题描述
我正在尝试制作一个闪亮的应用,它将用户.tsv文件作为输入,在定义的一列中查看一下时间范围,然后让用户从该时间范围中选择子集.
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.
我认为可以使用此信息在 dateRangeInput()
中设置最小值/最大值和开始/结束值.
I thought that it will be possible to use this information to set the min/max and start/end values in the dateRangeInput()
.
但是,即使在加载文件输入之后,这些字段仍为空白.我无法在global.R或其他任何地方设置这些日期,因为随着每个上载的文件而定,这些日期会发生变化.
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.
用户文件简化示例:
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(请原谅可能过于复杂的获取最小值/最大值的方法,下次我将使用 min()
和 max()
:)):
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.
推荐答案
由于 updateDateRangeInput
只能更新所选的值和标签(参考:
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
)
})
})
这篇关于R闪亮的dateRangeInput基于输入文件的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!