title: R Shiny assign reactive values to a pasted object
body: <p>I'm building a Shiny application which is populated by several repetitive inputs and outputs. Rather than copy and paste code over and over, I have followed <a href="https://shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html" rel="nofollow noreferrer">this example on the Shiny website</a> to generate inputs and outputs with <code>lapply()</code>. This works well up to a point, but I want to predict based on user inputs, and then store these predictions as reactive objects to be called by numerous outputs (e.g. plotting, printing and combining predictions). Here the assignment of a reactive object in an <code>lapply()</code> function seems problematic. I have a feeling that the <code>assign()</code> function doesn't like <code>reactive()</code> objects!</p>
  
  <p>I have written a simple working example of my problem below using <code>mtcars</code>. This code works fine, but I would like to drop the explicit assignment of <code>pred1</code> and <code>pred2</code> and replace with an <code>lapply()</code> function. I'm aware that in this simple example it would be easier to do the prediction inside the <code>output$out</code> object, but in my actual application I need to call the predicted object into numerous outputs. Any help would be greatly appreciated.</p>
  
  <pre><code>library(shiny)
  ui = fluidPage(
        # slider input for horse power to predict with...
        column(3,lapply(1:2, function(i) {
          sliderInput(paste0('hp', i), paste0('Select HP', i), min = 0, max = 300, value = 50)
        })
      ),
  
        # output display
        column(3,lapply(1:2, function(i) {
          uiOutput(paste0('out', i))
        })
      )
  
  )
  
  server = function(input, output, session) {
  
   # # I can work pred out separately like this...
   # pred1 &lt;- reactive({
   #     predict(lm(mpg ~ hp, data = mtcars),
   #             newdata = data.frame(hp = input$hp1), se.fit = TRUE)
   #
   # })
   #
   # pred2 &lt;- reactive({
   #   predict(lm(mpg ~ hp, data = mtcars),
   #           newdata = data.frame(hp = input$hp2), se.fit = TRUE)
   #
   #})
  
    # but I want to create pred1 and pred2 in one go...something like this:
    lapply(1:2, function(i){
       assign(paste0("pred",i), reactive({
         predict(lm(mpg ~ hp, data = mtcars),
                 newdata = data.frame(hp = input[[paste0("hp",i)]]), se.fit = TRUE)
         }))
     })
  
    # output
    lapply(1:2, function(i){
      output[[paste0("out",i)]] &lt;- renderText({
        paste0("MPG with HP",i," = ", round(get(paste0("pred",i))()$fit,0), " (", 
                     round(get(paste0("pred",i))()$fit - 1.96 * get(paste0("pred",i))()$se.fit,0), ", ",
                     round(get(paste0("pred",i))()$fit + 1.96 * get(paste0("pred",i))()$se.fit,0), ")")
  
        })
        })
  
  }
  
  
  # Compile
  shinyApp(
    ui = ui,
    server = server
  )
  </code></pre>