我可以在Shiny中以空dateRangeInput
和start
开头的end
。
dateRangeInput("date", "Date input", start = "", end = "")
在此日期范围内进行选择后,当我尝试
updateDateRangeInput(session, "date", start = "", end = "")
它不起作用。它可以与其他类型的输入配合使用(请参见下文)。
这是一个代表,当我单击go时,它放置了日期,当我单击go2时,它什么也没做。
library(shiny)
shinyApp(
ui = fluidPage(
h4("Date:"),
# Starting with an empty date
dateRangeInput("date", "Date input", start = "", end = ""),
actionButton("go","go"),
actionButton("go2","go2"),
# And an empty button
textInput("text", "text"),
actionButton("go3","go3")
),
server = function(input, output, session){
observeEvent( input$go , {
# Will change the date accordingly
updateDateRangeInput(
session,
"date",
start = Sys.Date()-10,
end = Sys.Date()-5
)
})
observeEvent( input$go2 , {
# Does nothing
updateDateRangeInput(
session,
"date",
start = "",
end = ""
)
})
observeEvent( input$go3 , {
# Empties the text input
updateTextInput(
session,
"text",
value = ""
)
})
}
)
答案 0 :(得分:2)
似乎需要日期格式。您可以使用NA
代替""
。
library(shiny)
shinyApp(
ui = fluidPage(
h4("Date:"),
dateRangeInput("date", label = h5("Date input"), start = "", end = ""),
actionButton("go","go"),
actionButton("go2","go2")
),
server = function(input, output, session){
observeEvent( input$go , {
cat("go1\n")
updateDateRangeInput(session, "date", start = Sys.Date()-10, end = Sys.Date()-5)
})
observeEvent( input$go2 , {
cat("go2\n")
updateDateRangeInput(session, "date", start = NA, end = NA)
})
}
)
答案 1 :(得分:1)
这是使用reset()
中的library(shinyjs)
而不是updateDateRangeInput
的另一种方法:
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
h4("Date:"),
# Starting with an empty date
dateRangeInput("date", "Date input", start = "", end = ""),
actionButton("go","go"),
actionButton("go2","go2"),
# And an empty button
textInput("text", "text"),
actionButton("go3","go3")
),
server = function(input, output, session){
observeEvent( input$go , {
# Will change the date accordingly
updateDateRangeInput(
session,
"date",
start = Sys.Date()-10,
end = Sys.Date()-5
)
})
observeEvent( input$go2 , {
reset("date")
})
observeEvent( input$go3 , {
# Empties the text input
updateTextInput(
session,
"text",
value = ""
)
})
}
)