【问题标题】:Condiontionalpannel Multiple Conditions and Calculating Age (R Shiny Dashboard)Condiontionalpannel 多重条件和计算年龄(R Shiny Dashboard)
【发布时间】:2017-12-14 17:35:34
【问题描述】:

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

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

问题 #1

在“SOR”选项卡中,第 1 步:目标是在 DateSurveyed = WorkshopDate 时通知用户,并在正确与否时显示相应的消息。这很好用。

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

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

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

  • 正确的戒烟日期基于:

i) 如果QuitDate DateSurveyed,则显示以下文本:“正确的退出日期是[此处DateSurveyed的值]

ii) 如果QuitDate > DateSurveyed + days (30) 则显示以下文本:“正确的退出日期是 [DateSurveyed + days(30) 的值]。

这是用户界面代码

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:计算年龄

在“年龄”选项卡中,我只想使用以下公式计算年龄: 年龄=(确定日期-出生日期)/365。例如,我想显示答案:19 岁零 3 个月(如果可能)。

这是我的 Github 仓库的链接:https://github.com/AhmadMobin/STOPDQ

感谢我能得到的帮助!

提前致谢

【问题讨论】:

    标签: r date shiny


    【解决方案1】:

    因为日期操作在 javascript 中不像在闪亮中那样容易 - 我会用 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)
    

    希望这会有所帮助!

    【讨论】:

    • @AhmadMobin 抱歉忘记了我的代码也更新了解决方案。请检查是不是你说的
    • 嗨@Bertil Nestorius,这让我朝着正确的方向前进。我还想根据以下条件提供正确的 QuitDates:i) 如果 QuitDate DateSurveyed + days (30)(退出日期在调查日期后 31 天)然后显示以下文本:“正确的退出日期是 [此处 DateSurveyed 的值 + days(30)]。
    • 嗨@Bertil Nestorius 您的更新代码没有显示基于此逻辑的正确退出日期:i)如果退出日期早于调查日期,则更正的退出日期是调查日期ii)否则退出日期是在调查日期加上 30 天之后,然后更正的退出日期是 DateSurvey+30 天,希望这是有道理的。非常感谢您的帮助,取得进展!
    • 抱歉,有点困 - 现在可以了吗?
    • 非常感谢@Bertil Nestorius。您的代码有效!我清理了它,但最终代码可以在这里找到:github.com/AhmadMobin/STOPDQ你已经帮了我很多,我很难过一直打扰你,但你有没有机会帮助我解决问题#2?问题 2:计算年龄 在“年龄”选项卡中,我只想使用以下公式计算年龄:Age=(Date Sureyed-DateOfBirth)/365。我想显示答案,例如:如果可能的话,19 岁零 3 个月零 12 天。闪亮的应用程序在这里:camh-nds.shinyapps.io/STOPDataQuality
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2020-09-25
    • 1970-01-01
    相关资源
    最近更新 更多