R how to use files in the same packages - r

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.

Related

Error when trying to install package from GitHub

I am trying to install a package from my own repository in order to test if the functions work. The link to the repository is here: https://github.com/hharder74/SampleMeansII. When I try to install it using the following code:
devtools::install_github("hharder74/SampleMeansII")
I get the following error:
Error: Failed to install 'unknown package' from GitHub: HTTP error 404. Not Found Did you spell the repo owner (`hharder74`) and repo name (`SampleMeansII`) correctly? - If spelling is correct, check that you have the required permissions to access the repo.
I am really confused on where this error is coming from. This is my first time trying to upload a package to GitHub, and I just wanted to check if the package can be installed before I turned it in to my teacher. Here is a bit of code to test the functions if anyone needs it:
vec <- runif(100)
sample_mean(vec, 50)
many_sample_means(vec, reps = 10, n = 50)
sample_means_ns(vec, reps = 10, ns = c(5, 50, 500))
You have not yet created a package. You've just created some files with R code in them. An R package has a very particular structure that includes things like a DESCRIPTION file and a NAMESPACE file. In theory you could create these yourself, but often it's easier to use things like devtools::create and roxygen to create them for you. Or if you are using RStudio, you can create a new R Package project with default versions of these files.
To add a DESCRIPTION File, try running
usethis::use_description()
That will fill in defaults you can change.
Then you will need to create a NAMESPACE file. If you just want to make all the functions you define inside your R files to be available outside the pacakge, you can just put
exportPattern("^[[:alpha:]]+")
in that file and that should work.
You might also consider following guides like thoses http://r-pkgs.had.co.nz/package.html or https://swcarpentry.github.io/r-novice-inflammation/08-making-packages-R/ for a better overview on creating a package.
Once your repo looks like a proper R package, then you can use devtools::install_github to install it.
Note that github can be useful for tracking changes to any types of files. You may perform an analysis in an R script that you would like to track changes for and save that on github but it may not make sense to turn that analysis script into a packages. You generally make packages when you want to reuse functions or data across different projects then those projects can install and load your package. So not all R code lives inside an R package, but devtools::install_github can only be used to install actual packages.

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)

Load my own R package

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.

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

Problems with R package dependencies

I created a n R package which has certain dependencies on other packages.
So i specified the package name as Imports and Depends in the Description file.
In the namespace file I specified that the package should be exported.
I checked the writing R extensions manual and there doesn't seem to be anything else that I should add.
When I try running the package it is not able to call the functions on which the package depends.Do I need to specify additional parameters to be able to call those functions.
I am using R version 2.15.0
Please help me out with this problem.
Thanks.
NAMESPACE:
import("RHive")
export("bigEDA")
export("procFreq")
export("procUnivariate")
I want all the functions from RHive to be present in the same environment as my package.
Just not able to do that.
I have tried specifying export("RHive") and exportFrom("RHive","rhive.query")
This also doesn't work. :(
Did you run R CMD check NAMEofYOURpackage in your terminal or shell to test whether your DESCRIPTION and NAMESPACE file are fine? This check routine creates the file 00install.out that can give you a hint where something went wrong.
If you just want to use functions from foreign packages in your package use the DESCRIPTION file. There you just type: Depends: RHive
Use the export in the NAMESPACE file to export your functions to the user. If you have a function sum() and you want the user who installed your package to be able to use this function use: exportMethods(sum) (you can add more functions right here by separating them with commas)

Resources