I have created an app that takes user inputs and returns values based on a search. I know that the user inputs are being saved since the print function returns the user inputs as an output in a table. For some reason when I try to push the user inputs into a reactive to start my data manipulation I keep getting an error($<-.data.frame: replacement has 0 rows, data has 1) but I can't figure out if my issue is in the first few lines of the reactive.
R code below:
''''
today=Sys.Date()
start_date=today - as.difftime(14,units = "days")
county_code=read_excel(choose.files())
ui<-fluidPage(
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",),
actionButton("add","Add location"),
actionButton("Reset","Reset App")
),
mainPanel(
tableOutput("summary"),
plotOutput("line")
#tableOutput(outputId = "summary")
)
)
server <- function(input, output, session){
ing_df <- shiny::reactiveValues()
base_state=c("WA","WA")
base_county=c("Benton","Franklin")
ing_df$test<-data.frame("State"=character(),
"County"=character())
df_capture<- eventReactive(input$add,{
ing_df$test[nrow(ing_df$test)+1,] <- c(input$user_state,input$user_county)
ing_df$test
})
#############################
df_2=reactive({
user_df=ing_df$test
l=nrow(ing_df$test)#length(memory$dat2)#input$user_county)
user_df1=data.frame(matrix(ncol=4,nrow = l))
for (j in 1:nrow(ing_df$test) ){
user_df1[j,]=filter(county_code,State== user_df$State[j] & Name==user_df$County[j] )
}
geo_code=sprintf("%05d",user_df$X2)
user_df1$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)]
})
#using df() we display a reactive plot based on user input which was proccessed ABOVE
my_plot=reactive({
x=ggplot(df_2(),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()
})
#####################
output$summary<-renderTable(df_capture()
)
}
shinyApp(ui=ui, server=server)