sliderInput显示标签上的日期时间但返回日期。

huangapple go评论57阅读模式
英文:

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 &lt;- basicPage(
  sliderInput(
    &quot;slider&quot;, 
    &quot;Time&quot;, 
    min   = as.Date(&quot;2010-12-31 00:00:00&quot;),
    max   = as.Date(&quot;2014-12-31 23:00:00&quot;),
    value = as.Date(&quot;2010-12-31 00:00:00&quot;),
    timeFormat=&quot;%F %T&quot;, 
    step = (1/24),
    animate = TRUE
  ),
  fluidRow(textOutput(&quot;SliderText&quot;))
)
server &lt;- shinyServer(function(input, output, session){
  output$SliderText &lt;- 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 &lt;- basicPage(
  sliderInput(
    &quot;slider&quot;, 
    &quot;Time&quot;, 
    min   = as.POSIXct(&quot;2010-12-31 00:00:00&quot;, tz = &quot;UTC&quot;),
    max   = as.POSIXct(&quot;2014-12-31 23:00:00&quot;, tz = &quot;UTC&quot;),
    value = as.POSIXct(&quot;2010-12-31 00:00:00&quot;, tz = &quot;UTC&quot;),
    step = 86400,
    animate = TRUE
  ),
  fluidRow(textOutput(&quot;SliderText&quot;))
)

server &lt;- shinyServer(function(input, output, session){
  output$SliderText &lt;- renderText({format(input$slider, &quot;%F %T&quot;)})
})

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(&quot;2010-12-31 00:00:00&quot;, tz = &quot;UTC&quot;)). To avoid that we are using format which will show 00:00:00 in time but will return output of type character.

huangapple
  • 本文由 发表于 2023年4月19日 17:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定