Multiple Conditions for conditionalPanel and help with calculating Age (display years and months)

560 views
Skip to first unread message

Ahmad MM

unread,
Dec 14, 2017, 12:41:18 PM12/14/17
to Shiny - Web Framework for R

Hello rstats family. I am building a shiny app to doing some data quality checking. In need of your help! 

2 Main Problems: 1) conditionalPanel w/ multiple conditions 2) calculating Age

Thanks in advance!


Background: I am building a shiny app (see here: https://camh-nds.shinyapps.io/STOPDataQuality/) to help with data quality control


PROBLEM #1

In the "SOR" tab, Step 1: the goal is to notify the user if DateSurveyed = WorkshopDate and display the appropriate message if it correct or not. This works fine.


For Step 2: a valid QuitDate falls between this range: DateSurveyed>= QuitDate <= DateSurveyed + days(30)

If this condition is satisfied display the following text: "The Quit Date entered is valid.

If the condition is not satisfied I would like to display the corrected quit dates.

  • The correct quit dates is based on:

i) if QuitDate < DateSurveyed, then display the following text: "Correct Quit Date is [value of DateSurveyed here]

ii) if QuitDate > DateSurveyed + days (30) then display the following text: "Correct Quit Date is [value of DateSurveyed + days(30) here].


Here is the ui code

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"))
              ),


server:

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"
}) 




})


Problem 2: Calculating Age

In the Age Tab, I simply want to calculate the age using the following formula: Age=(Date Sureyed-DateOfBirth)/365. I would like to display the answer as for example: 19 years old and 3 months if possible.


Here is the link the my Github repo: https://github.com/AhmadMobin/STOPDQ


Appreciate the help I can get!


Thanks in advance


Ahmad MM

unread,
Dec 15, 2017, 12:38:21 PM12/15/17
to Shiny - Web Framework for R
Hi All, 

I was able to solve the first problem!


Still stuck on Problem 2: Calculating Age

In the Age Tab, I simply want to calculate the age using the following formula: Age=(Date Sureyed-DateOfBirth)/365. I would like to display the answer as for example: 19 years old and 3 months and x days if possible.


Here is the link the my Github repo: https://github.com/AhmadMobin/STOPDQ


Appreciate the help I can get!


Thanks in advance


Reply all
Reply to author
Forward
0 new messages