Condiontionalpannel多条件和计算年龄(R Shiny Dashboard)

时间:2017-12-14 17:35:35

标签: r date shiny

我想知道我是否能得到你的帮助。

背景:我正在构建一个闪亮的应用(请参阅此处:https://camh-nds.shinyapps.io/STOPDataQuality/)以帮助进行数据质量控制

问题#1

在" SOR"选项卡,步骤1:目标是在DateSurveyed = WorkshopDate时通知用户,如果正确与否则显示相应的消息。这很好。

对于第2步:有效QuitDate介于此范围之间: DateSurveyed> = QuitDate< = DateSurveyed +天(30)

如果满足此条件,请显示以下文本:"输入的退出日期有效。

如果条件不满意,我想显示更正的退出日期。

  • 正确的退出日期基于:

i)如果QuitDate< DateSurveyed,然后显示以下文字:"正确退出日期为[此处DateSurveyed的值]

ii)如果QuitDate> DateSurveyed +天(30)然后显示以下文字:"正确退出日期为[此处DateSurveyed + days(30)的值]。

这是ui代码

dashboardBody(
            tabItems(  
              #Data Quality for Stop on The Road (SOR)
              tabItem(tabName="SOR",
              #Checking if Date Surveyed= Workshop Date         
                      h3("Step 1- check to see if Date Surveyed = Workshop Date"),
                      HTML ('</br>'),
              #Entering Date Surveyed  
                      dateInput('date',
                        label='Date Surveyed: yyyy-mm-dd'),
              #Entering Workshop Date    
                   dateInput('date2',
                       label='Workshop Date: yyyy-mm-dd'),
              #If DateSurveyed=WorkShopDate display this message
              conditionalPanel("input.date==input.date2",
                               textOutput("EqualDates")),
              #If DateSurveyed!=WorkshopDate display this message
              conditionalPanel ("input.date!=input.date2",
                               textOutput("ErrorDates")),
              HTML ('</br>'),
              HTML ('</br>'),
              HTML ('</br>'),
              #Checking to see if a Valid Quit Date has been entered
              h3("Step 2- check to see if subject has entered a valid quit date"),
              #Description to user what is a Valid Quit date 
              h5("* Note: Quit Dates can start as early as Date Surveyed or be set on 30 days after the Date Surveyed"),
              HTML ('</br>'),
              #Entering Quit Dates
              dateInput('date3',
                        label='Quit Date: yyyy-mm-dd'),

              #Valid Quit Dates: if QuitDate>=DateSurveyed AND QuitDate<=DateSurveyed + days(30); 
              #Then display message
              conditionalPanel("input.date3>=input.date && input.date3<=input.date + days(30)",
              #HELP! Conditional statement is not working!!!                 
                               textOutput("ValidQuitDate")),

              #Incorrect Quit Dates: if QuitDate<DateSurveyed OR QuitDate>DateSurveyed + days(30); 
              #Then display message 
              conditionalPanel("input.date3<input.date || input.date3>input.date + days(30)",
              #HELP! Conditional statement is not working!!!                 
                               textOutput("InvalidQuitDate"))
              ),

服务器:

library(lubridate)
library(shiny)
library(shinydashboard)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

#SOR Date Surveyed   
output$dateText  <- renderText({
paste("input$date is", as.character(input$date))
})

#SOR Work Shop Date  
output$dateText2 <- renderText({
paste("input$date2 is", as.character(input$date2))
})

#SOR: if DateSurveyed=WorkshopDate then display this message  
 output$EqualDates<- renderText({
 "**CORRECT** Dates are Equal!!!"
 })
#SOR: if DateSurveyed!= WorkshopDate then display this message   
 output$ErrorDates<- renderText({
 "**ERROR** Dates are NOT Equal"
})
#SOR: if QuitDate=DateSurveyed OR QuitDate<= DateSurveyed+ days(30) then 
display this message 
output$ValidQuitDate<- renderText({
 "You have entered a Valid Quit Date"
}) 

#SOR: if QuitDate<DateSurveyed OR QuitDate>DateSurveyed+ days(30) then 
display this message
output$InvalidQuitDate<- renderText({
 "**ERROR** Incorrect Quit Date Entered"
}) 




})

问题2:计算年龄

在Age选项卡中,我只想使用以下公式计算年龄: 年龄=(Date Sureyed-DateOfBirth)/ 365。我想展示答案,例如:19岁和3个月,如果可能的话。

以下是我的Github回购链接:https://github.com/AhmadMobin/STOPDQ

感谢我能得到的帮助!

提前致谢

1 个答案:

答案 0 :(得分:0)

因为日期操作在javascript中不像在flash中那样简单 - 我会用conditionalPanel来解决这个问题,而是在renderText自己解决这个问题

library(lubridate)
library(shiny)
library(shinydashboard)

# Define server logic required to draw a histogram
server <- function(input, output) {

  #SOR Date Surveyed   
  output$dateText  <- renderText({
    paste("input$date is", as.character(input$date))
  })

  #SOR Work Shop Date  
  output$dateText2 <- renderText({
    paste("input$date2 is", as.character(input$date2))
  })

  #SOR: if DateSurveyed=WorkshopDate then display this message  
  output$TestDates<- renderText({
    if(input$date == input$date2){
      "**CORRECT** Dates are Equal!!!"
    } else {
      "**ERROR** Dates are NOT Equal"
    }
  })
  #SOR: if DateSurveyed!= WorkshopDate then display this message   
  output$ErrorDates<- renderText({
  })
  #SOR: if QuitDate=DateSurveyed OR QuitDate<= DateSurveyed+ days(30) then 
  # display this message 
  output$ValidQuitDate<- renderText({
    if(input$date3 >= input$date && input$date3 <= input$date + 30){
      "You have entered a Valid Quit Date"
    } else if(input$date3 < input$date){
      paste("**ERROR** Incorrect Quit Date Entered
      Correct Quit Date is",input$date)
    } else {
      paste("**ERROR** Incorrect Quit Date Entered
      Correct Quit Date is",input$date + days(30))
    }
  }) 

  #SOR: if QuitDate<DateSurveyed OR QuitDate>DateSurveyed+ days(30) then 
  # display this message
  output$InvalidQuitDate<- renderText({
  }) 




}
ui <- dashboardBody(
  tabItems(  
    #Data Quality for Stop on The Road (SOR)
    tabItem(tabName="SOR",
            #Checking if Date Surveyed= Workshop Date         
            h3("Step 1- check to see if Date Surveyed = Workshop Date"),
            HTML ('</br>'),
            #Entering Date Surveyed  
            dateInput('date',
                      label='Date Surveyed: yyyy-mm-dd'),
            #Entering Workshop Date    
            dateInput('date2',
                      label='Workshop Date: yyyy-mm-dd'),
            #If DateSurveyed=WorkShopDate display this message
            textOutput("TestDates"),
            HTML ('</br>'),
            HTML ('</br>'),
            HTML ('</br>'),
            #Checking to see if a Valid Quit Date has been entered
            h3("Step 2- check to see if subject has entered a valid quit date"),
            #Description to user what is a Valid Quit date 
            h5("* Note: Quit Dates can start as early as Date Surveyed or be set on 30 days after the Date Surveyed"),
            HTML ('</br>'),
            #Entering Quit Dates
            dateInput('date3',
                      label='Quit Date: yyyy-mm-dd'),

            #Valid Quit Dates: if QuitDate>=DateSurveyed AND QuitDate<=DateSurveyed + days(30); 
            #Then display message
            textOutput("ValidQuitDate")
    )
  )
)
shinyApp(ui,server)

希望这有帮助!