EDIT: After clarification
So, if it is some known (limited) number of times you would like to apply p in succession, this would be one way (I have assumed some simple functions for sp and bp for illustration):
sp <- function(x) return(x+1)
bp <- function(x) return(2*x)
p <- function(x) return(sp(bp(x)))
# Applying p 3 times in succession:
p_old <- 1
for (i in 1:3){
p_new = p(p_old)
p_old = p_new
}
p_new
#> [1] 15
# Which is the same as
p(p(p(1)))
#> [1] 15
Created on 2020-09-11 by the reprex package (v0.3.0)
I am not quite sure what use case you have in mind (because that can easily lead to an infinite "loop", but here is one (admittedly contrived) example of a function which sums some numbers regardless in how many lists they can be nested which is using a call of the same function in the function definition itself:
sum_of_c_or_list <- function(x){
if (!is.list(x)) return(sum(x))
else {
x = unlist(x)
x = sum_of_c_or_list(x)
return(x)
}
}
sum_of_c_or_list(1:3)
#> [1] 6
sum_of_c_or_list(list(1,2,3))
#> [1] 6
sum_of_c_or_list(list(list(1,2,3)))
#> [1] 6
Created on 2020-09-11 by the reprex package (v0.3.0)
forloop?for(i in 1:n){x <- f(x)}