英文:
"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: 'get_monthly_listeners' is not an exported object from 'namespace:spotifystreams'
On the session with the package in development I had no problem running the following function from the package after using load_all()
#' Get Monthly Listeners
#' @export get_monthly_listeners
#' @param artist_code unique 22 alpha-numeric code to identify spotify artists
#' @return An integer represetning total monthly streams of an artists
#' @importFrom rvest read_html html_elements html_text
#' @importFrom dplyr %>%
get_monthly_listeners <- function(artist_code){
#Spotify link to artists page
artist_url <- paste0("https://open.spotify.com/artist/", artist_code)
#Load html data from spotify page in enviroment
web <- rvest::read_html(artist_url)
#select all text within div content
div_content <- web %>% rvest::html_elements("div") %>% rvest::html_text()
#10th item in the vector contains stream info
monthly_streams <- div_content[10]
#seperate text from "monthly listerers"
monthly_streams <- strsplit(monthly_streams, split = " ")
#subset list, and then vector to get first element
monthly_streams <- monthly_streams[[1]][1]
#remove the comma so we can convert to numeric
monthly_streams <- as.numeric(gsub(",", "", monthly_streams))
return(monthly_streams)
}
get_monthly_listeners('13y7CgLHjMVRMDqxdx0Xdo')
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("liamhaller/spotifystreams", force = TRUE)
library(spotifystreams)
spotifystreams::get_monthly_listeners('13y7CgLHjMVRMDqxdx0Xdo')
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:
#' Get Monthly Listeners
#' @param artist_code unique 22 alpha-numeric code to identify spotify artists
#' @return An integer represetning total monthly streams of an artists
#' @importFrom rvest read_html html_elements html_text
#' @importFrom dplyr %>%
#' @export
get_monthly_listeners <- function(artist_code){
#Spotify link to artists page
artist_url <- paste0("https://open.spotify.com/artist/", artist_code)
#Load html data from spotify page in enviroment
web <- rvest::read_html(artist_url)
#select all text within div content
div_content <- web %>% rvest::html_elements("div") %>% rvest::html_text()
#10th item in the vector contains stream info
monthly_streams <- div_content[10]
#seperate text from "monthly listerers"
monthly_streams <- strsplit(monthly_streams, split = " ")
#subset list, and then vector to get first element
monthly_streams <- monthly_streams[[1]][1]
#remove the comma so we can convert to numeric
monthly_streams <- as.numeric(gsub(",", "", monthly_streams))
return(monthly_streams)
}
get_monthly_listeners('13y7CgLHjMVRMDqxdx0Xdo')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论