'map_dfr with a function and paste0

I have a function reviews and i want iterate the function parameters with map_dfr and paste 0 to change the input. so something like

got_reviews <- function(page) {
    
    ...
    
    return(reviews)
}

link <- "blah=?"
all_reviews <- map_dfr(seq(0,80, by = 10), got_reviews(paste0(link,.?.)))

i want the paste0 to loop the sequence but i don't know how to refer to it inside the map_dfr or if this is even possible. i thought of doing it with a for loop but from reading it was suggested to use map or lapply or slappy but i am not sure how to refer to the sequence in any of them



Solution 1:[1]

We can use .x with a lambda call (~)

library(purrr)
map_dfr(seq(0,80, by = 10), ~ got_reviews(paste0(link,.x)))

Also, as paste is vectorized, there is no need to do this in a loop i.e. create the input with paste0, loop over the vector, with map and apply the got_reviews. As there is only a single argument for the function, we don't need to specify the argument

map_dfr(paste0(link, seq(0, 80, by = 10)), got_reviews)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun