简体   繁体   English

eventReactive,source()并在R Shiny上绘图

[英]eventReactive, source() and plotting on R Shiny

I am having some trouble with creating plots on eventReactive. 我在eventReactive上创建图时遇到了一些麻烦。 I have a source code for inside event reactive, and I am trying to make multiple plots. 我有内部事件反应性的源代码,并且我正在尝试绘制多个图。 I am a little unsure how to make multiple plots, so I tried to make one into a plot. 我不太确定如何绘制多个图,因此我尝试将一个绘制为一个图。 However, I am still having trouble with this. 但是,我仍然对此感到麻烦。

My ui and server are 我的用户界面和服务器是

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)

And my source code is 我的源代码是

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 
}

My actual code is a lot more complicated, so I tried to simplify it to test with this one, and it does not seem happy. 我的实际代码要复杂得多,因此我尝试对其进行简化以进行测试,但似乎并不高兴。 Nothing is running. 什么都没跑。 However, if I try to run it as a regular R code, I am able to get all the results. 但是,如果我尝试将其作为常规R代码运行,则可以获得所有结果。

Please help!!! 请帮忙!!!

The mistake in your code is in your ui where your sliderInput Time has a default value 0 . 代码中的错误是在您的ui ,您的sliderInput Time的默认值为0 This causes the following loop to fail not assigning any value to array abcd : 这将导致以下循环无法不为数组abcd分配任何值:

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

Hence, colSums(abcd[1,,]) dos not have the value due to which it fails. 因此, colSums(abcd[1,,])由于失败而没有值。

If you change the sliderInput("Time", label = h3("Total Number of Months for Future Decision"), min = 0, max = 60, value = 0) to sliderInput("Time", label = h3("Total Number of Months for Future Decision"), min = 0, max = 60, value = 2) your code creates a graph as follows: 如果将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)您的代码创建的图形如下:

在此处输入图片说明

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM