2

I am new to reactive part of shiny. I have tried writing a code where there is a big reactive function and inside that I have another reactive function which changes on basis of a slider input. Is it possible to write one reactive inside other, because if it is my code doesn't run? Is it a good practice to write a reactive inside another reactive?

Here is my code:

dt <- reactive({
    con = dbConnect(MySQL(), user='root', password='root', db='simulator', host='localhost')
    size <- reactive({dbGetQuery( con, statement = paste(" Select count(*) from event where event_type = 'pe_send_flit' and run_id = ",input$run))})
    j = (size()$`count(*)`) - 1
    a <- j
    b <- j
    for(i in 0:j)
    {
      a[i] <- dbGetQuery(con, statement = paste(" Select event_time from event where event_type = 'pe_receive_flit' and run_id = ",input$run," and event_data =", i,"order by event_data asc"))
      b[i] <- dbGetQuery(con, statement = paste(" Select event_time from event where event_type = 'pe_send_flit' and run_id = ",input$run," and event_data =",i,"order by event_data asc"))
    }
    receive <- as.vector(a,mode='numeric')
    send <- as.vector(b,mode='numeric')
    difference = receive - send
    len = length(difference)
    avg = 0.0
    sum_f = 0
    counter = 1
    xrange <- vector(mode = "numeric", length = len)
    yrange <- vector(mode = "numeric", length = len)
    for (k in 1:len)
    {
      temp = receive[k]
      sum_f = difference[k]
      l = k + 1
      for(m in l:len)
      {
        if(isTRUE(all.equal(temp,receive[m])))
        {
          sum_f = sum_f + difference[m]
          counter = counter + 1
          receive[m] = 0
          difference[m] = 0
        }
      }
      avg = sum_f/counter
      xrange[k] = receive[k]
      yrange[k] = avg
    }
    xrange <- xrange[xrange!=0]
    yrange <- yrange[yrange!=0.000]
    xrange <- xrange[!is.na(xrange)]
    yrange <- yrange[!is.na(yrange)]
    array_length = length(xrange)
    ind <- sort(xrange,index.return = TRUE)$ix
    xrange <- xrange[ind]
    yrange <- yrange[ind]
    myfunction <- function(pa,val,x)
    {
      y = x - 1
      tmp = pa*y
      sm = tmp + val
      incr = y + 1
      result = sm/incr
      return(result)
    }
    incremental_average <- vector(mode = "numeric",length = array_length)
    pa = as.integer(0)
    for(r in 1:array_length)
    {
      pa = myfunction(pa,yrange[r],r)
      incremental_average[r] = pa
    }
    dbDisconnect(con)
    df <- data.frame(xrange,incremental_average)
    dat <- reactive({
      if(!is.null(input$range))
      {
        test <- df[df$xrange %in% seq(from=input$range[1],to=input$range[2]),]
        test
      }
    })

    df
  })

How do I use the 'dat' reactive part?

3
  • 3
    Personally I would use one reactive function to load the data (e.g. load_data) and then another reactive function to filter the data (e.g. filter_data). load_data() is called inside filter_data (e.g. dt <- load_data()) and then data is filtered based on inputs. In the functions where you need the filtered data (e.g. output plots/tables) you then call dt <- filter_data() before plotting etc. Commented Apr 20, 2017 at 9:22
  • @KristofferWintherBalling Can u explain it in more detail with my example? Do i have to write filter_data() and inside that load_data()? Commented Apr 20, 2017 at 13:34
  • Not inside, beside. You have two inputs here input$run and input$range. You want the load_data() reactive to react when you change the input$run input and filter_data() to run when you change the input$range input. Your current function would, I think, run every time either input is updated. Commented Apr 20, 2017 at 20:33

1 Answer 1

1

Pull this bit out and rewrite as

dat <- reactive({
    if(!is.null(input$range)) {
        test <- dt()[dt()$xrange %in% seq(from=input$range[1],to=input$range[2]),]
        test
    }
})

Now your dt() function returns the data (effectively just loading it) but your dat() function returns the filtered data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.