Suppose we have the following function
library("jsonlite")
library("dplyr")
test_function <- function(year) {
link <- sprintf(paste0('https://api.hello/', year, '/1.json'))
data_frame <- fromJSON(link) %>%
pluck(2) %>%
as_tibble
return(data_frame)
}
Essentially you can enter a year into the function and it returns a tibble for that year from month 1 (i.e. the '/1'
bit).
How can the function be looped so that it first returns a tibble e.g. from year 2019, month 1 (represented by /1.json
), then returns a tibble from month 2 (represented by /2.json
), etc., up to month 12?
Essentially the number part of the link needs to increase by 1 until 12, so that the function returns a data frame with data from all 12 months.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…