Suppress other packages' startup messages OR fix my imports - r

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

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

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)

Purging previous versions of functions when developing packages in R

I am developing a package in R. Let's call it mypkg.
Because some functions behave differently when they are run from a package (I am not sure why--but this is not the question), I am editing functions in the package, then rebuilding the package from the command line. For some reason a given R instance retains older versions of the functions even though the source has been changed and the package rebuilt and re-installed. I need to start a new instance to see the changes.
Here is the typical workflow.
Make changes to myfunction() in mypkg.R
In R: detach(package:mypkg); remove.packages("mypkg")
Command line: R CMD INSTALL --build c:\mypkg
Notifies me that it has been installed to the default library
In R: library(mypkg)
In R: myfunction() runs the previous version before changes.
[The next three steps I want to avoid]
Start a new R instance
In R: library(mypkg)
myfunction() works as expected
Run under R.2.14.1.
I am looking for suggestions of how to improve this workflow to avoid starting a new R instance.
You need to try to unload the package as well as detach it. ?detach has:
If a package has a namespace, detaching it does not by default
unload the namespace (and may not even with ‘unload=TRUE’), and
detaching will not in general unload any dynamically loaded
compiled code (DLLs). Further, registered S3 methods from the
namespace will not be removed. If you use ‘library’ on a package
whose namespace is loaded, it attaches the exports of the already
loaded namespace. So detaching and re-attaching a package may not
refresh some or all components of the package, and is inadvisable.
Note the last sentence in particular.
Try:
detach(package:mypkg, unload = TRUE)
Note that the "If a package has a namespace" now means all packages as this was changed in R 2.14.0 (IIRC the version number)
If the code you are changing is R code, consider using assignInNamespace() to assign an object/function in your global workspace (i.e. the newer version of function in mypkg) to the namespace of mypkg. For example we have function foo() in mypkg and locally we have newfoo() which is the newer version of foo():
assignInNamespace("foo", newfoo, "mypkg")
If the changes relate to C code, or the above don't work, then perhaps you should follow the advice of R Core and spawn a new R instance instead of trying to detach your package.
See also the devtools package of Hadley Wickham and Emacs+ESS might make the development process easier (spawning new R instances etc) if you speak Emacs.

Resources