0

I am having some trouble with creating plots on 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.

Please help!!!

Florian
  • 24,425
  • 4
  • 49
  • 80
IKumar
  • 35
  • 2
  • 6

1 Answers1

0

The mistake in your code is in your ui where your sliderInput Time has a default value 0. This causes the following loop to fail not assigning any value to array 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.

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:

enter image description here

Hope it helps!

Community
  • 1
  • 1
SBista
  • 7,479
  • 1
  • 27
  • 58
  • Hi, thanks for this information. I was able to get this plot, but I am unable to get the image from the following code 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() Do you know how to fix that part?? – IKumar Jan 16 '18 at 20:37
  • The image is saved in the temporary folder. Can you check that? – SBista Jan 17 '18 at 06:15