How to build an R package base on other package? [duplicate] - r

This question already has answers here:
Make third party library available in my R package [duplicate]
(2 answers)
Closed 4 years ago.
I am trying to build an R package. But now my function has used some function from package stringr. Could anyone tell me how can I add stringr in to my package? Thank you.

The function you have used should be added to roxygen documentation. For example, if you used the function stringr::str_extract(), you can add-
#' #importFrom stringr str_extract()
And then run roxygen2::roxygenise() and you should see this function added to your NAMESPACE file. You can read about this in Hadley Wickham's (freely available book on how to make R packages): r-pkgs.had.co.nz/namespace.html
Or see this playlist:
https://www.youtube.com/playlist?list=PLk3B5c8iCV-T4LM0mwEyWIunIunLyEjqM

Related

What alternative of `spread` function and tidyr package should I use? [duplicate]

This question already has answers here:
Error: could not find function ... in R
(10 answers)
Closed 1 year ago.
I want to use spread function from tidyr package but my RStudio says
"Error in spread(., nama_event, created_at) :
could not find function spread"
Therefore, I want to install tidyr package but it doesn't exist in RStudio 1.4.1.
Instead of using spread you should use pivot_longer.
https://tidyr.tidyverse.org/articles/pivot.html
To install a package, you shoud use install.packages.

Package dependencies for R base packages [duplicate]

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.

Is there a command in R to view all the functions present in a package? [duplicate]

This question already has answers here:
How to find all functions in an R package?
(6 answers)
Closed 7 years ago.
I would like to know if there is a command, using which one can view all the functions that are built into an R package.
For example, let's say I loaded a package into environment:
require(dplyr)
Now, I would like to get a list of all the functions present in the dplyr package.
Is there any way to get such a list?
You can use lsf.str.
For instance:
lsf.str("package:dplyr")
To list all objects in the package use ls
ls("package:dplyr")
Note that the package must be attached.
To see the list of currently loaded packages use
search()
Alternatively calling the help would also do, even if the package is not attached:
help(package = dplyr)
Finally, you can use RStudio which provides an autocomplete function. So, for instance, typing dplyr:: in the console or while editing a file will result in a popup list of all dplyr functions/objects.

Can I generate comments using roxygen2 without building an R package? [duplicate]

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.

Make third party library available in my R package [duplicate]

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

Resources