【问题标题】:How to display age automatically when date of birth is entered in R shiny?在R闪亮中输入出生日期时如何自动显示年龄?
【发布时间】:2017-01-30 12:16:41
【问题描述】:

在R中输入出生日期时如何自动显示年龄? UI.R

shinyUI(
    fuildpage({
    column(2, actionButton("calculate", "Calculate age")),
    fluidRow(uiOutput("calculatedage")
})

服务器.R

library(shiny)
library(shinyjs)
library(shinythemes)

shinyServer(function(input, output,session){

    observeEvent( input$calculate, 
       output$calculatedage <- renderUI({isolate({

      fluidRow(
          column(3,dateInput("dob", label="DATE OF BIRTH:",min = "1960-01-01",
          max = Sys.Date(), format = "yyyy-mm-dd", startview = "year",
          weekstart = 0, language = "en")),

          column(3, textInput("age",label = "AGE:")),
         column(3,textInput("address",label = "Address:"))                                              
          )

  })}))           
})

在上面的代码中有一个叫做计算年龄的按钮,当点击它时,用户可以输入出生日期。当用户输入出生日期时,年龄应该自动显示在定义为“年龄”的文本框中。 如何在 R Shiny 中做到这一点

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    这个怎么样?

    library(shiny)
    library(shinyjs)
    library(shinythemes)
    
    ui <- shinyUI(
      fluidPage({
        column(2, actionButton("calculate", "Calculate age"),
        fluidRow(uiOutput("calculatedage")))
      })
    )
    
    server <- shinyServer(function(input, output,session){
    
          observeEvent( input$calculate, 
                        output$calculatedage <- renderUI({isolate({
    
                          fluidRow(
                            column(10,dateInput("dob", label="DATE OF BIRTH:",
                                               min = "1960-01-01",
                                               max = Sys.Date(), format = "yyyy-mm-dd", 
                                               startview = "year",
                                               weekstart = 0, language = "en")),
    
                            column(10, textInput("age",label = "AGE:")),
                            column(10,textInput("address",label = "Address:"))                                              
                          )
    
                        })}))  
    
          observe({   dob <- input$dob
                      if(!is.null(dob)) {
                         days <- as.integer((Sys.Date() - as.Date(dob)))
                         years <- as.integer(days / 365)
                         months <- as.integer((days %% 365 ) / 30)
                         age <- paste(years, 'year(s)', months, 'month(s)')                                          
                         #print(age)
                         updateTextInput(session, "age", value = age)
                      }
    
                })
     })
    
     shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢先生,它成功了,但是年龄可以打印成月和年的格式,例如3年2个月。
    • 更新了代码,你介意投票吗?
    • 谢谢先生,它成功了。先生,我的声望是一,我已投票但显示一条消息,记录声望低于 15 的人的投票,但不要更改公开显示的帖子分数。
    • 我明白了,如果你这么认为,你能不能选择我的答案作为最佳答案。
    【解决方案2】:

    创建了一个动态闪亮的应用程序,因此当您输入以前的日期时,它会自动在 3 个差异列中按天、月和年计算,您可以添加尽可能多的以前的日期,它会在闪亮的应用程序中生成表格输出

    library(shiny)
    library(lubridate)
    
    ui <- shinyUI( fluidPage(
                       titlePanel("Date calculator"),
                        sidebarLayout(
                        sidebarPanel(
                        textInput("Possible.date", label="Previous date", value= "2016-08-23"),
                        textInput("Current.date", label="Current .date", value=Sys.Date()),
                        actionButton("addButton", "Calculate")
                           ),
     mainPanel(
               tableOutput("table"))
               )))
    
    server = function(input, output) {    
    
    
    values <- reactiveValues()
    values$df <- data.frame(Possible.date = NA, Current.date = NA, Age_days = NA ,Age_months=NA, Age_years = NA)
    
    
    
    observeEvent(input$addButton, {
    
    if(input$addButton >= 0) {
      # create the new line to be added from your inputs
    
      newLine <- isolate(c(lubridate::ymd(input$Possible.date), lubridate::ymd(input$Current.date), 
        difftime(lubridate::ymd(input$Current.date),lubridate::ymd(input$Possible.date)),
        (difftime(lubridate::ymd(input$Current.date),lubridate::ymd(input$Possible.date)))/30,
        (difftime(lubridate::ymd(input$Current.date),lubridate::ymd(input$Possible.date)))/365))
      isolate(values$df <- rbind(as.matrix(values$df), unlist(newLine)))
      }
     })
    
     output$table <- renderTable({values$df}, include.rownames=F)
    
    
      }
    
     shinyApp(ui = ui, server = server)  
    

    【讨论】:

    • @Anu 如果您需要下载 .csv 格式的文件,可以添加下载按钮
    猜你喜欢
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    相关资源
    最近更新 更多