If I need to use a data set inside a function (as a lookup table) inside of a package I'm creating do I need to explicitly load the data set inside of the function?
The function and the data set are both part of my package.
Is this the correct way to use that data set inside the function:
foo <- function(x){
x <- dataset_in_question
}
or is this better:
foo <- function(x){
x <- data(dataset_in_question)
}
or is there some approach I'm not thinking of that's correct?
Function packages are basically several external functions and subroutines that are grouped or packaged together.
Open a new project in RStudio. Go to the 'File' menu and click on 'New Project. ' Then select 'New Directory,' and 'R Package' to create a new R package.
To see the list of available datasets, use data() function. All available datasets in R can be accessed by their explicit names. This function also helps to access build-in datasets from other R packages (special module-based prebuild datasets).
Below is an example of creating a new package specification called personnel. The personnel package contains two functions: get_fullname () and get_salary () based on employee’s ID. PL/SQL package body contains all the code that implements stored functions, procedures, and cursors listed in the package specification.
Those variables are known as package data. The scope of package data is global to applications. It is recommended that you should hide as much as package data as possible and use get and set functions to read and write that data. By doing this, you can prevent your package data changed unintentionally.
And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course To tell Python that a particular directory is a package, we create a file named __init__.py inside it and then it is considered as a package and we may create other modules and sub-packages within it.
PL/SQL package body contains all the code that implements stored functions, procedures, and cursors listed in the package specification. The following illustrates the syntax of creating package body: CREATE [ OR REPLACE] PACKAGE BODY package_name { IS | AS }. [definitions of private TYPEs.
There was a recent discussion about this topic (in the context of package development) on R-devel, numerous points of which are relevant to this question:
If only the options you provide are applicable to your example R himself (i.e., Brian Ripley) tells you to do:
foo <- function(x){
data("dataset_in_question")
}
This approach will however throw a NOTE in R CMD check which can be avoided in upcoming versions of R (or currently R devel) by using the globalVariables()
function, added by John Chambers
The 'correct' approach (i.e., the one advocated by Brian Ripley and Peter Dalgaard) would be to use the LazyData option for your package. See this section of "Writing R Extensions".
Btw: I do not fully understand how your first approach should work. What should x <- dataset_in_question
do? Is dataset_in_question
a global Variable or defined previously?
For me it was necessary to use get()
additionally to LazyData: true
in DESCRIPTION
file (see postig by @Henrik point 3) to get rid of the NOTE no visible binding for global variable ...
. My R version is 3.2.3
.
foo <- function(x){
get("dataset_in_question")
}
So LazyData makes dataset_in_question
directly accessible (without using data("dataset_in_question", envir = environment())
) and get()
is to satisfy R CMD check
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With