Reading same data frame in a loop in shiny


#1

...Hello,
I am new to Shiny. Suppose I have a function which needs to run in a loop. It takes two inputs, one changes, the other is a data frame hat does not change. To be specific, I will write a simplified function and inputs.

vec1 = rnorm(100)
vec2 = rnorm(100)


input = data.frame(vec1,vec2)


trial1 = function(input){
  return(input$vec1)
}


trial2 = function(n,input){
d = rep(0,n)
for(i in 1:n){
b = rnorm(100)
d[i] = b + trial1(input = input)[1] + trial1(input = input)[2]
}
return(mean(d))
}

In this code, the input dataframe is read repeatedly in the loop slowing down the code. Can reactivity in shiny be used so that vec1 is read off from the input only once since input is unchanging in each loop? Will making trial1 reactive do the job(and 'input' coming in as input or another reactive function/value)?