Problems with R package dependencies - r

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)

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.

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.

R alternative to install.packages() function

Is there any documentation on manually installing a package in a user library when the R.home() path is locked down and incomplete (no etc, no bin, just library?) The system does NOT support shelling out to execute R CMD, which I believe standard R does.
I would like to build existing source packages (from CRAN) and install into a user library directory, so that I can use the library() function and get all the usual namespace and *.Rdx and *.Rdb files.
At the moment, I'm plodding through install.packages, tools::.build_package, and tools:::.install.packages source, using a standard MacOS R and the r source. Hopefully this has been documented in a more user-friendly fashion and my google searches have missed it.
Thanks.
You don't need to use a different install.packages method, rather you only need to specify a writable location for storing packages and give it precedence over the system default one. A simple way to accomplish this is to set an R_LIBS environment variable. For instance, in my .bashrc I have
export R_LIBS='/home/username/.local/lib/R-3.3.3'
Then, by default, all packages are installed here. Further, packages installed both here and the system-wide location will give priority to the ones here when loading.
You can verify that the location is being used by checking .libPaths() in your R session.

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.

Suppress other packages' startup messages OR fix my imports

I am about to release an R package, but I am having trouble with the messages that are printed to the R console when the package is loaded in R. My package depends on several other packages, and these other packages display various welcome and startup messages. This is because I added the packages to the Depends field in the DESCRIPTION file rather than the Imports field. My understanding is that using the Imports field would suppress the startup messages. However, if I import the packages rather than depend on them, my examples in the help files no longer work because some functions in these packages, which are used by other functions in the same packages which are in turn used by functions I am using in my package code, cannot be found. What can I do to suppress the dozens of lines of messages without importing them (that is, by having them in the Depends field)? Or, alternatively, how can I make sure the functions are all available when I import them? Does the problem occur because the functions which are indirectly needed are not exported by the package in question? It does not seem to work even if I import the problematic functions explicitly...
Edit - more details: A function in my package calls the ergmMPLE() function in the ergm package. So I have added Imports: ergm to the DESCRIPTION file and import("ergm") to the NAMESPACE file. When I use the function in my package, I get an error message that the function check.control.class could not be found by the ergmMPLE function. So I looked up this function and it appears to be located in yet another package called statnet.common. Therefore I added statnet.common to the Imports field in the DESCRIPTION file as well and added import("statnet.common") to the NAMESPACE file. Now it looks like the ergmMPLE function still does not find the check.control.class function. My solution would be to just let my package depend on the statnet.common package, but then I have the problem with the startup messages again...
You can put the packages in the Import part and load the necessary functions from the other packages using import or importForm. For more details see:
R suppress startupMessages from dependency

Resources