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



这里有一个以前的问题,有很多相同的问题,但我不理解给出的解决方案,它不是作为答案给出的,只有在评论中。因此需要一个新的问题。

我按照这个指南创建了一个新的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上使用行名来匹配行的完整外部连接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)

基于nicola上面的评论。这里的解决方案是避免在函数名中使用点。因此,将函数重命名为merge_datasets(),然后它就可以工作了。

最新更新