I would like ideias to iterate rows from tibble
parameter into a user function
.
I create a loop for
, but I would like to use some functions of Tidyverse
, like purrr
or dplyr
.
The data set with Keys of API is like this:
keys.bots <- tibble(bot = c('bot1', 'bot2', 'bot3', 'bot4', 'bot5'),
key = c('Key bot1','Key bot2','Key bot 3','Key bit 4','Key bot 5'))
Where each key should be iterated with some function with purr
or dplyr
, to iterate each key into the second parameter, keys.bots[]
, in the function.
The function is:
fun.get_data_blip <- function(body, key, tzone){
# Fetch Data from API
request <- POST( url = url
,body = body
,add_headers(.headers = c( 'Authorization' = key
,'Content-Type' = 'application/json')))
# Extracting Request Content
content <- content(request, as = 'parsed')
# Converting the List in Content to a Data Frame
content$resource$items %>%
toJSON() %>%
fromJSON() %>%
mutate( lastMessage.date_alt = ymd_hms(lastMessage$date, tz = tzone.sao_paulo)
,lastMessage.direction_alt = if_else(lastMessage$direction == 'sent', 'Bot', 'Usuário')
,'#' = row_number()) %>%
flatten(recursive = T) %>%
as_tibble()
The function in action:
tbl.chatbots <- fun.get_data_blip(body.threads, as.character(keys.bots[]), tzone.sao_paulo)
So during the iterate, the logic need to bind de result into tbl.chatbots
, to increment the data of five bots one at time.