Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Created June 1, 2015 03:09
Show Gist options
  • Select an option

  • Save mrdwab/31de50bd4c1cbc69ba6d to your computer and use it in GitHub Desktop.

Select an option

Save mrdwab/31de50bd4c1cbc69ba6d to your computer and use it in GitHub Desktop.
Half my life....
half_my_life <- function(birth = "1978-09-05", event_date = "1997-05-30") {
birth <- as.Date(birth)
event_date <- as.Date(event_date)
if (event_date <= birth) stop("You've got your dates mixed up....")
Duration <- (event_date - birth) * 2
Date <- birth + (Duration)
FDate <- format(Date, "%A %d %B %Y")
Age <- as.numeric(Duration)/365.242
if (Date < Sys.Date()) {
String <- c("You were able to say that this event has been half your life on ",
"\n%s, \n", "at which point you would have been about \n%02f years old.\n\n")
} else {
String <- c("You will be able to say this event has been half of your life on ", "\n%s, \n",
"at which point you would be about \n%.02f years old.\n\n")
}
sprintf(paste(String, collapse = ""), FDate, Age)
# return(invisible(list(Target = Date, Target_Formatted = FDate, Age = Age)))
}
# server.R
library(shiny)
shinyServer(function(input, output) {
output$summary <- renderText({
input$goButton
paste0('The start date specified is ', format(input$birth, "%d %B %Y"),
', and the event date specified is ', format(input$event, "%d %B %Y"), ".")
})
output$value <- renderText({
half_my_life(input$birth, input$event)
})
})
# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Half my life"),
sidebarLayout(
sidebarPanel(
dateInput("birth",
label = h3("Birth"),
value = "1978-09-05"),
hr(),
dateInput("event",
label = h3("Event Date"),
value = "1997-05-30"),
br(),
actionButton("goButton", "Go!")
),
mainPanel(
h4("Input Values"),
textOutput("summary"),
h4("Results"),
textOutput("value")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment