I am creating an app in Shiny. One of the things I need it to do is display a boxplot that summarizes a data frame that is subsetted based on inputs from the user. Here is some of the code I use to subset the data frame here: Due to NDA I cannot provide the data source.
newone <- subset(mydata, mydata$ShippingCondition==input$mode)
if (input$mode=="Truck"){
if (input$zipwhse == 0){
newtwo<- subset(newone, newone$totalmiles>=(inmiles-100) & newone$totalmiles<=(inmiles+100) & newone$Qty_KG>=(input$qty-2000) & newone$Qty_KG<=(input$qty+2000) & newone$manufacturingzip == newone$shipfromzip)
newthree<-subset(newtwo, newtwo$BUGroup==new_bugroup)
if (nrow(newthree)< 10) {
plotdata<-newtwo
textout<-"Truck Shipments that are not warehoused. They traveled a distance within +/- 100 miles, and weigh within +/- 2,000 kg of the current shipment."
} else {
newfour<-subset(newthree, newthree$ContainerReq==input&containerreq)
if (nrow(newfour)< 10) {
plotdata<-newthree
textout<-sprintf("Truck shipments of %s that are not warehoused. They traveled a distance within +/- 100 miles, and weigh within +/- 2,000 kg of the current shipment.", input$bugroup)
}
else {
plotdata<-newfour
textout<-sprintf("Truck shipments of %s that are not warehoused. They traveled a distance within +/- 100 miles, and weigh within +/- 2,000 kg of the current shipment, as well as have the same container requirements.", input$containerreq)}
}
}
...etc.
The final data frame that is plotted is called plotdata, and the associated text is called textout.
The thing is, I also need to use plotdata for another table, and it would make the code a lot neater and easier to read if I could make the subset code above a reactive function. However, I can't make the entire block of code reactive as it is, because there is no one value to return. If I try to use this block of code in a reactive function, then call it later as function(), I get the "object of type 'closure' is not subsettable" error. Is there some way I can make this reactive code return plotdata in a way that I can use it outside of a single renderPlot?
I apologize that this isn't reproducable, but if you can just tell me theoretically how/if I can do it, that would be much appreciated.