Stack Exchange
Append sequentially within while loop for formula
I want to append sequentially my formula within a while loop so that after each iteration I get the previous value appended to the new value, for example:
i = 0
predictor = "test" 
while(i < 7){
  i = i+1
  predictor <- reformulate(c(predictor, glue('I({predictor}^{i})')), response='go')
  print(predictor) # Now we print predictor
  
}

[1] go ~ test + I(test^1)  
>Error in reformulate(c(predictor, glue("I({predictor}^{i})")), response = "go") : 
  'termlabels' must be a character vector of length at least one


Expected output:
[1] go ~ test + I(test^1)  + I(test^2) 
[1] go ~ test + I(test^1)  + I(test^2) + I(test^3) 
[1] go ~ test + I(test^1)  + I(test^2) + I(test^3) + I(test^4) +  
[1] go ~ test + I(test^1)  + I(test^2) + I(test^3) + I(test^4) + I(test^5)  
[1] go ~ test + I(test^1)  + I(test^2) + I(test^3) + I(test^4) + I(test^5) + I(test^6)  
[1] go ~ test + I(test^1)  + I(test^2) + I(test^3) + I(test^4) + I(test^5) + I(test^6) + I(test^7)  

Here is the working code that I used with the help of @onyambu
:
poly66 <- function(data, response, predictor){
  for(i in 1:3){
    test <- reformulate(glue('poly({predictor},{i}, raw=TRUE)'), response=response)
    lmod <- lm(test, data)
    print(lmod)
  }
}

poly66(savings, 'sr', 'pop15')
>Call:
lm(formula = test, data = data)

Coefficients:
               (Intercept)  poly(pop15, 1, raw = TRUE)  
                    17.497                      -0.223  


Call:
lm(formula = test, data = data)

Coefficients:
                (Intercept)  poly(pop15, 2, raw = TRUE)1  poly(pop15, 2, raw = TRUE)2  
                  22.399363                    -0.522473                     0.004268  


Call:
lm(formula = test, data = data)

Coefficients:
                (Intercept)  poly(pop15, 3, raw = TRUE)1  poly(pop15, 3, raw = TRUE)2  poly(pop15, 3, raw = TRUE)3  
                 -88.696453                     9.808536                    -0.305374                     0.002995  


change your form1 to be predictor <- c(predictor, i)

https://www.geeksforgeeks.org/pattern-drawing-in-r/

Where are you using the formula at? instead you can do go~poly(test, i, raw = TRUE)

Since you start with predictor, you need to modify it rather than creating a new variable. Hence
i = 0
predictor = "test" 
while(i < 7){
  i = i+1
  predictor <- c(predictor, i)
  print(predictor) # Now we print predictor
  
}

Edit:
Depending on what exactly you are working on, you might find easier methods. Eg if you are fitting models, then you could simply use poly with raw = TRUE to get the desired results. ie:
test <- 1:4
poly(test,1,raw = TRUE)
     1
[1,] 1
[2,] 2
[3,] 3
[4,] 4
attr(,"degree")
[1] 1
attr(,"class")
[1] "poly"   "matrix"

poly(test,2,raw = TRUE)
     1  2
[1,] 1  1
[2,] 2  4
[3,] 3  9
[4,] 4 16
attr(,"degree")
[1] 1 2
attr(,"class")
[1] "poly"   "matrix"

poly(test, 3, raw = TRUE)
     1  2  3
[1,] 1  1  1
[2,] 2  4  8
[3,] 3  9 27
[4,] 4 16 64
attr(,"degree")
[1] 1 2 3
attr(,"class")
[1] "poly"   "matrix"

Notice how the degrees were added to you without physically creating the I(test^2), I(test^3) variables.
In this case you will only change the degree.
If you are only interested in the formula, then consider using update:
form <- go~test
i <-1
while(i < 7){
  i = i+1
  form <- update(form, sprintf('.~. + I(test^%d)',i))
  print(form)
}
go ~ test + I(test^2)
go ~ test + I(test^2) + I(test^3)
go ~ test + I(test^2) + I(test^3) + I(test^4)
go ~ test + I(test^2) + I(test^3) + I(test^4) + I(test^5)
go ~ test + I(test^2) + I(test^3) + I(test^4) + I(test^5) + I(test^6)
go ~ test + I(test^2) + I(test^3) + I(test^4) + I(test^5) + I(test^6) + 
    I(test^7)

NOTE: --I am quite convinced you are fitting a model hence USE poly:

I thought I made my example simple enough to reproduce the issue but it seems that there's something else involved.I will update the post

@Emil11 why do you want to do that to a formula? Which then you would have to evaluate? use poly/update

The difference is that poly uses orthogonal polynomials, so this generates a different output as opposed to directly indexing with I(). My current purpose requires the latter, but thank you for showing how to do it!

@Emil11 i clearly indicated that you use raw = TRUE which will be exactly as I() Check my output

Ah brilliant! Sorry, I have had a long day so my mind is a little slow. However, I managed to correctly implement poly within the function and it works far better and less lines of code!
