如何捕获两个 inputDate() 并计算 shinyapp 中的天数?

How to capture two inputDate() and calculate number of days in shinyapp?

我正在尝试捕获两个不同的 'dateInput()' 并计算 shinyApp 中的天数。 有人可以帮我解决这个问题吗?

我的代码:

library(shiny)
library(shinydashboard)


ui <- shinyUI(dashboardPage(
  dashboardHeader(title = "Leave"),
  dashboardSidebar(),
  dashboardBody(
    column(4, dateInput('st', "Start Date", format = "yyyy-mm-dd", width = '200px')),
    column(4, dateInput('ed',"End Date", format = "yyyy-mm-dd", width = '200px')),
    column(4, valueBoxOutput('caldif'))
    )
              
))

server <- shinyServer(function(input,output,session){
  output$caldif <-  renderValueBox("Days",input$st - input$ed)
})
shinyApp(ui,server)

主要问题是 renderValueBox 的调用。它需要一个 valueBox() 里面。 difftime 可能是计算时差的最佳函数。

library(shiny)
library(shinydashboard)


ui <- shinyUI(dashboardPage(
  dashboardHeader(title = "Leave"),
  dashboardSidebar(),
  dashboardBody(
    column(4, dateInput('st', "Start Date", format = "yyyy-mm-dd", width = '200px')),
    column(4, dateInput('ed',"End Date", format = "yyyy-mm-dd", width = '200px')),
    column(4, valueBoxOutput('caldif', width = 8))
  )
  
))

server <- shinyServer(function(input,output,session){
  output$caldif <-  renderValueBox({  
      #valueBox needed here
      #difftime will calculate the time difference
      valueBox('Days', as.character(difftime(input$ed, input$st)))
    })
})
shinyApp(ui,server)

输出: