0

I'm making a deploy for R via plumber package api, the code itself runs flawlessly when local, but when it's inside plumber's environment it gives the follwing error <simpleError in do.call(private$func, args, envir = private$envir): 'what' must be a function or character string>

There's is the example:

    library('tidyverse')

    #data head using dput()
    tweets <- structure(list(text = c("@dottore_marcelo @LorranParadiso @1pedroOsilva @Ronaldocampos00 @jairbolsonaro Mas quem disse que @jairbolsonaro vai resolver todos os problemas do país tem 4 anos? Ele é um ponto de inflexão, quem sabe depois de 8 anos elegeremos um rocha ou um Amoedo, pois a estrada já estará pavimentada. Vamos pensar que no longo prazo a disputa será entre liber e conser", 
"@Ideias_Radicais Opiniao sobre a Marina Silva? Geraldo Alckmin? vai fazer oq se eles ganhar as eleiçoes?", 
"@pkogos E se a Marina Silva ou o Ciro gomes ganhar?", "@pkogos A França está dominada pela mentalidade esquerdista ! Se a Marina Silva ou o Ciro Ganhar vai acontecer o mesmo", 
"@cirogomes @guilhermefpenna @geraldoalckmin @MarinaSilva @jairbolsonaro @alvarodias_ Passo. Próximo.", 
"@joaopedro27696 @marx_araujo @folha 1) Não sou robô; 2) É \"Amoêdo\" e não \"Amoado\"; 3) Não voto com base em pesquisa, e sim em ideias, currículo e histórico... @jairbolsonaro é populista"
), created_at = structure(c(1527523890, 1527799974, 1527650098, 
1527724269, 1527881693, 1528111294), class = c("POSIXct", "POSIXt"
))), row.names = c(NA, 6L), class = "data.frame", .Names = c("text", 
"created_at"))

##data for date filtering

min_data_tweet <- min(tweets$created_at)
max_data_tweet <- max(tweets$created_at)

##regex I want to group_by 
reg_twe <- c("Bolsonaro"='bolsonaro|@jairbolsonaro',
             "João Amoêdo" ='amoedo|@joaoamoedonovo',
             "Marina Silva" ='marina silva|@marinasilva')

## This is the function that works without any problem
 map_df(reg_twe, 
             ~tweets %>% 
               filter(created_at >= min_data,
                      created_at <= max_data) %>%
               summarise(regex=.x,
                         n=sum(grepl(.x, text, ignore.case = TRUE))) %>%
               mutate(portal = 'Twitter'), 
             .id="Candidato") %>%
        select(Candidato, portal, n)

#Expected output

     Candidato  portal n
1    Bolsonaro Twitter 3
2  João Amoêdo Twitter 1
3 Marina Silva Twitter 4

Now, when I put this inside a plumber function, loading all the data before it, doing this:

#' mencoes por candidato
#' @param pres bolsonaro amoedo marinasilva
#' @param fonte twitter noticias
#' @param min_data format: yyyy-mm-dd
#' @param max_data 
#' @get /candidato_mencoes

cat('Running candidato_mencoes\n')

function(min_data = min_data_tweet, 
         max_data = max_data_tweet){

      map_df(reg_twe, 
             ~tweets %>% 
               filter(created_at >= min_data,
                      created_at <= max_data) %>%
               summarise(regex=.x,
                         n=sum(grepl(.x, text, ignore.case = TRUE))) %>%
               mutate(portal = 'Twitter'), 
             .id="Candidato") %>%
        select(Candidato, portal, n)

}
## I get this error
<simpleError in do.call(private$func, args, envir = private$envir): 'what' must be a function or character string>
## sometimes when tweaking the function, this warning
Warning in formals(fun) : argument is not a function

This is the function (different file) to run the plumber.R (previous function) file

    library('plumber')
setwd("~/path/to/plumber")

pr <- plumber::plumb("plumber.R")
pr$run(port = 2424)

To connect to the api: localhost:2424/candidato_mencoes

So far I've read that it might be a conflict of a variable being name of a function, but I couldn't trace it. The problem runs only when running in the plumber so, I'm not sure if it could be a bug.

Ochetski
  • 105
  • 1
  • 13

1 Answers1

1

Ok, the error was mine and pretty simple. I wanted to know when a function was called using ´cat()´ but it was in the wrong place, because of it the plumber wasn't creating a function. The second part has to be this way:

#' mencoes por candidato
#' @param pres bolsonaro amoedo marinasilva
#' @param fonte twitter noticias
#' @param min_data format: yyyy-mm-dd
#' @param max_data 
#' @get /candidato_mencoes

    function(min_data = min_data_tweet, 
             max_data = max_data_tweet){
### put this cat() inside the function.
        cat('Running candidato_mencoes\n')

          map_df(reg_twe, 
                 ~tweets %>% 
                   filter(created_at >= min_data,
                          created_at <= max_data) %>%
                   summarise(regex=.x,
                             n=sum(grepl(.x, text, ignore.case = TRUE))) %>%
                   mutate(portal = 'Twitter'), 
                 .id="Candidato") %>%
            select(Candidato, portal, n)

    }
Ochetski
  • 105
  • 1
  • 13