R:自定义包安装,加载正常,但功能不存在

R: Custom packages installs, loads fine, but functions are not there

There is a previous question here 有很多相同的问题,但我不理解给出的解决方案并且它没有作为答案给出,仅在评论中给出。因此需要一个新问题。

我已经按照 this guide 创建了一个新的 R 包。

为了创建、安装和加载它,我使用了以下代码:

#libs
ibrary(pacman)
p_load(devtools, roxygen2)

#create
#navigate to the current folder
create("kirkegaard")

#make documentation
setwd("./kirkegaard")
document()

#install
setwd("..")
install("kirkegaard")

#load
library(kirkegaard)

我已将一个文件 functions.R 放入 R 文件夹中。之所以这样称呼它,是因为我想将所有函数放在一个文件中,而不是像指南建议的那样将文件分开。

上面写着:

# This file contains various useful functions for working with datasets
#
#

#this is the dataset merger
#note thet variables from DF1 are put in front
#note also that DF1 overwites any values from DF1

#' Dataset merger function
#'
#' This function allows you to merge two data.frames by their overlapping rownames.
#' @param DF1 the first data.frame
#' @param DF2 the second data.frame
#' @param main which data.frame should be used as the main? Choose the larger one if working with large datasets. Default to using neither.
#' @keywords merging combining datasets data.frame
#' @export
#' @examples
#' merge.datasets()
merge.datasets = function (DF1, DF2, main=0, time=F){
  #time if desired
  if (time) {time1 = proc.time()} #start timer

  #colnames, remove duplicates
  total.colnames = c(colnames(DF1),colnames(DF2))
  total.colnames.unique = unique(total.colnames)

  #rownames, remove duplicates
  total.rownames = c(rownames(DF1),rownames(DF2))
  total.rownames.unique = unique(total.rownames)

  #combined dataset
  #main setting decides how to combine
  #default is to create a new DF and add everything into it
  #but this will be slow for larger DFs
  if (!(main == 1 | main == 2 | main == 0)){ #check for valid input
      print("Valid input to parameter 'main' not provided");return(NULL)
  }
  if (main==0){ #create a combined dataset
    DF3 = as.data.frame(matrix(nrow = length(total.rownames.unique),
                             ncol = length(total.colnames.unique)))
    rownames(DF3) = sort(total.rownames.unique)
    colnames(DF3) = total.colnames.unique
  }
  if (main==1){ #use first DF as main
      DF3 = DF1
  }
  if (main==2){ #use second DF as main
      DF3 = DF2
  }

  if (main!=2){
    #loop over input dataset 2
    for (variable in 1:length(colnames(DF2))){ #loop over variables/cols
        for (case in 1:length(rownames(DF2))){ #loop over cases/rows
          if (is.na(DF2[case,variable])){ #skip if datapoint is missing
              next
          }
          DF3[rownames(DF2)[case], colnames(DF2)[variable]] = DF2[case,variable]
          #print(DF3[rownames(DF2)[case], colnames(DF2)[variable]]) #used for debugging
        }
    }
  }
  if (main!=1){ #if DF2 is main
        #loop over input dataset 1
        for (variable in 1:length(colnames(DF1))){ #loop over variables/cols
            for (case in 1:length(rownames(DF1))){ #loop over cases/rows
              if (is.na(DF1[case,variable])){ #skip if datapoint is missing
                next
              }
            DF3[rownames(DF1)[case], colnames(DF1)[variable]] = DF1[case,variable]
            #print(DF3[rownames(DF1)[case], colnames(DF1)[variable]]) #used for debugging
            }
        }
    }

  #output time
  if (time) {
    time2 = proc.time()-time1 #end timer
    print(time2) #print time
  }

  return(DF3)
}

该函数按行合并两个 data.frames。此功能与 data.frames 上的完全外部联接 sql 命令基本相同,使用行名来匹配行。

加载和安装都很好。它在 RStudio 中显示为一个包。文档也在那里。但是,调用该函数只会给出:

#some data to merge
d1 = iris[sample(1:150, 50),] #random sample from isis
d2 = iris[sample(1:150, 50),]

#try it
merge.datasets(d1, d2)
kirkegaard::merge.datasets(d1, d2)

然而,这只是给出:

> merge.datasets(d1, d2)
Error: could not find function "merge.datasets"
> kirkegaard::merge.datasets(d1, d2)
Error: 'merge.datasets' is not an exported object from 'namespace:kirkegaard'

怎么了? R 以某种方式加载了文档,但没有加载函数。或者它隐藏在错误的命名空间中。

我查看了另一个问题的评论中提到的命名空间文件,但它没有说任何对我有用的东西:

# Generated by roxygen2 (4.1.0.9001): do not edit by hand

S3method(merge,datasets)

基于尼古拉的上述评论。这里的解决方案是避免在函数名称中使用点。所以将函数重命名为merge_datasets(),然后就可以了。