rrscriptrpresrfacebook

Stop Rfacebook for loop outputting while still running code


The point in the code is to gather posts from a Facebook page and store them in my_page however i am unfamiliar with the code as it is for a Uni project. The problem i have is that it has to be used in a .rpres format created using Rstudio and as such i don't want the output but still need to run the code.

This is the output i don't want to be displayed:Code output

```{r, echo = FALSE}

#install.packages("Rfacebook") 

include(Rfacebook)

token <- "Facebook dev auth token goes here"

page_name <- "BuzzFeed"

my_page <- getPage(page_name, token, n = 2,reactions = TRUE,api = "v2.10") 

number_required <- 50

dates <- seq(as.Date("2017/07/14"), Sys.Date(), by = "day")

#
n <- length(dates) - 1 

df_daily <- list() 

for (i in 1:n){

cat(as.character(dates[i]), " ")

try(df_daily[[i]] <- getPage(page_name, token, 
                             n = number_required,reactions = TRUE,api = "v2.10", 
                             since = dates[i], 
                             until = dates[i+1]))

cat("\n")
}
```

Solution

  • Your problem is simply that Rfacebook::getPage prints to the console when it runs. That's because it calls cat(), which is the same thing as print(). Fortunately the package provides a switch to turn that off - all you need to do is add the verbose = FALSE argument to your call and it will stop printing:

    getPage(...)
    getPage(..., verbose = FALSE)
    

    It's pretty bad practice for a package to call cat or print - they should use message and warning instead - so I have raised an issue with the package maintainer to ask for this to be changed, which you can watch here if you like:

    https://github.com/pablobarbera/Rfacebook/issues/145