Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data inside a function (package creation)

Tags:

r

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?

like image 486
Tyler Rinker Avatar asked May 08 '12 04:05

Tyler Rinker


People also ask

What is a function package?

Function packages are basically several external functions and subroutines that are grouped or packaged together.

How do you create a package in R?

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.

What is data function R?

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

How to create a new package specification?

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.

What is package data in Python?

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.

How to create a package in Python for machine learning?

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.

What is package body in PL/SQL?

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.


2 Answers

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:

  1. 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")
    }
    
  2. 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

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

like image 146
Henrik Avatar answered Sep 30 '22 17:09

Henrik


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

like image 29
nachti Avatar answered Sep 30 '22 19:09

nachti