This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Better explanation of when to use Imports/Depends
(4 answers)
Closed 4 years ago.
I am developing an R package that uses third party functions
available in the Bioconductor package "methyilumi"
In the code for my R package at the very beginning I import methylumi
with library(methylumi).
During the development (I use roxygen2 and devtools) everything works fine.
However, when I install the package and run my functions, I get the error:
could not find function "methylumIDAT".
Of course everything is solved if I import the package manually, but how can I make
so that methylumi is available whenever I load my own package?
Since you are using devtools, you can add
devtools::use_package("methyilumi")
in the console, and call methyilumi::methylumIDAT in the body of your function. That way, the package is automatically listed in Imports in the DESCRIPTION file.
This sections gives the instructions for several different cases:
http://r-pkgs.had.co.nz/namespace.html#imports
This is done with the NAMESPACE file and also noted in the DESCRIPTION file. There are a few ways to import a function in NAMESPACE, but the simplest is just importFrom("[PACKAGE_NAME]",[FUNCTION_NAME). Then, in DESCRIPTION, add the package name to imports.
See this very good tutorial from Friedrich Leisch.
http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf
Related
This question already has answers here:
How to use an R script from GitHub?
(4 answers)
Install R-Package from Github
(3 answers)
Closed 2 years ago.
I'm using a package from github and functions in it are not... connected.
The main file has a function but to make that function work i need to run 4 diffrent files (3 files got a lot of functions in them and 1 has class).
Do we have any option to import whole scripts (with all the functions, classes, variables) on top of my main file so they will appear in my enviroment?
for exmaple in file "foo.R" i have a function "food = function(...)" so i do smth like
from foo.R import food
result <- 2*food(a,b,c)
or
from foo.R import *
result <- 2*foo.food(a,b,c)
like in python?
I think you should take a look at the combination of here + source. Basically, what you can do is the following:
library(here)
source(here::here('R', 'file_that_contains_a_lot_of_functions.R'))
In that way, you will get all the functions from file_that_contains_a_lot_of_functions.R in the GlobalEnviroment make it easy to work with them.
PS1: the 'R' above is because I am assuming you downloaded a mirror of the library from Github, and you are taking the functions from the R folder.
PS2: it also assumed that you generated a project at the level of the main folder of the package. What I also do is to generate an extra folder my_folder where I store everything that I create to do not disturb the workflow of the original package.
use the command source() and put in the file to run - then you will have access to all objects within.
source("path/to/your/file.R")
This question already has answers here:
Elegant way to check for missing packages and install them?
(33 answers)
Closed 4 years ago.
The typical way a package developer is advised to check whether a user has installed a package is like this:
if (!requireNamespace("package")) {
stop("Please install package.")
}
requireNamespace loads the package (in the current scope?) and returns a TRUE/FALSE value. I need to check the install state of a package without loading the namespace.
The reason for this is because I am writing a knit_print S3 method (extending the knitr package) and the namespace I am checking for kableExtra has side effects outside of the context of my knit_print method that I want to avoid.
When loaded, kableExtra changes how subsequent calls to knitr::kable are formatted at the global level. It has good reasons for doing so, but I want to use kableExtra inside my S3 method and not have end users confused about why kable behaves differently after my knit_print method is called.
That's why I want to do the check for the namespace (and if kableExtra is not installed, just call knitr::normal_print) without loading the namespace.
Edit: To clarify why I don't think this is a duplicate of this question, those answers do not pay any special attention to whether the solution loads the package when it is installed. It turns out that some of the solutions do not load the package in question, but they are not clearly differentiated.
Use installed.packages.
if ("kableExtra" %in% rownames(installed.packages()) {
# do something
}
This question already has an answer here:
no visible global function definition for ‘median’
(1 answer)
Closed 6 years ago.
When writing a package in R, is it necessary to add base packages (utils, grDevices, stats, etc.) as dependencies in the DESCRIPTION of your package?
Some packages do this, but most seem to not.
I have not found any information in the Writing R Extensions manual regarding this.
You should not set too much dependencies but prefer to use those packages as import :
for instance in the DESCRIPTION you will write
Imports:
graphics,
utils,
stats,
grDevices
In your NAMESPACE you can then use either importFrom, in the case you only have a few functions to use. Then you don't have to point to the function using pkg::fun(), or import pkg which will import the whole package, and again you will not need to use the pkg::fun().
Below an example of what you can write in your NAMESPACE
import(graphics)
importFrom(stats,coef)
importFrom(stats,ftable)
importFrom(stats,na.fail)
importFrom(utils,data)
importFrom(utils,globalVariables)
importFrom(utils,read.csv)
importFrom(utils,select.list)
importFrom(utils,stack)
importFrom(utils,write.table)
If you try to use those functions without importing them or use depends, the R-CMD check will fail.
This question already has answers here:
Possible to create Rd help files for objects not in a package?
(7 answers)
Closed 7 years ago.
I need to generate documentation for a collection of R programs. Unfortunately, building a package based on the source code is not an option (I know how to do that, and I have already experimented with RStudio, roxygen2, and packages, and it works like a charm). Can I use roxygen2 to to generate documentation from the R source code without building a package in a way similar to how doxygen works with C++? If not, are there other options for documenting R code that do not rely on packages?
If your only after the documentation, devtools::document() will build the documentation files without building the package.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to properly use functions from other packages in a R package
load data set automatically
I've built an R package and am now testing it. It requires several packages like scatterplot3d, gdata, etc.
How can I get those packages to load automatically, when someone loads my package?
Thanks!
Edit Re Comments: I've already set imports in the Description and I've set the namespaces file. I know this works because the examples I put in work when I do R CMD Check. However, if I just load up R, type library(mypackage) it gives me an error.
Edit: Solved, I moved the packages I had in the imports section to the depends section. Thank you!