So my basic problem is that I am dynamically creating a certain number of selectInputs and want to use the values from those inputs to edit the dates on a data table. The issue is that the server wont remember the input$.... for the selectInputs that I create for me to call out in an if statement. Run the code and you'll see what I'm talking about!
library(shiny)
library(ggplot2)
library(dplyr)
library(shinythemes)
library(reshape)
mdfr <- read.table(header=TRUE, text="ID Drug.Name Indication.Group Filing.Date Approval.Date MyBreak PriorityReview
1 APREMILAST HIV 2014-01-21 2015-04-11 yes yes
2 LIFITEGRAST HIV 2017-09-22 2018-02-21 no yes
3 CHOLIC HIV 2012-13-17 2016-05-15 no yes
4 crofelemer HIV 2014-11-22 2016-10-18 no no
5 ezogabine HIV 2010-12-30 2013-04-19 yes yes"
)
mdfr <- melt(mdfr, measure.vars = c("Filing.Date","Approval.Date"),na.rm = TRUE)
# 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("Indication.Group"),#add selectinput boxs
htmlOutput("Drug.Name"),
htmlOutput("UpdatingDates")),
mainPanel(plotOutput("DrugSchedule"))))
))
# Server ####
server <- function(input, output) {
mdfr <- reactiveValues()
mdfr <- 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 drugone AIDS 2012-11-22 2016-10-18 no no
7 drugtwo AIDS 2011-12-30 2013-04-19 yes yes"
)
mdfr$Filing.Date <- as.Date(mdfr$Filing.Date)
mdfr$Approval.Date <- as.Date(mdfr$Approval.Date)
mdfr <- melt(mdfr, measure.vars = c("Filing.Date","Approval.Date"),na.rm = TRUE)
output$Indication.Group <- renderUI({
selectInput("Indication.Group", "Indication Group", choices= unique(mdfr$Indication.Group),selected = unique(mdfr$Indication.Group)[4])
})
output$Drug.Name <- renderUI({
data_available <- mdfr[mdfr$Indication.Group==input$Indication.Group,]
checkboxGroupInput(inputId = "Drug.Name", label = "Drug", choices = unique(data_available$Drug.Name), selected = unique(data_available$Drug.Name)[1])
})
output$UpdatingDates <- renderUI({
data_available <- mdfr[mdfr$Indication.Group==input$Indication.Group,]
updatedDates <- vector("list",length(data_available$Drug.Name))
for(i in 1: length(data_available$Drug.Name)/2){
updatedDates[i] <- list(selectInput(inputId = paste0("updatedDates",i),paste("Update:",LETTERS[i]),choices = c("None"="None", "Priority Review"="PR", " Priority Review and Breakthrough"="BRK"), selected = "None"))
}
return(updatedDates)
})
filtered <- reactive({
filtered <- mdfr %>%
filter(Indication.Group %in% input$Indication.Group,
Drug.Name %in% input$Drug.Name)
for(i in 1:length(input$Drug.Name)){
if(input[[paste("Update:",LETTERS[i])]] == "PR"){
mdfr()[i,mdfr()$Approval.Date] <- mdfr()[i,mdfr()$Approval.Date] + 125
}else if(input[[paste("Update:",LETTERS[i])]] == "BRK"){
mdfr()[i,mdfr()$Approval.Date] <- mdfr()[i,mdfr()$Approval.Date] + 125 + 25
} else if(input[[paste("Update:",LETTERS[i])]] == "None"){
mdfr()[i,mdfr()$Approval.Date] <- mdfr()[i,mdfr()$Approval.Date]
}
}
})
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])))
})
}
shinyApp(ui = ui, server = server)