“Function” 不是从 “Package” 导出的对象。

huangapple go评论72阅读模式
英文:

"Function" is not an exported object from "Package"

问题

我正在尝试开发一个包,并且在从包中导出函数方面遇到了问题。

我的问题是,尽管这些函数在我的包的NAMESPACE函数中明显可见,并且在使用document()和test()更新和测试包后,但当我下载包并尝试在另一个会话中运行函数时,我收到以下错误消息:

错误:'get_monthly_listeners'不是从'namespace:spotifystreams'导出的对象。

在开发包的会话中,我在使用load_all()后运行以下来自包的函数时没有问题:

#获取每月听众
#@export get_monthly_listeners
#@param artist_code用于识别Spotify艺术家的唯一22个字母数字代码
#@return表示艺术家总每月流的整数
#@importFrom rvest read_html html_elements html_text
#@importFrom dplyr %>%

get_monthly_listeners <- function(artist_code){

#Spotify艺术家页面的链接
artist_url <- paste0("https://open.spotify.com/artist/", artist_code)

#从Spotify页面加载html数据到环境中
web <- rvest::read_html(artist_url)

#选择div内容中的所有文本
div_content <- web %>% rvest::html_elements("div") %>% rvest::html_text()

#向量的第10项包含流信息
monthly_streams <- div_content[10]

#从"每月听众"中分离文本
monthly_streams <- strsplit(monthly_streams, split = " ")
#子集列表,然后向量以获取第一个元素
monthly_streams <- monthly_streams[[1]][1]
#删除逗号,以便我们可以转换为数字
monthly_streams <- as.numeric(gsub(",", "", monthly_streams))

return(monthly_streams)
}

get_monthly_listeners('13y7CgLHjMVRMDqxdx0Xdo')

但在下载包并尝试运行相同函数后,我收到错误消息,说明没有从包中导出任何函数。

devtools::install_github("liamhaller/spotifystreams", force = TRUE)
library(spotifystreams)

spotifystreams::get_monthly_listeners('13y7CgLHjMVRMDqxdx0Xdo')

这是有关此命名空间的问题:https://github.com/liamhaller/spotifystreams/blob/main/NAMESPACE

和来自包的相关函数:https://github.com/liamhaller/spotifystreams/blob/main/R/get_monthly_listeners.R

编辑:经过一段时间后,似乎问题可能与devtools::install_github()没有立即更新托管在GitHub上的版本有关。目前加载到install_github()包中的代码版本似乎没有合并最新的更改,并且是一个小时前的版本。我怀疑install_github()下载的版本可能会有一些延迟。

英文:

I am attempting to develop a package and am having trouble exporting functions from the package.

My issue is that despite the fact that the functions are clearly visible in my packages NAMESPACE function, and after having updated and tested the packaged with document() and test(), when I download the package and attempt to run the function in another session I get the following error error

Error: &#39;get_monthly_listeners&#39; is not an exported object from &#39;namespace:spotifystreams&#39;

On the session with the package in development I had no problem running the following function from the package after using load_all()

#&#39; Get Monthly Listeners
#&#39; @export get_monthly_listeners
#&#39; @param artist_code unique 22 alpha-numeric code to identify spotify artists
#&#39; @return An integer represetning total monthly streams of an artists
#&#39; @importFrom rvest read_html html_elements html_text
#&#39; @importFrom dplyr %&gt;%




get_monthly_listeners &lt;- function(artist_code){

  #Spotify link to artists page
  artist_url &lt;- paste0(&quot;https://open.spotify.com/artist/&quot;, artist_code)

  #Load html data from spotify page in enviroment
  web &lt;- rvest::read_html(artist_url)

  #select all text within div content
  div_content &lt;- web %&gt;% rvest::html_elements(&quot;div&quot;) %&gt;% rvest::html_text()

  #10th item in the vector contains stream info
  monthly_streams &lt;- div_content[10]

  #seperate text from &quot;monthly listerers&quot;
  monthly_streams &lt;- strsplit(monthly_streams, split = &quot; &quot;)
  #subset list, and then vector to get first element
  monthly_streams &lt;- monthly_streams[[1]][1]
  #remove the comma so we can convert to numeric
  monthly_streams &lt;- as.numeric(gsub(&quot;,&quot;, &quot;&quot;, monthly_streams))

  return(monthly_streams)
}


get_monthly_listeners(&#39;13y7CgLHjMVRMDqxdx0Xdo&#39;)

But after downloading the package and attempting to run the same function, I get the error that no functions from within the package are exported.

devtools::install_github(&quot;liamhaller/spotifystreams&quot;, force = TRUE)
library(spotifystreams)

spotifystreams::get_monthly_listeners(&#39;13y7CgLHjMVRMDqxdx0Xdo&#39;)

This is the namespace in question: https://github.com/liamhaller/spotifystreams/blob/main/NAMESPACE

And associated function from the package: https://github.com/liamhaller/spotifystreams/blob/main/R/get_monthly_listeners.R

Edit: After giving it some time it appears as if the issue might be with devtools::install_github() not updating the version that is hosted on github immediately. The version of the code that is currently being loaded into the install_github() package appears not to have incorporated the most up-to-date changes and is an hour old. I suspect there might be some delay with the version that install_github() downloads

答案1

得分: 1

以下是代码的翻译部分:

# 获取每月听众数
# @param artist_code 唯一的 22 个字母数字代码,用于识别 Spotify 艺术家
# @return 代表艺术家总月度流量的整数
# @importFrom rvest read_html html_elements html_text
# @importFrom dplyr %>%
# @export
get_monthly_listeners <- function(artist_code){

  # Spotify 艺术家页面的链接
  artist_url <- paste0("https://open.spotify.com/artist/", artist_code)

  # 从 Spotify 页面中加载 HTML 数据
  web <- rvest::read_html(artist_url)

  # 选择 div 内容中的所有文本
  div_content <- web %>% rvest::html_elements("div") %>% rvest::html_text()

  # 向量中的第 10 项包含流量信息
  monthly_streams <- div_content[10]

  # 从“monthly listeners”中分离文本
  monthly_streams <- strsplit(monthly_streams, split = " ")
  # 子集列表,然后将其转化为向量以获取第一个元素
  monthly_streams <- monthly_streams[[1]][1]
  # 删除逗号以便进行数值转换
  monthly_streams <- as.numeric(gsub(",", "", monthly_streams))

  return(monthly_streams)
}

get_monthly_listeners('13y7CgLHjMVRMDqxdx0Xdo')

希望这能帮助您理解代码的功能。

英文:

You don't need to add the function name after "@export". Try the below:

#&#39; Get Monthly Listeners
#&#39; @param artist_code unique 22 alpha-numeric code to identify spotify artists
#&#39; @return An integer represetning total monthly streams of an artists
#&#39; @importFrom rvest read_html html_elements html_text
#&#39; @importFrom dplyr %&gt;%
#&#39; @export
get_monthly_listeners &lt;- function(artist_code){

  #Spotify link to artists page
  artist_url &lt;- paste0(&quot;https://open.spotify.com/artist/&quot;, artist_code)

  #Load html data from spotify page in enviroment
  web &lt;- rvest::read_html(artist_url)

  #select all text within div content
  div_content &lt;- web %&gt;% rvest::html_elements(&quot;div&quot;) %&gt;% rvest::html_text()

  #10th item in the vector contains stream info
  monthly_streams &lt;- div_content[10]

  #seperate text from &quot;monthly listerers&quot;
  monthly_streams &lt;- strsplit(monthly_streams, split = &quot; &quot;)
  #subset list, and then vector to get first element
  monthly_streams &lt;- monthly_streams[[1]][1]
  #remove the comma so we can convert to numeric
  monthly_streams &lt;- as.numeric(gsub(&quot;,&quot;, &quot;&quot;, monthly_streams))

  return(monthly_streams)
}


get_monthly_listeners(&#39;13y7CgLHjMVRMDqxdx0Xdo&#39;)

huangapple
  • 本文由 发表于 2023年6月2日 00:57:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定