4

I have an R/Shiny app that responds to a selectInput() by doing an expensive operation (fetch data from a DB, compute, produce chart etc).

I would like R to 'cancel' any operations in progress if I choose another element and simply react to the latest choice.

Instead what I see is that it queues up all choices I made and forces me to wait until it executes thru -- for example if i up-arrow thru the combo box.

Here's some sample code -- `

    baseStockData <- reactive({
        # Parse out the first part of the ticker descriptor to get the tick
        print(input$toolDetrendBase)
        print(">>>> in baseStockData")
        if (input$stockDetrend)
        {
            ticker.firstpart <- strsplit(input$toolDetrendBase, " ")[[1]][1]
            ticker <- str_replace_all(ticker.firstpart, "[^[:alnum:]]", "")
            # parse ticker till first space

            print(paste('loading base stock data for', ticker))
            db.con <- xdbOpen()
            cur.proc.time <- system.time(

                stock <- xdbReadStockEvents(db.con,ticker)
                )
            print(paste('load stock perf :'))
            print(cur.proc.time)

            xdbClose(db.con)
            stock
        }
        else
        {
            NA
        }
        }
    )


   output$stockViewPanelAdj <- 
        renderPlot({
            print(">>> in stockViewPanelAdj")
            stock <- currentStockData()
            daterange <- paste(input$stockViewDateStart, '::', input$stockViewDateEnd, sep='')
            print(paste('updating view panel for stock data', daterange))

            if (input$stockDetrend)
            {
                print('calculating detrended stock series view')
                baseStock <- baseStockData()
                calcDetrendStockSeries(stock, baseStock, daterange)
            }
            else
            {

                if (input$stockNormalize)
                {
                    baseval <- 100.0 / as.numeric(stock$adj[daterange][1,4])
                    if (!is.numeric(baseval)) {
                        baseval <- 1.0
                    }
                    print(baseval)
                    stockvals <- stock$adj[daterange] * baseval
                    yrange <- c(80,120)
                    theme <- chartTheme('white')
                }
                else
                {
                    stockvals <- stock$adj[daterange]
                    baseval <- 1.0
                    yrange <- NULL
                    theme <- chartTheme('black')
                }
                render.time <- system.time(
                chartSeries(stockvals,
                            name = stock$stock.metadata$Name,
                            minor.ticks = T,
                            major.ticks =T,
                            theme = theme,
                            yrange = yrange,
                            TA=c(addBBands())))

                print("Render perf")
                print(render.time)
                if(input$stockNormalize) {
                    abline(h=100, lwd=2, col='green')
                }
            }
        }
nxstock-trader
  • 223
  • 2
  • 6
  • See http://stackoverflow.com/questions/30587883/is-it-possible-to-stop-executing-of-r-code-inside-shiny-without-stopping-the-sh and http://stackoverflow.com/questions/31385861/shiny-i-dont-know-how-stop-one-process-started-by-a-button-by-pressing-another#comment50777131_31385861 -- doesn't seem possible in R – DeanAttali Aug 03 '15 at 02:49
  • 1
    You could bind the evaluation to a button, so that the expensive part would be computed only on explicit demand. – Roman Luštrik Aug 03 '15 at 06:49
  • Your options are an explicit button, or something along the lines of my answer to [this question](http://stackoverflow.com/questions/31051133/how-do-i-make-sure-that-a-shiny-reactive-plot-only-changes-once-all-other-reacti) where you have a timed delay before the reactive events fire. – Nick Kennedy Aug 03 '15 at 07:21
  • Fetching the data and brining it back might take a while if you are doing it everytime. If possible fetch it once for all the products and store it either in a .rda or in memory if you're using the whole set and then just listen to deltas (updates) on the DB side. – Pork Chop Aug 04 '15 at 11:35

0 Answers0