简体   繁体   English

R:安装自定义软件包,可以很好地加载,但是功能不存在

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

There is a previous question here with much the same issue, but I don't understand the solution given and it is not given as an answer, only in the comments. 这里有一个先前的问题,也有几乎相同的问题,但是我不理解给出的解决方案,也没有给出答案,只能在评论中给出。 Hence the need for a new question. 因此,需要一个新的问题。

I have followed this guide to create a new R package. 我已经按照本指南创建了一个新的R包。

To create, install and load it, I have used the following code: 要创建,安装和加载它,我使用了以下代码:

#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)

I have put one file, functions.R , in the R folder. 我在R文件夹中放了一个文件functions.R The reason to call it that is that I want to put all my functions in one file, rather than separate files as the guide suggests. 之所以这样称呼它,是因为我想将所有函数放在一个文件中,而不是按照指南的建议放在单独的文件中。

It reads: 内容为:

# 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)
}

The function merges two data.frames by their rows. 该函数按行合并两个data.frame。 This function is basically the same as the full outer join sql command on data.frames using the rownames to match rows. 此功能与使用行名匹配行的data.frames上的完全外部join sql命令基本相同。

It loads fine and installs fine. 它可以很好地加载和安装。 It shows up as a package in RStudio. 它在RStudio中显示为软件包。 The documentation is there too. 文档也在那里。 However, calling the function just gives: 但是,调用该函数仅给出:

#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)

However, this just gives: 但是,这仅给出:

> 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'

What is wrong? 怎么了? R has somehow loaded the documentation, but not the function. R以某种方式加载了文档,但未加载功能。 Or it is hidden somewhere in the wrong namespace. 还是将其隐藏在错误的命名空间中。

I looked in the namespace file as mentioned in the comments in the other question, but it did not say anything useful to me: 我查看了另一个问题的注释中提到的名称空间文件,但是它没有对我有用的任何信息:

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

S3method(merge,datasets)

Based on nicola's comment above. 基于上述尼古拉的评论。 The solution here is to avoid using dots in the function names. 解决方案是避免在函数名称中使用点。 So rename the function to merge_datasets(), then it works. 因此,将函数重命名为merge_datasets(),然后它就可以工作了。

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

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