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.
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. 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. This function is basically the same as the full outer join sql command on data.frames using the rownames to match rows.
It loads fine and installs fine. It shows up as a package in 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. 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.