【发布时间】:2015-03-04 17:35:15
【问题描述】:
我正在 Shiny 中创建一个应用程序。我需要它做的一件事是显示一个箱线图,该箱线图总结了一个基于用户输入的子集数据框。这是我在这里用来对数据框进行子集化的一些代码:由于 NDA,我无法提供数据源。
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)}
}
}
...等等。
最终绘制的数据框称为plotdata,关联的文本称为textout。
问题是,我还需要为另一个表使用 plotdata,如果我可以在反应函数之上制作子集代码,它将使代码更整洁、更易于阅读。但是,我不能使整个代码块保持原样,因为没有一个值可以返回。如果我尝试在反应函数中使用此代码块,然后稍后将其调用为 function(),我会收到“'closure' 类型的对象不是子集”错误。有什么方法可以让这个反应式代码返回 plotdata,以便我可以在单个 renderPlot 之外使用它?
抱歉,这是不可重现的,但如果您能从理论上告诉我如何/如果我能做到,那将不胜感激。
【问题讨论】:
标签: r function shiny conditional-statements