2016-11-12 214 views
2

我有這個函數不會運行。代碼內部的命令在它自己應用時起作用,但是當我在這個lapply函數中運行它時,它會出錯。我試圖更新我的捲曲庫等...不知道爲什麼發生。R - curl :: curl_fetch_memory(url,handle = handle)中的錯誤:使用錯誤/非法格式或丟失URL的URL

library(curl) 
    ##dataframe looks like this as factor list: x = "http://google.com" 
    funk <- function(x) { 
      read_html(x) %>% html_node("title") %>% html_text 
    } 
    df$titles<-lapply(df$urls,funk) 

    Error: 'Error in curl::curl_fetch_memory(url, handle = handle) : 
    URL using bad/illegal format or missing URL 
    ' does not exist in current working directory ('/Users/Home/'). 

任何幫助表示讚賞。

+0

更新。數據框是一個URL列表,就像上面的例子中的因子列表一樣。 – frameworkgeek

+0

脅迫人格?沒有看到網址就無法診斷。 – alistaire

+0

@alistaire是在樂譜裏試過as.character(df $ urls),沒有運氣 – frameworkgeek

回答

0

功能的工作原理裏面lapply

df <- data.frame(urls=c("http://google.com", "http://ytcracker.com"), stringsAsFactors = F) 
funk <- function(x) { 
    read_html(x) %>% html_node("title") %>% html_text 
} 

lapply(df$urls, funk) 
[[1]] 
[1] "Google" 

[[2]] 
[1] "ytOS/2014" 
df$titles <- lapply(df$urls, funk) 
df 
    urls titles 
1 http://google.com Google 
2 http://ytcracker.com ytOS/2014 

所以這個問題必須與你的問題,的不可再現的一部分將 成爲數據框架(請在將來的R問題中使用dput()您的數據 - 查看R標籤說明了解更多信息)。

具體而言,錯誤消息意味着問題可能是您在該數據框中指定了URL的方式。

更具體地講,當你的錯誤說

' does not exist in current working directory ('/Users/Home/'). 

,幾乎總是意味着你沒有資格跟http://https://域等