I am using shiny to create app that allows user to enter locations which should go through some functions to pull data and be displayed on a table/graph. How can I adjust my code to either store previous user entries or add them to a list?
library('shiny')
library('shinythemes')
library('xlsx')
library("readxl")
library('xlsx')
library("tidyverse")
library("writexl")
library("covidcast")
library("dplyr")
#basic file loading
today=Sys.Date()
start_date=today - as.difftime(14,units = "days")
county_code=read_excel(choose.files())
#install.packages("covidcast_meta")
#define page layout
ui=fluidPage(
#options(shiny.sanitize.errors=TRUE),
theme=shinytheme("cerulean"),
titlePanel("Covid Infection Rate Tracking App"),
sidebarPanel(
textInput(inputId ="user_state",label ="Please enter a state:eg. WA",),
textInput(inputId ="user_county",label ="Please enter a county:eg. Benton",),
submitButton("Update View", icon("refresh")),
downloadButton("downloadPlot","Download Plot"),
downloadButton("downloadData","Download Table"),
),
mainPanel(
tabsetPanel(
tabPanel(title = "Infection_Rate_Plot",plotOutput("line")),
tabPanel(title="Data_Table", dataTableOutput("table"))
),
#plotOutput("line"),
#dataTableOutput("table")
),
hr(),
print("The data displayed on this application is sourced from the Covidcast API.Infection rates cannot be displayed for a current day or for any locations that are not a part of the United States.")
)
#create output functions and insert source code
server <- function(input, output, session) {
#####
#####
#### reactive is used to create a df from user entry which can be displayed.
######this serves as a function to run the background code and save it to a df
#df_state=c("WA","WA")
#df_county=c("Benton","Franklin")
df=reactive({
#####test
df_state=c("WA","WA")
df_county=c("Benton","Franklin")
df_state=append(df_state,input$user_state)
df_county=append(df_county,input$user_county)
######
l=length(df_county)#input$user_county)
user_df=data.frame(matrix(ncol = 4, nrow = l))
counter=1
for (j in df_county){
user=filter(county_code,State== df_state[counter] & Name== j)
user_df[counter,]=user
counter=counter+1
}
geo_code=sprintf("%05d",user_df$X2)
user_df$X2=geo_code
#data request
Covidcast_data= suppressMessages(covidcast_signal(data_source = "jhu-csse",
signal = "confirmed_7dav_incidence_prop",start_day =start_date,
end_day = today,geo_type ="county",geo_values = geo_code))
#data cleaning
Test3 <-Covidcast_data%>%group_by(geo_value)%>% arrange(geo_value, time_value)
Test3 <-Test3 %>% select(one_of("geo_value","time_value","value"))
Test3$ID=user_df$X1[match(Test3$geo_value,user_df$X2)]
Test3
})
#using df() we display a reactive plot based on user input which was proccessed ABOVE
my_plot=reactive({
x=ggplot(df(),aes(x = time_value,y = value,color = ID))+
geom_line()+
geom_point()
x + ggtitle("Plot of Covid Cases by Selected County per 100,000 people") +
xlab("Time(days)") +
ylab("Confirmed case per 100000") +theme_bw() + scale_x_date(date_breaks = "3 day")+
theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust=2))
})
output$line=renderPlot({
my_plot()
})
#####using df() we display a table based on user input proccessed above
output$table=renderDataTable({
df()
})
#######Download table
output$downloadData <- downloadHandler(
filename = function() {
paste("Covid_Plot-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(df(), file)
}
)
#######Download Plot
output$downloadPlot <- downloadHandler(
filename = function() {
paste("Infection_Rate_Graph-", Sys.Date(), ".png", sep="")
},
content = function(file) {
png(file=file)
plot(my_plot())
dev.off()
}
)
}
shinyApp(ui=ui, server=server)