Load my own R package - r

I made an R package for personal use, but the way I load it is by individual files. Such as:
source("../compr/R/compr.R")
source("../compr/R/error_df.R")
source("../compr/R/rmse.R")
I would like to load the entire package, which is called compr, as I would other libraries.

If you are using RStudio, I would suggest creating a project and setting it to your compr directory. After that you will be able to use devtools::load_all() to load your package directly.
If you don't want to do this, or you don't use RStudio devtools::load_all('path/to/compr') will also work.
P.S. compr directory needs to be the root of the package i.e. the place where your DESCRIPTION file is.

Related

Installation Failed during intall_github with wilcox_R package

I am trying to use the zdepth R function from the package wilcox_R available at github. As suggested, I used the usual command lines:
library("devtools"); install_github("musto101/wilcox_R")
However, I received the following error:
Any way out of this would be of great help.
There are problems with the package metadata that are preventing it from building, probably because the author hasn't finished developing the package yet.
Probably the easiest way to use the zdepth function is to download the repository source code. Open the repository in github (https://github.com/musto101/wilcox_R) then click Clone or Download and Download Zip.
Unzip the archive and the zdepth function looks like it's in R/zdepth.R. Copy this file to your own project then load the function with source(), e.g.: source("R/zdepth.R").
This is not the 'best' way to use this function, particularly if you want to use other functions in the package, but short of fixing the metadata yourself it's the most straightforward.

Unable to generate help files from R package

I have created a package in R. It is all fully documented and written according to R package guidelines. I have used devtools to generate documentation.
document("/home/rstudio/EndoMineR/")
However when I try to use ?EndoMineR I get the error:
No documentation for ‘EndoMineR’ in specified packages and libraries:
you could try ‘??EndoMineR’
How can I create the help files for my package? What am I likely to be missing?
As additional information, when I click the package name in R studio I get the help files but not if I try ?EndoMineR. Also the .Rd files in the man directory (which I think is what devtools::document() generates) seem to be updating just fine. I assume the ?EndoMiner accesses the man files so I'm not sure why this folder is not accessible (it is top level)

How to include bash scripts in a package?

I need to include several bash scripts in the R package I'm writing. I'd love to distribute them together with the package, so when a user installs the package via devtools::install_github(...) he/she gets the scripts as well.
I know it is possible, but I don't know how. Including the files in the scripts subdirectory doesn't seem to suffice. I need a means to tell R (or RStudio) to include them.
I use RStudio for development, so I would appreciate a solution that integrates with the "Build package" functionality that RStudio provides.
Simply add whatever you want to the inst/xxx folder in your package.
The folder will get installed as xxx when you compile/publish the package as a library.
You access the files via system.file(), e.g.
system.file('scripts/peak_mem.sh', package='clustertools')
See more details on the R packages by Hadley Wickham
Thank you #Axeman!

R how to use files in the same packages

I am planing to do all my R scripts in the same package in order to move it easily between my friends.
What I have done is created an R packages using R studio and the following files have been automatically generated:
projectName.Rproj
DESCRIPTION
man
NAMESPACE
R
Read-and-delete-me
I created a new R script and I save it in R folder. Now I want to add a new R script that uses functions that have been defined in the first script.
I created this new script and I tried to use on of the functions that are located in the other script. I got error that the function is not defined.
What I tried to solve the problem
I used the source command in the beginning of the new script like this:
source('something.R')
I got error message that something.R doesn't exist.
What is the solution please to include functions that exist in a different scripts ** but in the same packages**?
NoteI don't want to use relative paths because I want the package to be as portable as possible
Thanks a lot
You appear to misunderstand how package works: by having a file with a function in the R/ directory, it is already visible to other code in the package.
If you want to make it available to other packages, you can control this via the NAMESPACE file. All this is well-documented in Writing R Extensions which comes with your copy of R, and a number of additional books and tutorials you could peruse.

creating package

i am creating a package in R language, everything is running properly, but when i run R CMD check , it shows an error message while running examples.. i.e.
"can't open the file." "No such file or directory"
actually my function needs a PubMed text file containing abstracts from the PubMed, i have placed my text file in every sub-directory of my package, but its not working. showing same error again and again.
so please suggest me the right way how to put a text file in a package which can be used by examples to run properly.
i will be very thankful to you.
Usually you put such data in the /inst folder. E.g.:
<packageRoot>/inst/pubmed/myfile
After the package is build you can access the content of this folder from within the package like this:
system.file( "pubmed/myfile", package="<package>" )
See for more information http://cran.r-project.org/doc/manuals/r-release/R-exts.pdf (1.1.5 Data in packages).
I suggest you to use devtools and roxygen2 packages. Basically, you just need to prepare description and .R files.
see more details in this brilliant answer :devtools roxygen package creation and rd documentation

Resources