简体   繁体   English

创建的 package 未从 NAMESPACE 文件中导入的包中运行函数

[英]Created package not running functions from imported packages in NAMESPACE file

this is my first package.这是我的第一个 package。 Here is how my.R file goes, with 3 functions, two of them to be exported.这是 my.R 文件的运行方式,具有 3 个函数,其中两个要导出。 Their actual content doesn't matter much, only that it uses a lot of tidyverse functions.它们的实际内容并不重要,只是它使用了很多tidyverse函数。

#' @import tidyverse
#' @importFrom labelled set_variable_labels var_label
#' @importFrom readxl read_excel cell_cols

NULL

#' Carregando dados brutos
#'
#' @param diretorio_dados Diretório onde os microdados originais em formato de texto estão armazenados
#'
#' @param diretorio_dicionario Diretório onde o dicionário da pesquisa em formato xls está armaezenado
#'
#' @param ... vetores com datas das pesquisas de interesse no  formato \code{c('trimestre', 'ano')}
#'
#' @return Lista de dataframes, sendo cada entrada um trimestre/ano em \code{...}
#' @encoding UTF-8
#' @export
#'
#' @examples
#' datazoom_pnadc('./Desktop', './Desktop', c(1, 2000), c(2, 2000))
#'
datazoom_pnadc <- function(diretorio_dados,
                           diretorio_dicionario,
                           ...) {


  return(NULL)
}

 #' Painel básico
 #'
 #' @param build_data Default \code{TRUE}.
 #' Se \code{TRUE}, implementa primeiro \code{\link{datazoom_pnadc}} e depois
 #' monta paineis de indivíduos. Se \code{FALSE}, a função constrói paneis a partir de dados já carregados no R
 #'
 #' @param dados_prontos Bases de dados para diferentes trimestres da PNAD contínua.
 #' Necessário se \code{build_data = FALSE}
 #'
 #' @param local_dados Diretório onde os microdados originais em formato de texto estão armazenados
 #' caso \code{build_data = TRUE}
 #'
 #' @param local_dicionarios Diretório onde o dicionário da pesquisa em formato xls está armaezenado caso
 #' \code{build_data = TRUE}
 #'
 #' @param periodos Lista de vetores com períodos de interesse no formato
 #' \code{periodos = list(c(trimestre1, ano1), c(trimestre2, ano2), ...)}
 #'
 #' @encoding UTF-8
 #'
 #' @return Lista de dataframes, sendo cada entrada um trimestre/ano
 #'
 #' @examples
 #' PNADC_2012 <- datazoom_pnadc(diretorio_dados = './Desktop',
 #' diretorio_dicionario = './pnadcontinua/Desktop',
 #' c(1,2012), c(2,2012))
 #'
 #' teste <- pnadc_painel_basico(build_data = FALSE,
 #'                              dados_prontos = PNADC_2012)
 #'
 #' teste2 <- pnadc_painel_basico(build_data = TRUE,
 #'                               local_dados = './pnadcontinua',
 #'                               local_dicionario = './pnadcontinua/Dicionario_e_input',
 #'                               periodos = list(c(1,2012), c(2,2012)))
 #' @export
pnadc_painel_basico <- function(build_data = TRUE, ...){
    return(NULL)

}

For some reason, even after running devtools::document() and having this in my NAMESPACE file:出于某种原因,即使在运行devtools::document()并将其包含在我的 NAMESPACE 文件中之后:

# Generated by roxygen2: do not edit by hand

export(datazoom_pnadc)
export(pnadc_painel_basico)
import(tidyverse)
importFrom(labelled,set_variable_labels)
importFrom(labelled,var_label)
importFrom(readxl,cell_cols)
importFrom(readxl,read_excel)

Functions from the package do not run after it is installed and loaded if I do not set library(tidyverse) , library(labelled) and library(readxl) beforehand.如果我没有事先设置library(tidyverse)library(labelled)library(readxl) ,则 package 的功能在安装和加载后不会运行。

Alongside the other required parts, my DESCRIPTION file has this:除了其他必需的部分,我的说明文件有这个:

Imports: 
    labelled,
    readxl,
    tidyverse

Can someone help?有人可以帮忙吗?

Moving tidyverse from Imports to Depends in DESCRIPTION would make it work, but is not the recommended approach if you are developing a formal package.tidyverseImports移到Depends in DESCRIPTION会使其工作,但如果您正在开发正式的 package,则不推荐使用这种方法。 Like @Bruno commented:就像@Bruno 评论的那样:

> usethis::use_package("tidyverse")
Error: 'tidyverse' is a meta-package and it is rarely a good idea to
depend on it. Please determine the specific underlying package(s) that
offer the function(s) you need and depend on that instead. For data 
analysis projects that use a package structure but do not implement a
formal R package, adding 'tidyverse' to Depends is a reasonable 
compromise. Call `use_package("tidyverse", type = "depends")` to achieve
this.

The reason why having it in Imports does not work is that only variables exported by the packages listed there are placed on the search path, as mentioned in the section Package namespaces of Writing R Extensions :将它放在Imports中不起作用的原因是只有在那里列出的包导出的变量被放置在搜索路径上,如编写 R Extensions 的 Package 命名空间部分所述:

Only the exported variables are placed in the attached frame.只有导出的变量被放置在附加的框架中。 Loading a package that imports variables from other packages will cause these other packages to be loaded as well (unless they have already been loaded), but they will not be placed on the search path by these implicit loads.加载从其他包导入变量的 package 也会导致这些其他包也被加载(除非它们已经被加载),但这些隐式加载不会将它们放在搜索路径上。 Thus code in the package can only depend on objects in its own namespace and its imports (including the base namespace) being visible因此 package 中的代码只能依赖于其自己的命名空间中的对象,并且其导入(包括基本命名空间)是可见的

tidyverse , being a meta-package, does not export the functions of its included packages, but uses the .onAttach hook to attach these packages (see tidyverse:::.onAttach for details). tidyverse是一个元包,它不会导出其包含的包的功能,而是使用.onAttach挂钩来附加这些包(详见tidyverse:::.onAttach )。 This means that if tidyverse is imported using the above method then this hook will not run and the variables exported by the other packages will not be added to the search path, which is why you can not access them.这意味着如果使用上述方法导入tidyverse ,则该钩子将不会运行,并且其他包导出的变量不会添加到搜索路径中,这就是您无法访问它们的原因。

Putting tidyverse under Depends does run the .onAttach hook, and therefore the other packages are also attached.tidyverse放在Depends下确实会运行.onAttach挂钩,因此也会附加其他包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM