使用@importFrom pkg导入功能时出现问题

时间:2019-03-23 10:44:20

标签: r

我目前正在尝试开发我的第一个R软件包并遇到以下问题。

我有一个Description文件,其中所有需要的软件包都列在 Imports (进口)中,就像Hadley Wickham在书中建议的那样。我正在使用@importFrom dplyr select加载函数以供内部使用。

运行devtools::document()时出现错误:

  

选择错误(paths_original,household_id = H_ID,person_id = P_ID 、:     找不到功能“选择”

如果有人可以帮助我理解我的错误,那将会很棒。

谢谢!

编辑:

说明

Package: first_package
Title: first package
Version: 0.0.0.9000
Description: first package
Depends:
    R (>= 3.5.2)
Imports:
    foreign (>= 0.8.71),
    plyr (>= 1.8.4),
    dplyr (>= 0.7.7),
    leaflet (>= 2.0.2),
    sf (>= 0.7.1),
    rgeos (>= 0.4.2),
    geosphere (>= 1.5.7),
    sp (>= 1.3.1),
    rgdal (>= 1.3.6),
    mapview (>= 2.6.0),
    lwgeom (>= 0.1.6),
    roxygen2
License: BSD 2-Clause + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.0

功能:

get_clear_df_paths <- function(paths_original) {
  clean_paths <- select(paths_original,
                        household_id = H_ID,
                        person_id = P_ID,
                        household_person_id = HP_ID,
                        weekday = ST_WOTAG,
                        month = ST_MONAT,
                        holiday = feiertag,
                        season = saison,
                        regular_job_related_path = W_RBW,
                        path_purpose = W_ZWECK,
                        starting_point = W_SO2,
                        start_time_hour = W_SZS,
                        start_time_min = W_SZM,
                        arrival_next_day = W_FOLGETAG,
                        arrival_time_hour = W_AZS,
                        arrival_time_min = W_AZM,
                        path_length = wegkm,
                        path_length_imp = wegkm_imp,
                        path_duration_min = wegmin,
                        path_duration_min_imp = wegmin_imp,
                        main_vehicle = hvm,
                        car_driver = pkw_fmf,
                        vehicle_car = W_VM_G,
                        vehicle_carsharing = W_VM_H,
                        district = stt_mun)
}

imports.R

#' @importFrom plyr revalue
#' @importFrom dplyr select
#' @importFrom foreign read.spss
NULL

NAMESPACE

# Generated by roxygen2: do not edit by hand

importFrom(dplyr,select)
importFrom(foreign,read.spss)
importFrom(plyr,revalue)

带有相关代码段的公共摘要链接:

https://gist.github.com/bgrt/b7d32cb3aa0bb128f276bad86c89bdd4

2 个答案:

答案 0 :(得分:1)

我讨厌添加它,因为它不是“答案”,并且会对其进行更新以尽快包含答案或将其删除。但是,我认为证明OP的基本方法应该有效可能会有所帮助,因此我创建了一个最小的示例包,其中导入了明显的问题功能。

首先,我设置了包结构:

library(devtools)
create_package("funImport", rstudio = FALSE)
use_package("dplyr")
use_gpl3_license("X")

然后我向R/添加了一个文件,其中包含以下内容:

#' Select wrapper
#' 
#' @param .data A tbl
#' @param ... Variable names to select
#' 
#' @return The selected variables
#' @export
Select <- function(.data, ...) {
    return(select(.data, ...))
}

#' @importFrom dplyr select
NULL

然后,我可以document()install()check()没问题了:

document()
# Updating funImport documentation
# Updating roxygen version in /home/jb/funImport/DESCRIPTION
# Writing NAMESPACE
# Loading funImport
# Writing NAMESPACE
# Writing Select.Rd
install()
# Output omitted
check()
# Some output omitted
# ── R CMD check results ─────────────────────────────── funImport 0.0.0.9000 ────
# Duration: 48.2s
# 
# 0 errors ✔ | 0 warnings ✔ | 0 notes ✔

我也能够使用该功能,没问题:

library(funImport)
tbl <- tibble::tibble(x = 1:10, y = letters[1:10])
tbl
# # A tibble: 10 x 2
#        x y    
#    <int> <chr>
#  1     1 a    
#  2     2 b    
#  3     3 c    
#  4     4 d    
#  5     5 e    
#  6     6 f    
#  7     7 g    
#  8     8 h    
#  9     9 i    
# 10    10 j  
Select(tbl, x)
# # A tibble: 10 x 1
#        x
#    <int>
#  1     1
#  2     2
#  3     3
#  4     4
#  5     5
#  6     6
#  7     7
#  8     8
#  9     9
# 10    10

答案 1 :(得分:1)

创建了一个虚拟包进行测试,并且可以正常工作。您需要记录功能,如下所示。另外,请提供一种为函数中的某些变量提供全局绑定的方法。

    #' Some paths
    #' @description some paths
    #' @param paths_original Some path
    #' @importFrom dplyr select
    #' @export 
    get_clear_df_paths <- function(paths_original) {
      clean_paths <- select(paths_original,
                            household_id = H_ID,
                            person_id = P_ID,
                            household_person_id = HP_ID,
                            weekday = ST_WOTAG,
                            month = ST_MONAT,
                            holiday = feiertag,
                            season = saison,
                            regular_job_related_path = W_RBW,
                            path_purpose = W_ZWECK,
                            starting_point = W_SO2,
                            start_time_hour = W_SZS,
                            start_time_min = W_SZM,
                            arrival_next_day = W_FOLGETAG,
                            arrival_time_hour = W_AZS,
                            arrival_time_min = W_AZM,
                            path_length = wegkm,
                            path_length_imp = wegkm_imp,
                            path_duration_min = wegmin,
                            path_duration_min_imp = wegmin_imp,
                            main_vehicle = hvm,
                            car_driver = pkw_fmf,
                            vehicle_car = W_VM_G,
                                vehicle_carsharing = W_VM_H,
                                district = stt_mun)
    }

步骤:

library(devtools)
library(roxygen2)
create("SODummypkg")
document("SODummypkg")
check("SODummypkg")

结果:忽略警告(出于此答案的目的)。至少错误不会出现。

-- R CMD check results ---------------------------------------------- SODummypkg 0.0.0.9000 ----
Duration: 1m 10.7s

> checking DESCRIPTION meta-information ... WARNING
  Dependence on R version '3.5.3' not with patchlevel 0

0 errors √ | 1 warning x | 0 notes √