title: For Loop in Shiny Server: How to Not Overwrite Values with Each ActionButton Press?
body: <p>I am trying to create an app in which part of the UI displays a wordcloud generated by words/strings inputted by the user. To do this, I pass the input to a for loop which is supposed to then store every input in an empty vector with ever press of the action button. However, I am encountering a couple problems, though: one in that no word cloud is displaying, with no error indicated, and another in that the for loop will just overwrite the vector each time the button is pressed, such that it always only has one word in it instead of gradually adding more words. I figured the lack of display is because there is only one word, and it seems like wordcloud needs at least two words to print anything: so how can I get the for loop to work as intended with Shiny?</p>
  
  <pre><code>library(shiny)
  library(stringr)
  library(stringi)
  library(wordcloud2)
  
  
  
  ui &lt;- fluidPage(
  
  titlePanel("Strings Sim"),
  
   sidebarLayout(
   sidebarPanel(
    textInput("string.input", "Create a string:", placeholder = "string &lt;-"),
    actionButton("go1", "GO!")
  ),
  
    mainPanel(
     textOutput("dummy"),
     wordcloud2Output("the.cloud")
    )
  
  
  )
  )
  
  server &lt;- function(input, output, session) {
  
  
   observeEvent(input$go1, {
  
     high.strung &lt;- as.vector(input$string.input)
     empty.words &lt;- NULL
  
  for (i in high.strung) {
    empty.words &lt;- c(empty.words, i)
  }
  
  word.vector &lt;-matrix(empty.words, nrow = length(empty.words),ncol=1)
  
  num.vector &lt;- matrix(sample(1000), nrow=length(empty.words),ncol=1)
  
  prelim &lt;- cbind(word.vector, num.vector)
  
  prelim.data &lt;- as.data.frame(prelim)
  
  prelim.data$V2 &lt;- as.numeric(as.character(prelim.data$V2))
  
   output$the.cloud &lt;- renderWordcloud2(
    wordcloud2(prelim.data)
     )
  
  print(empty.words)
    })
    }
  
       shinyApp(ui=ui,server=server)
  </code></pre>
  
  <p>The operation works as intended when I run it without Shiny code; I basically just use a string in place of the input, run through the for loop a few times to generate the dataframe to be used by word cloud, and get something like the attached picture, which is what I am after:<a href="https://i.stack.imgur.com/XWpiw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XWpiw.png" alt="Example Output"></a></p>
  
  <p>Functional code without Shiny: </p>
  
  <pre><code> empty.words &lt;- NULL
  
   #Rerun below here to populate vector with more words and regenerate wordcloud
  
   high.strung &lt;- as.vector("gumbo")
  
   for (i in high.strung) {
     empty.words &lt;- c(empty.words, i)
     return(empty.words)
   }
  
   word.vector &lt;-matrix(empty.words, nrow = length(empty.words),ncol=1)
  
   num.vector &lt;- matrix(sample(1000), nrow=length(empty.words),ncol=1)
  
   prelim &lt;- cbind(word.vector, num.vector)
  
   prelim.data &lt;- as.data.frame(prelim)
  
   prelim.data$V2 &lt;- as.numeric(as.character(prelim.data$V2))
  
   str(prelim.data)
  
   wordcloud2(prelim.data)
  </code></pre>
  
  <p>Any help is much appreciated!</p>
  
  <p>Edit: More pictures of the desired output using the non-Shiny code. (I editted the dataframe output to overlay the wordcloud just to show the cloud and frame in one picture, i.e. don't need them to display in that way). With each press of the button, the inputted word(s) should be added to the dataframe that builds the cloud, gradually making it larger.The random number vector which determines the size doesn't have to stay the same with each press, but each inputted word should be preserved in a vector. <a href="https://i.stack.imgur.com/3oI8I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3oI8I.png" alt="Ex2"></a> <a href="https://i.stack.imgur.com/DZgPP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DZgPP.png" alt="Ex3"></a></p>