英文:
sliderInput shows on labels datetime but return date
问题
以下是您提供的代码的中文翻译部分:
我想在我的 `Shiny App` 中使用带有 `datetimes` 的 `sliderInput`。我发现可以通过 `timeFormat` 选项来实现。我添加了步进值 (1/24) 以每小时跳跃,时间轴上看起来不错,但当我检查 `sliderInput` 返回的内容时,发现它只返回了日期。是否可能获得与在 `sliderInput` 上显示的相同值?
简单的代码示例:
rm(list=ls())
library(shiny)
ui <- basicPage(
sliderInput(
"slider",
"时间",
min = as.Date("2010-12-31 00:00:00"),
max = as.Date("2014-12-31 23:00:00"),
value = as.Date("2010-12-31 00:00:00"),
timeFormat="%F %T",
step = (1/24),
animate = TRUE
),
fluidRow(textOutput("SliderText"))
)
server <- shinyServer(function(input, output, session){
output$SliderText <- renderText({as.character(input$slider)})
})
shinyApp(ui = ui, server = server)
请注意,我已经将代码中的HTML实体转换成了相应的R代码,以使其在Shiny应用中工作。
英文:
I would like to have in my Shiny App
sliderInput
with datetimes
. I found out that it is possible to do by timeFormat
option. I add step (1/24) to jump by 1 hour and on timeline it looks fine, but when I checked what sliderInput
return I found out that it returns only date
. It is possbile to get the same value that we have shown on sliderInput
?
Simply code to show example:
rm(list=ls())
library(shiny)
ui <- basicPage(
sliderInput(
"slider",
"Time",
min = as.Date("2010-12-31 00:00:00"),
max = as.Date("2014-12-31 23:00:00"),
value = as.Date("2010-12-31 00:00:00"),
timeFormat="%F %T",
step = (1/24),
animate = TRUE
),
fluidRow(textOutput("SliderText"))
)
server <- shinyServer(function(input, output, session){
output$SliderText <- renderText({as.character(input$slider)})
})
shinyApp(ui = ui, server = server)
答案1
得分: 1
以下是要翻译的内容:
"min"、"max" 和 "value" 中的值应为 "POSIXct" 类型,如果您希望滑块的输出为日期时间。在滑块中使用日期时间值时,"step" 将成为以秒为单位的值,因此如果我们希望表示一天,我们需要将其更改为 86400。
library(shiny)
ui <- basicPage(
sliderInput(
"slider",
"时间",
min = as.POSIXct("2010-12-31 00:00:00", tz = "UTC"),
max = as.POSIXct("2014-12-31 23:00:00", tz = "UTC"),
value = as.POSIXct("2010-12-31 00:00:00", tz = "UTC"),
step = 86400,
animate = TRUE
),
fluidRow(textOutput("SliderText"))
)
server <- shinyServer(function(input, output, session){
output$SliderText <- renderText({format(input$slider, "%F %T")})
})
shinyApp(ui = ui, server = server)
请注意,"input$slider" 返回类型为 "POSIXct" 的输出。然而,当时间为 "00:00:00" 时,默认情况下不会显示它(尝试使用 "as.POSIXct("2010-12-31 00:00:00", tz = "UTC")")。为了避免这种情况,我们使用 "format",它将在时间中显示 "00:00:00",但将返回字符类型的输出。
英文:
The values in min
, max
and value
should be of type POSIXct
if you want output from slider to be of datetime. In case of datetime values in slider the step
then becomes value in seconds so we need to change it to 86400 if we want it to represent a day.
library(shiny)
ui <- basicPage(
sliderInput(
"slider",
"Time",
min = as.POSIXct("2010-12-31 00:00:00", tz = "UTC"),
max = as.POSIXct("2014-12-31 23:00:00", tz = "UTC"),
value = as.POSIXct("2010-12-31 00:00:00", tz = "UTC"),
step = 86400,
animate = TRUE
),
fluidRow(textOutput("SliderText"))
)
server <- shinyServer(function(input, output, session){
output$SliderText <- renderText({format(input$slider, "%F %T")})
})
shinyApp(ui = ui, server = server)
Note that input$slider
returns output of type POSIXct
. However, when you have time as 00:00:00
then it is not displayed by default(Try as.POSIXct("2010-12-31 00:00:00", tz = "UTC")
). To avoid that we are using format
which will show 00:00:00
in time but will return output of type character.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论