相同的R函数在Global Env中作为函数/对象工作,从R包

时间:2016-08-03 06:04:49

标签: r scope package environment-variables packages

这可能是一个难以解决的问题,因为我无法在不向Gmail API提供访问令牌的情况下轻松提供可重现的SNAFU,但我希望我能解决一个问题,这个问题在我的说明中已经足够清楚了。我们会看到......

我写了一个函数,它接受Google学术搜索警报的gmail消息ID并将其解析为一个数据框,其中包含文章标题,作者,出版物,日期等列。我的难题是相同的代码我以交互方式加载它(即,将函数加载到会话RAM中“my_fancy_function< - function(arguments){blah blah})”当我将函数作为R包的一部分加载时,它不起作用(la'devtools: :load_all的数据类型( “/ mypackage的/”)')。值得注意的是,我的包中的其他主要功能都可以正常工作,但是当我尝试在通过devtools :: load_all(“my_misbehaving_package /")”加载包之后从包版本中使用它时,它会跳起来。

下面是函数的.R文件内容,当作为软件包的一部分加载时不会到达脚跟 - 我在此提前确认这是一些丑陋的代码,如果那样会让我不知所措在你的答案摇摆。这感觉就像经典的“字符串作为因素”SNAFU的包版本,但你告诉我。问题似乎在功能评估的早期发生,我得到的错误如下:

UseMethod(“read_xml”)中的错误:   没有适用于'read_xml'的方法应用于类“NULL”的对象

这是我为此功能编写的全部代码:

library(stringr)
library(rvest)
library(plyr)
library(dplyr)
library(lubridate)
library(gmailr)

GScholar_alert_msg_to_df <- function(message_id){
     one_message <- message(message_id)
     msg_html <- body(one_message)
     title <- read_html(msg_html) %>% html_nodes("h3") %>% html_text()
     link <- read_html(msg_html) %>% html_nodes("h3 a") %>% html_attr("href")
     msg_chunks <- msg_html %>% str_split("<a href") %>% unlist
     msg_chunks <- msg_chunks[2:(length(msg_chunks)-2)]
     excerpt <- msg_chunks %>% str_replace_all(fixed("<b>"), "") %>%
          str_replace_all(fixed("</b>"), "") %>%
          str_replace_all(fixed("<br>"), "") %>%
          str_extract("<(font size=2 color=#222222|font size=\"-1\")>(.*?)</font>") %>%
          unlist %>% str_replace_all("<(font size=2 color=#222222||font size=\"-1\")>", "") %>%
          str_replace_all("</font>", "")
     author_pub_field <- msg_chunks %>% str_replace_all(fixed("<b>"), "") %>%
     str_replace_all(fixed("</b>"), "") %>%
     str_extract("<font size=(2|\"-1\") color=#(006621|009933|008000)>(.*?)</font>") %>%
     unlist %>%
     str_replace_all("<font size=(2|\"-1\") color=#(006621|009933|008000)>", "") %>%
     str_replace_all("</font>", "")
     one_message_df <- data.frame(title, excerpt, link, author_pub_field, stringsAsFactors = FALSE)
     one_message_df$date <- date(one_message) # needs reformatting
     one_message_df$date %>% str_replace("[[:alpha:]]{3}, ", "") %>%
          str_extract("^.{11}") %>% dmy -> one_message_df$date
     one_message_df$author_only <- str_detect(one_message_df$author_pub_field, " - ")
     one_message_df$author <- one_message_df$author_pub_field %>%
          str_extract("^(.*?) - ") %>% str_replace(" - ", "")
     one_message_df$author <- ifelse(one_message_df$author_only == 1, one_message_df$author, one_message_df$author_pub_field)
     one_message_df$publication <- one_message_df$author_pub_field %>%
          str_extract(" - (.*?)$") %>% str_replace(" - ", "") %>%
          str_replace(", [0-9]{4}$", "") %>% str_replace("^[0-9]{4}$", NA)
     one_message_df$publication <- str_replace(one_message_df$publication, "^[0-9]{4}$", NA)
     one_message_df$author_MIAs <- str_detect(one_message_df$author, "&hellip;")
     one_message_df$author %>% str_replace("&hellip;", " \\.\\.\\.") ->      one_message_df$author
     one_message_df$pub_name_clipped <- one_message_df$publication %>% str_detect("&hellip;")
     one_message_df$publication %>% str_replace("&hellip;", " \\.\\.\\.") -> one_message_df$publication
     return(one_message_df)

是的,它很难看,但是我再次承诺,当代码以交互方式输入时,它会起作用。至于行为不端的软件包版本,根据RStudio回溯,错误似乎发生在顶部,我认为我正在使用gmailr包中的body()或message()函数。 (是的,我通过终端尝试了代码,没有更快乐的结论。)帮助我,哦,无论是谁,你都是我唯一的希望。

1 个答案:

答案 0 :(得分:0)

感谢@MrFlick指出了一条走出困境的道路。我能够通过将包列表移动到&#39;取决于&#39;来解决问题。 DESCRIPTION文件的字段,我还必须使用&#39; ::&#39;运营商明确呼叫&#39;机构&#39;和&#39; date&#39;来自gmailr包的函数。这两个名字也属于非标准功能,属于&#39; base&#39;命名空间,它们不能通过在包环境中加载gmailr来屏蔽。根据哈德利的说法,我应该使用&#39; ::&#39;当我自己写一个函数时,对于每个函数的使用。