the html_nodes()
function fails as follows when run as executable RScript, but succeeds when run interactively. Does anybody know what could be different in the runs?
The interactive run was run with a fresh session, and the source statement was the first one run.
$ ./test-pdp.R
>
> ################################################################################
> # Setup
> ################################################################################
> suppressPackageStartupMessages(library(plyr))
> suppressPackageStartupMessages(library(dplyr))
> suppressPackageStartupMessages(library(stringr))
> suppressPackageStartupMessages(library(rvest))
> suppressPackageStartupMessages(library(httr))
>
>
> read_html("http://google.com") %>%
+ html_nodes("div") %>%
+ length()
Error in as.vector(x, "list") :
cannot coerce type 'environment' to vector of type 'list'
Calls: %>% ... <Anonymous> -> lapply -> as.list -> as.list.default
Execution halted
Yet it succeeds when run as source()
interactively:
> source("/Users/a6001389/Documents/projects/hottest-deals-page-scrape/src/test-pdp.R", echo=TRUE)
> #!/usr/bin/RScript
> options(echo=TRUE)
> ################################################################################
> # Setup
> ####################################################### .... [TRUNCATED]
> suppressPackageStartupMessages(library(dplyr))
> suppressPackageStartupMessages(library(stringr))
> suppressPackageStartupMessages(library(rvest))
> suppressPackageStartupMessages(library(httr))
> read_html("http://google.com") %>%
+ html_nodes("div") %>%
+ length()
[1] 17
Thank you, Matt
Adding the line:
library(methods)
Per the comment to the original question by Hadley Wickham did solve this error. Why it solved the error, I do not know. But I am posting an answer so there is an easily referenced solution here. If why this solves the problem is posted, I will accept that answer.
Adding comment from below from @mekki-macaulay into text here because it really adds some clarity:
This thread might shed some light on it. It seems that in some contexts RSCRIPT doesn't load package::methods by default, whereas interactive sessions do load it by default. It seems that the "when" is not clear, but explicitly calling library(methods) for all RSCRIPT executions seems to be the safe bet: can use package interactively, but Rscript gives errors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With