2016-02-11 40 views
10

中工作html_nodes()函數在作爲可執行RScript運行時失敗如下,但在交互式運行時成功。有人知道運行中會有什麼不同嗎?rvest,html_nodes()錯誤:無法將類型'environment'強制爲類型'list'的向量。失敗RScript,在會話

交互式運行使用新的會話運行,源語句是第一次運行。

$ ./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 

然而,它成功時,作爲source()交互方式運行:

> 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 

謝謝 馬特

+0

我沒有用過rvest,但與'RSelenium'遇到類似的問題很多次。它可能會破壞管道,但你可能想用'Sys.sleep(5)'來探索。偶爾我不得不去'Sys.sleep(15)'甚至20來允許頁面加載。 – PavoDive

+4

嘗試添加'庫(方法)'到你的腳本開頭 – hadley

+1

@hadley:添加'library(methods)'工作。如果是解決方案,我會接受它。謝謝你。 – mpettis

回答

6

添加一行:

library(methods) 

每註釋原來的問題由哈德利韋翰沒有解決這個錯誤。爲什麼它解決了錯誤,我不知道。但是我發佈了一個答案,所以這裏有一個很容易引用的解決方案。如果爲什麼這個問題解決了,我會接受這個答案。

添加從下面的評論來自@ mekki - 麥考利成文本這裏,因爲它確實增加了一些清晰:

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

+0

我很想知道爲什麼。 –

+1

這個線程可能會對此有所瞭解。看起來在某些情況下'rsCRIPT'默認不加載'package :: methods',而交互式會話默認加載它。看起來「when」不明確,但爲所有的'RSCRIPT'執行明確調用'library(methods)'似乎是安全的選擇:http://stackoverflow.com/questions/19780515/can-use-package -interactively-but-rscript-gives-errors –

+0

這真的很好理解,謝謝! – mpettis

-1

很可能如何magrittr::%>%運營商工作的副作用。從Magrittr Documentation - Page 8: %>% Pipe

The magrittr pipe operators use non-standard evaluation. They capture their inputs and examines them to figure out how to proceed. First a function is produced from all of the individual right-hand side expressions, and then the result is obtained by applying this function to the left-hand side. For most purposes, one can disregard the subtle aspects of magrittr's evaluation, but some functions may capture their calling environment, and thus using the operators will not be exactly equivalent to the "standard call" without pipe-operators (Emphasis mine).

因此,嘗試沒有%>%,看它是否是因爲html_nodes被錯誤地捕獲在命令行環境(如您的錯誤信息提示),而在互動環節,它可以抓取會話的環境變量:

google_node <- read_html("http://google.com"); 
div_nodes <- html_nodes(google_node, "div"); 
length(div_nodes); 

當作爲一個可執行的RScript調用時,這樣工作嗎?

+0

它不起作用,並且在'html_nodes()'調用中出現相同的錯誤。不過謝謝你的想法。 – mpettis

+0

添加'--vanilla'到命令行調用改變什麼? –

+0

不,使用'--vanilla'我仍然得到相同的錯誤。被稱爲:'$ RScript --vanilla test-pdp.R' – mpettis

相關問題