0

I'm trying to create a Shiny app that will allow the user to select some input parameters, perform an analysis based on the inputs, and then output the results of the analysis in the form of a variable number of plots.

The idea is that the user sets the analysis running using an action button, and then if anything interesting is found (depending on certain conditions of the analysis and the input parameters), x number of plots are generated and then displayed on the screen. x is usually somewhere between 0 (nothing interesting found) and 20 (20 interesting results found).

I've had a look here: Shiny: Dynamic Number of Output Elements/Plots, here: Shiny example app with dynamic number of plots, and here: Dynamic number of reactive plots with shiny amongst other places, but I'm very new to Shiny and so I'm really struggling to make it all work. I've had some limited success in plotting variable numbers of images/graphs, but I've had no success in plotting the images in 'real time' as my analysis is running.

The code for the analysis is an R-script which I have written in the past, and I'm trying to adapt it to Shiny. It's fairly long and uses several functions. I've managed to get the script to run within Shiny, but the only thing I can't do is print the results to screen.

Here's my (pseudo)code (with most of the analysis stuff stripped out because it isn't really relevant):

ui.R

shinyUI(fluidPage(
  titlePanel(title=div(img(src="logo.png"))),

  sidebarLayout(
    sidebarPanel(

      h3("Select analysis parameters"),

      # Select date range for analysis
      dateRangeInput("dates", 
                     "Date range",
                     start = "2016-10-01", 
                     end = as.character(Sys.Date())),

      # Specify some variable related to the analysis
      sliderInput("var1", label = h3("Variable 1"),
                  min = 1, max = 20, value = 10),

      # Begin analysis
      actionButton("do","Run Scan")
    ),

    mainPanel(
    # I want my analysis results images to be displayed here. The results are generated using plot() and the number of plots is variable.
    )
  )
))

server.R

# Define server logic
shinyServer(function(input, output, session) {

  observeEvent(input$do, {

    # Start analysis that looks at 10 test cases
    for (i in 1:10) {

    # Perform analysis that fills the 'pattern' dataframe with a variable number of rows... 
    # <code snipped for brevity>

      # Then plot the results of the analysis if anything interesting has been found and save to disk...
      if(nrow(patterns) > 0){

        for(i in 1:nrow(patterns)){
          # Results from each test case save as a different image
          mypath <- file.path("C:/MyFolder/ShinyImage",paste(patterns$name[i],"_", i, ".png", sep = ""))
          png(file=mypath)

          # plot the results *THESE ARE THE IMAGES THAT I WANT TO PRINT TO SCREEN*
          plot(whatever_has_been_found)

          dev.off()
        }
      }
    }
  })
})

Ideally the result plots would pop up in real time as the analysis is running, but an alternative would be for the analysis to complete and then display all of the plots.

Any help on this problem would be very much appreciated! Thank you in advance.

Community
  • 1
  • 1
BigWill
  • 1
  • 2
  • I'd like to point out that one should NOT copy a normal code as-is in one chunk to Shiny server. You need to break it down and understand the concept of reactives. You also need to understand input/output. This will make it easier for you. Then try to check [renderImage & renderPlot](https://shiny.rstudio.com/reference/shiny/latest/plotOutput.html) and see examples of how this is implemented. – OmaymaS Feb 20 '17 at 15:45
  • To be honest with you, I'm completely lost with regards to reactives, inputs, outputs, events, observers etc. I think I'm operating at the limit of my programming ability here. Which is why I turned to Stackoverflow for help! Thanks for the comment though. – BigWill Mar 07 '17 at 14:24

0 Answers0