title: For loop inside output in Shiny
body: <p>I have found similar question here: <a href="https://stackoverflow.com/questions/32366432/how-to-make-for-loop-reactive-in-shiny-server-in-r">How to make for loop reactive in shiny server in R?</a>, but it is not answered correctly.</p>
  
  <p>I am using R, version 3.3.1.In shiny. I am trying to make a loop in shiny. Here is my shortened code version:</p>
  
  <pre><code>library(shiny)
  library(dplyr)
  library(data.table)
  library(dtplyr)
  library(stringr)
  library(jsonlite)
  library(httr)
  library(mongolite)
  library(RCurl)
  library(XML)
  
  f1 &lt;- function(lst) lapply(lst, function(x) if (is.list(x)) f1(x) else if (is.null(x)) NA_character_ else x)
  
  ui &lt;- fluidPage(
    titlePanel(h1("FORENSIS")),
  
    sidebarLayout(
  
      sidebarPanel(h4("Upute za korištenje:"),
                   p("Podaci se prikupljaju iz javnih registara"),
                   br(),
                   br(),
                   em("Ukliko imate pitanja, slobodno nas kontaktirajte:")
      ), 
  
      mainPanel(h3("Upit"),
                textInput(inputId = "oib", label = "OIB"),
                actionButton("kreiraj", "Pretraži"),
                br(),
                br(),
                htmlOutput(outputId = "oib_output"),
                h4("STATUS OIB-A"),
                htmlOutput(outputId = "oib_status"),
                br(),
                h4("OSNOVNI PODACI"),
                htmlOutput(outputId = "oib_ime"),
                htmlOutput(outputId = "oib_prezime"),
                htmlOutput(outputId = "oib_spol"),
                htmlOutput(outputId = "oib_dob"),
                htmlOutput(outputId = "oib_adresa"),
                htmlOutput(outputId = "oib_mjesto"),
                htmlOutput(outputId = "oib_naselje"),
                htmlOutput(outputId = "oib_zip"),
                htmlOutput(outputId = "oib_zupanija"),
                br(),
                h4("PRAVNE FUNKCIJE U POSLOVNIM SUBJEKTIMA"),
                htmlOutput(outputId = "oib_funkcija_funkcija")
      )
    )
  )
  
  server &lt;- function(input, output) {
  
    report_exe &lt;- eventReactive(input$kreiraj, {
      input$oib
    })
  
    output$oib_output &lt;- renderUI({
      HTML(paste0('&lt;h3&gt;', 'Upit za OIB: ', report_exe(), '&lt;/h3&gt;'))
    })
    output$oib_status &lt;- renderUI({
      req &lt;- list()
      oib_status &lt;- NULL
      i &lt;- 0
      for (i in 1:length(report_exe())) {
        reqOP &lt;- rbind(fromJSON(toJSON(content(GET(url = "https://api.data-api.io/v1/oibstatus/",
                                                   add_headers('x-dataapi-key' = "xxxx"),
                                                   query = list(oib = report_exe())), type = "application/json"), null = "null"), flatten = TRUE))
        req[[i]] &lt;- reqOP
      }
      json &lt;- do.call(rbind, req)
      json &lt;- as.data.frame(json)
  
      oib_status &lt;- as.data.frame(lapply(f1(json), unlist), stringsAsFactors=FALSE)
  
      HTML(paste0('&lt;h4&gt;', 'Status: ', ifelse(oib_status$X_status[1] == 1, 'Aktivan', 'Neaktivan'), '&lt;/h4&gt;'))
    })
  
    preb &lt;- reactive({
      req &lt;- list()
      my_get &lt;- for (i in 1:length(report_exe())) {
        reqOP &lt;- rbind(fromJSON(toJSON(content(GET(url = "https://api.data-api.io/v1/prebivaliste/",
                                                   add_headers('x-dataapi-key' = "xxxx"),
                                                   query = list(oib = report_exe())), type = "application/json"), null = "null"), flatten = TRUE))
        req[[i]] &lt;- reqOP
      }
      json &lt;- do.call(rbind, req)
      json &lt;- as.data.frame(json)
  
      prebivaliste &lt;- as.data.frame(lapply(f1(json), unlist), stringsAsFactors=FALSE)
      return(prebivaliste)
    })
  
    funkcije &lt;- reactive({
      req &lt;- list()
      my_get &lt;- for (i in 1:length(report_exe())) {
        reqOP &lt;- rbind(fromJSON(toJSON(content(GET(url = "https://api.data-api.io/v1/osobe/",
                                                   add_headers('x-dataapi-key' = "xxxxx"),
                                                   query = list(oib = report_exe())), type = "application/json"), null = "null"), flatten = TRUE))
        req[[i]] &lt;- reqOP
      }
      json &lt;- do.call(rbind, req)
      json &lt;- as.data.frame(json)
      povezani_subjekti &lt;- json$povezaniSubjekti
      json$povezaniSubjekti &lt;- NULL
  
      funkcije &lt;- as.data.frame(lapply(f1(json), unlist), stringsAsFactors=FALSE)
      funkcije &lt;- funkcije[!duplicated(funkcije),]
  
      oibreq_subjekti &lt;- unique(funkcije$subjektOib)
      req &lt;- list()
      if (is.null(oibreq_subjekti)) {
        funkcije &lt;- NULL
      } else {
        my_get &lt;- for (i in 1:length(oibreq_subjekti)) {
          reqOP &lt;- rbind(fromJSON(toJSON(content(GET(url = "https://api.data-api.io/v1/subjekti/",
                                                     add_headers('x-dataapi-key' = "xxxxxx"),
                                                     query = list(oib = oibreq_subjekti[i])), type = "application/json"), null = "null"), flatten = TRUE))
          req[[i]] &lt;- reqOP
        }
        json &lt;- do.call(rbind, req)
        json &lt;- as.data.frame(json)
  
        subjekti &lt;- as.data.frame(lapply(f1(json), unlist), stringsAsFactors=FALSE)
        subjekti$isActive &lt;- NULL
        colnames(subjekti)[which(colnames(subjekti)=="adresa")] &lt;- "adresa_subjekta"
        funkcije &lt;- merge(x = funkcije, y = subjekti, by.x = "subjektOib", by.y = "oib", all.x = TRUE, all.y=FALSE)
  
        return(funkcije)
      }
    })
  
    output$oib_ime &lt;- renderUI({
      HTML(paste0('&lt;h4&gt;', 'Ime: ', preb()$ime, '&lt;/h4&gt;'))
    })
  
    output$oib_prezime &lt;- renderUI({
      HTML(paste0('&lt;h4&gt;', 'Prezime: ', preb()$prezime, '&lt;/h4&gt;'))
    })
  
    output$oib_adresa &lt;- renderUI({
      HTML(paste0('&lt;h4&gt;', 'Adresa: ', preb()$adresa, '&lt;/h4&gt;'))
    })
  
    output$oib_mjesto &lt;- renderUI({
      HTML(paste0('&lt;h4&gt;', 'Mjesto: ', preb()$mjesto, '&lt;/h4&gt;'))
    })
  
    output$oib_naselje &lt;- renderUI({
      HTML(paste0('&lt;h4&gt;', 'Naselje: ', preb()$naselje, '&lt;/h4&gt;'))
    })
  
    output$oib_naselje &lt;- renderUI({
      HTML(paste0('&lt;h4&gt;', 'Poštanski broj: ', preb()$posta, '&lt;/h4&gt;'))
    })
  
    output$oib_zupanija &lt;- renderUI({
      HTML(paste0('&lt;h4&gt;', 'Županija: ', preb()$zupanija, '&lt;/h4&gt;'))
    })
  
  
    output$oib_funkcija_funkcija &lt;- renderUI({
    for (j in 1:2) {
  
    HTML(paste0('&lt;h4&gt;', 'Funkcija: ', funkcije()$funkcija[j], '&lt;/h4&gt;',
                '&lt;h4&gt;', 'Naziv tvrtke: ', funkcije()$naziv[j], '&lt;/h4&gt;'))
    }
    })
  
  
  
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>It is a big chunk of code so I would like to simplify. I have one text input argument <code>textInput(inputId = "oib", label = "OIB")</code>. In this argument someone has to type some id number. Then, in the reactive part of the code, this input is used to retrieve data from REST API (in the end this reactive object i s simple data frame). I can successfully add reactive object to output, if there is only one row. But if I want to use the for loop inside output, it doesn't give me an answer:</p>
  
  <pre><code>  output$oib_funkcija_funkcija &lt;- renderUI({
    for (j in 1:2) {
  
    HTML(paste0('&lt;h4&gt;', 'Funkcija: ', funkcije()$funkcija[j], '&lt;/h4&gt;',
                '&lt;h4&gt;', 'Naziv tvrtke: ', funkcije()$naziv[j], '&lt;/h4&gt;'))
    }
    })
  </code></pre>