mercredi 28 mars 2018

If statements with checkboxgroupinput, how to call out dynamic variables in the server.

How can I edit the code below so that when one of the check boxes is checked, it'll add days to the Approval.Date? I can't seem to get the if statements below to recognize the input variable. Any help? I know my main problem is in the if statement. I've tried several things but I think that I don't quite understand how to call out variables that I create in the second observe event in the server.

# loading libraries ####
#rm(list = ls())
library(shiny)
library(ggplot2)
library(dplyr)
library(shinythemes)
library(reshape)

# UI ####
ui <- fluidPage(theme = shinytheme("cerulean"),
                tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }"),  #Hides errors when the date is too low. 
                titlePanel("Drug Schedule"),
                tabsetPanel(
                  tabPanel("Plot", fluid = TRUE,
                           sidebarLayout(
                             sidebarPanel(dateRangeInput("DateRange","Date Range",  min= as.Date("2000/01/01"), max= as.Date("2060/01/01"), start = as.Date("2010/01/01"), end = as.Date("2019/01/01")),
                                          htmlOutput("IndicationGroup"),#add selectinput boxs
                                          htmlOutput("DrugName"),
                                          htmlOutput("UpdatingDates")),
                             mainPanel(plotOutput("DrugSchedule"))))

))

# Server ####
server <- function(input, output) {
  # Data ####

  mdfr2 <- read.table(header=TRUE, stringsAsFactors = FALSE,
                     text="ID Drug.Name Indication.Group Filing.Date Approval.Date MyBreak PriorityReview
                     1 HDrug_one HIV 2014-01-21 2015-04-11 yes yes
                     2 HDrug_two HIV 2017-09-22 2018-02-21 no yes
                     3 HDrug_three  HIV 2012-11-17 2016-05-15 no yes
                     4 HDrug_four HIV 2014-11-22 2016-10-18 no no
                     5 HDrug_five HIV 2010-12-30 2013-04-19 yes yes
                     6 HDrug_six AIDS 2012-11-22 2016-10-18 no no
                     7 HDrug_seven AIDS 2011-12-30 2013-04-19 yes yes"
  )
  mdfr2$Filing.Date <- as.Date(mdfr2$Filing.Date) 
  mdfr2$Approval.Date <- as.Date(mdfr2$Approval.Date)

  mdfr <- melt(mdfr2, measure.vars = c("Filing.Date","Approval.Date"),na.rm = TRUE)

  output$IndicationGroup <- renderUI({ 
    selectInput("IndicationGroup", "Indication Group", choices= unique(mdfr$Indication.Group),selected = unique(mdfr$Indication.Group)[4])
  })

  observeEvent(input$IndicationGroup,({
    output$DrugName <- renderUI({
      data_available <- mdfr[mdfr$Indication.Group==input$IndicationGroup,]
      checkboxGroupInput(inputId = "DrugName", label = "Drug", choices = unique(data_available$Drug.Name), selected = unique(data_available$Drug.Name)[1])
    })
  })
  )

  observeEvent(input$DrugName,({
    output$UpdatingDates <- renderUI({
      updatedDates <- vector("list",length(input$DrugName)) 
      for(i in 1: length(input$DrugName)){
        updatedDates[[i]] <- list(checkboxGroupInput(inputId = paste0("updatedDates",i),paste("Update:",input$DrugName[i]),choices = c("None"="None", "Priority Review"="PR", " Priority Review and Breakthrough"="BRK"), selected = "None"))
      }
      return(updatedDates)
    })
  })
  )

observeEvent(input$UpdatingDates,({
  data_available <- mdfr2[mdfr2$Indication.Group==input$IndicationGroup & mdfr2$Drug.Name==input$DrugName,]
  for(i in 1:length(input$DrugName)){
    if(updatedDates[[i]] == "None"){
      mdfr[mdfr$Indication.Group==input$IndicationGroup & mdfr$Drug.Name==input$DrugName[[i]] & mdfr$variable == "Approval.Date","value"]<- data_available$Approval.Date[i] + 125
    }else if(updatedDates[[i]] == "BRK"){
      mdfr[mdfr$Indication.Group==input$IndicationGroup & mdfr$Drug.Name==input$DrugName[[i]] & mdfr$variable == "Approval.Date","value"]<- data_available$Approval.Date[i] + 125 +25
    } else if(updatedDates[[i]] == "None"){
      mdfr[mdfr$Indication.Group==input$IndicationGroup & mdfr$Drug.Name==input$DrugName[[i]] & mdfr$variable == "Approval.Date","value"]<- data_available$Approval.Date[i]
    }
  }
})
)



  filtered <- reactive({
    filtered <- mdfr %>%
      filter(Indication.Group  %in% input$IndicationGroup,
             Drug.Name  %in% input$DrugName)
  })

  output$DrugSchedule <- renderPlot({
    if (is.null(filtered())) {
      return()
    }
    ggplot(filtered(), aes(as.Date(value, "%m/%d/%Y"), Drug.Name))+ 
      geom_point(data= filtered()[filtered()$variable=="Filing.Date",], aes(as.Date(value, "%m/%d/%Y"), Drug.Name))+
      #geom_point(data= filtered()[filtered()$variable=="PDUFA.Date",], aes(as.Date(value, "%m/%d/%Y"), Drug.Name))+
      geom_point(data= filtered()[filtered()$variable=="Approval.Date",], aes(as.Date(value, "%m/%d/%Y"), Drug.Name))+
      #geom_point(data= filtered()[filtered()$variable=="Start.Marketing.Date",], aes(as.Date(value, "%m/%d/%Y"), Drug.Name))+
      xlab("Date") + ylab("") +  ggtitle("Drug Schedule")+
      scale_x_date(date_breaks = "1 year", date_labels =  "%b %Y")+
      theme(axis.text.x=element_text(angle=60, hjust=1),axis.text.y = element_blank(),axis.ticks.y = element_blank())+
      geom_line(data=(filtered()[filtered()$variable=="Filing.Date" |  filtered()$variable=="Approval.Date",]), aes(as.Date(value, "%m/%d/%Y"), Drug.Name), linetype="solid", size=1)+
      #geom_line(data=(filtered()[filtered()$variable!="PrimCompDate",]), aes(as.Date(value, "%m/%d/%Y"), name), size=1)+
      #facet_wrap(~Country, nrow=2,scales = "free")+
      geom_text(data= subset(filtered()[filtered()$variable=="Approval.Date",]),aes(as.Date(value, "%Y-%m-%d"), Drug.Name, label=Drug.Name), hjust = -.1)+
      coord_cartesian(xlim=c(as.Date(input$DateRange[1]),as.Date(input$DateRange[2])))
  })
}

# Running the app ####
shinyApp(ui = ui, server = server)

whenever i try to

Aucun commentaire:

Enregistrer un commentaire