0

我在 eventReactive 上创建图时遇到了一些麻烦。我有一个内部事件反应的源代码,我正在尝试制作多个图。我有点不确定如何制作多个情节,所以我尝试将一个情节制作成一个情节。但是,我仍然遇到这个问题。

我的用户界面和服务器是

library(shiny)
library(lpSolve)

ui <- fluidPage(
  mainPanel(
    tabsetPanel(
      tabPanel("Information required for the model",
               sliderInput("Reservoirs", label = h3("Total Number of Reservoirs"), 
                           min = 1, max = 25, 
                           value = 10),
               sliderInput("Municipalities", label = h3("Total Number of Municipalities Served by the Reservoirs"), 
                           min = 1, max = 150, 
                           value = 15),
               sliderInput("Time", label = h3("Total Number of Months for Future Decision"), 
                           min = 0, max = 60, 
                           value = 0)
      ),
    tabPanel("Summary of csv files",
             actionButton("Run_Model", "Run Model")),
    tabPanel("Results", 
             plotOutput("plot_ipsita"),
             img(outfile)
             )

)))



server <- function(input, output) {


  running_code<-eventReactive(input$Run_Model, {
    source("Source_code.R", local=TRUE)
    outfile <- tempfile(fileext = '.png')
    png(outfile,width=30,height=nR*3,units = "in",res=200)
    par(mfrow=c(ceiling(nR)/2, 2))
    for (i in 1:nR){
      hist(abcd[i,1,1])
    }
    dev.off()
    plot((colSums(abcd[1,,])),type="l",ylab="Withdrawal [mio m3]",xlab = "months",col=1,lwd=3,lty=1)
    abline(h=130, col = 2,lwd=3,lty=3)
    abline(h=205, col=3, lwd=3,lty=4)
    legend("topleft", c("","All Reservoirs","Import","Failure"), col = c(0,1,2,3),pt.cex=0.5,lty=1:4,lwd=3, cex=0.75,bty="n")
    title(paste0("Withdrawals from reservoirs and imports and failure for  % initial storage"  ), cex.main=1)


  })

  output$plot_ipsita <- renderPlot({
    running_code()
  })



}

shinyApp(ui = ui, server = server)

我的源代码是

nR<-input$Reservoirs
nM<-input$Municipalities
nT<-input$Time

abcd<-array(data=0, c(nR,nM,nT))
for (i in 1:nR){
 abcd[i,,]<-(1+i)*55 
}

我的实际代码要复杂得多,所以我试图简化它以用这个进行测试,它似乎并不高兴。什么都没有运行。但是,如果我尝试将它作为常规 R 代码运行,我可以获得所有结果。

请帮忙!!!

4

1 回答 1

0

您的代码中的错误在于您uisliderInput Time默认值0。这会导致以下循环无法为 array 分配任何值abcd

for (i in 1:nR){
 abcd[i,,]<-(1+i)*55 
}

因此,colSums(abcd[1,,])不具有导致失败的价值。

如果您将 更改sliderInput("Time", label = h3("Total Number of Months for Future Decision"), min = 0, max = 60, value = 0)sliderInput("Time", label = h3("Total Number of Months for Future Decision"), min = 0, max = 60, value = 2)您的代码,则会创建如下图表:

在此处输入图像描述

希望能帮助到你!

于 2018-01-16T05:18:57.497 回答