0

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.

Rafael Lima
  • 420
  • 1
  • 5
  • 16
  • 1
    what is `fun.get_data_blip` – akrun Jul 22 '19 at 20:44
  • It would help if this question were a bit more reproducible and clear. For instance, as akrun commented, we don't know what your function is or does. Second, it would help to know what you expect `tbl.chatbots` to look like as output. – r2evans Jul 22 '19 at 20:53
  • This is my function, I think it would be irrelevant to post the contents of it, since I'd just like to know how to iterate each `tbl.chatbots` line within the second parameter. – Rafael Lima Jul 22 '19 at 20:54
  • @r2evans, I agree. Above, I added the function – Rafael Lima Jul 22 '19 at 20:57
  • Are you looking for `rlang::exec` or `do.call`? – Calum You Jul 22 '19 at 20:57

0 Answers0