Making data visible in packages (no visible binding for global variable ...)
3
0
Entering edit mode
Tarca, Adi ▴ 570
@tarca-adi-1500
Last seen 13 months ago
United States
Hi all, I am trying to write a package that contains one function MYF which requires some data (e.g. a matrix X). I put the matrix X in the \data folder as a myX.Rdata file. The function MYF makes a data(myX) call to make X available within MYF before doing some computations. The resulting package gets installed and runs fine but when I do the R CMD CHECK I get "no visible binding for global variable X". I have two questions: A) is there a better way to make X visible within MYF rather than making a data() call B) wht should be done to fix the "no visible binding for global variable". Thanks, Adi Tarca
• 4.9k views
ADD COMMENT
1
Entering edit mode
kmezhoud ▴ 10
@kmezhoud-6841
Last seen 5.7 years ago
Tunisia

Dear Dr Morgan,

Can I use your code as function to convert temporal variables to global as:

Dear All,
 

I am writing a GUIpackage that needs global variables.
I had many warning message when I checked the code as for example:
geteSet: no visible binding for global variable ‘curselectCases’
I would like to write a function that creates a global place for Objects to be loaded as:


Fun <- function(){

Object <- 5

Var2Global <- function(Object){
.myDataEnv <- new.env(parent=emptyenv()) # not exported
isLoaded <- function(Object) {
    exists(Object, .myDataEnv)
}
getData <- function(Object) {
    if (!isLoaded(Object)) data(Object, envir=.myDataEnv)
    .myDataEnv[[Object]]
}
}

}

To avoid the use of:  Object <<- 5

but it seems not working yet. Object == 5 is not a global variable after running Fun().
 

Any Idea?

Thanks

Karim

ADD COMMENT
0
Entering edit mode
@martin-morgan-1513
Last seen 5 months ago
United States
"Tarca, Adi" <atarca at="" med.wayne.edu=""> writes: > Hi all, > I am trying to write a package that contains one function MYF which > requires some data (e.g. a matrix X). > I put the matrix X in the \data folder as a myX.Rdata file. The function > MYF makes a data(myX) call to make X available within MYF before doing > some computations. > The resulting package gets installed and runs fine but when I do the R > CMD CHECK I get "no visible binding for global variable X". > I have two questions: > A) is there a better way to make X visible within MYF rather than making > a data() call data() loads the objects in the data file into the global (user) environment, which might not be very polite (e.g., overwriting an existing user object). One option is to create a place for your data to be loaded, e.g., within the function where it will be used, or in your package name space if it will be used repeatedly or if you wish users to access it directly. For instance you might write (this is untested, so hopefully not too misleading) .myDataEnv <- new.env(parent=emptyenv()) # not exported isLoaded <- function(dataset) { exists(dataset, .myDataEnv) } getData <- function(dataset) { if (!isLoaded(dataset)) data(dataset, envir=.myDataEnv) .myDataEnv[[dataset]] } Martin > B) wht should be done to fix the "no visible binding for global > variable". > > Thanks, > > Adi Tarca > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M2 B169 Phone: (206) 667-2793
ADD COMMENT
0
Entering edit mode
Hi Martin, This solved the issue. The code you provided works fine if the data(dataset) call brings up only one variable called dataset also as well but can be easy generalized. Thanks, Adi -----Original Message----- From: Martin Morgan [mailto:mtmorgan@fhcrc.org] Sent: Monday, October 27, 2008 5:59 PM To: Tarca, Adi Cc: bioconductor at stat.math.ethz.ch Subject: Re: [BioC] Making data visible in packages (no visible binding for global variable ...) "Tarca, Adi" <atarca at="" med.wayne.edu=""> writes: > Hi all, > I am trying to write a package that contains one function MYF which > requires some data (e.g. a matrix X). > I put the matrix X in the \data folder as a myX.Rdata file. The > function MYF makes a data(myX) call to make X available within MYF > before doing some computations. > The resulting package gets installed and runs fine but when I do the R > CMD CHECK I get "no visible binding for global variable X". > I have two questions: > A) is there a better way to make X visible within MYF rather than > making a data() call data() loads the objects in the data file into the global (user) environment, which might not be very polite (e.g., overwriting an existing user object). One option is to create a place for your data to be loaded, e.g., within the function where it will be used, or in your package name space if it will be used repeatedly or if you wish users to access it directly. For instance you might write (this is untested, so hopefully not too misleading) .myDataEnv <- new.env(parent=emptyenv()) # not exported isLoaded <- function(dataset) { exists(dataset, .myDataEnv) } getData <- function(dataset) { if (!isLoaded(dataset)) data(dataset, envir=.myDataEnv) .myDataEnv[[dataset]] } Martin > B) wht should be done to fix the "no visible binding for global > variable". > > Thanks, > > Adi Tarca > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor -- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M2 B169 Phone: (206) 667-2793
ADD REPLY
0
Entering edit mode
@richard-pearson-1304
Last seen 10.3 years ago
Adi A rather inelegant way to avoid the "no visible binding" message is: X <- NULL data(myX) Regards Richard Tarca, Adi wrote: > Hi all, > I am trying to write a package that contains one function MYF which > requires some data (e.g. a matrix X). > I put the matrix X in the \data folder as a myX.Rdata file. The function > MYF makes a data(myX) call to make X available within MYF before doing > some computations. > The resulting package gets installed and runs fine but when I do the R > CMD CHECK I get "no visible binding for global variable X". > I have two questions: > A) is there a better way to make X visible within MYF rather than making > a data() call > B) wht should be done to fix the "no visible binding for global > variable". > > Thanks, > > Adi Tarca > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor > -- Richard D. Pearson richard.pearson at postgrad.manchester.ac.uk School of Computer Science, http://www.cs.man.ac.uk/~pearsonr University of Manchester, Tel: +44 161 275 6178 Oxford Road, Mob: +44 7971 221181 Manchester M13 9PL, UK. Fax: +44 161 275 6204
ADD COMMENT
0
Entering edit mode
Thanks Richard, It is indeed true that I do not get anymore "no visible binding" message, but the script does not work anymore :). I guess what happens is that, when the function tries to use X, it will get the one defined in the function first (which is NULL)and not look anymore for it the the global environment where the data() call will bring it up and initialize it with the real values. Regards, Adi -----Original Message----- From: Richard Pearson [mailto:richard.pearson@postgrad.manchester.ac.uk] Sent: Tuesday, October 28, 2008 5:38 AM To: Tarca, Adi Cc: bioconductor at stat.math.ethz.ch Subject: Re: [BioC] Making data visible in packages (no visible binding for global variable ...) Adi A rather inelegant way to avoid the "no visible binding" message is: X <- NULL data(myX) Regards Richard Tarca, Adi wrote: > Hi all, > I am trying to write a package that contains one function MYF which > requires some data (e.g. a matrix X). > I put the matrix X in the \data folder as a myX.Rdata file. The > function MYF makes a data(myX) call to make X available within MYF > before doing some computations. > The resulting package gets installed and runs fine but when I do the R > CMD CHECK I get "no visible binding for global variable X". > I have two questions: > A) is there a better way to make X visible within MYF rather than > making a data() call > B) wht should be done to fix the "no visible binding for global > variable". > > Thanks, > > Adi Tarca > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > -- Richard D. Pearson richard.pearson at postgrad.manchester.ac.uk School of Computer Science, http://www.cs.man.ac.uk/~pearsonr University of Manchester, Tel: +44 161 275 6178 Oxford Road, Mob: +44 7971 221181 Manchester M13 9PL, UK. Fax: +44 161 275 6204
ADD REPLY
0
Entering edit mode
On Tue, Oct 28, 2008 at 8:52 AM, Tarca, Adi <atarca at="" med.wayne.edu=""> wrote: > > Thanks Richard, > It is indeed true that I do not get anymore "no visible binding" > message, but the script does not work anymore :). > I guess what happens is that, when the function tries to use X, it will > get the one defined in the function first (which is NULL)and not look > anymore for it the the global environment where the data() call will > bring it up and initialize it with the real values. Easy... X <- NULL; rm(X); data(myX); /Henrik > Regards, > Adi > > > > > > -----Original Message----- > From: Richard Pearson [mailto:richard.pearson at postgrad.manchester.ac.uk] > > Sent: Tuesday, October 28, 2008 5:38 AM > To: Tarca, Adi > Cc: bioconductor at stat.math.ethz.ch > Subject: Re: [BioC] Making data visible in packages (no visible binding > for global variable ...) > > Adi > > A rather inelegant way to avoid the "no visible binding" message is: > > X <- NULL > data(myX) > > Regards > > Richard > > > Tarca, Adi wrote: >> Hi all, >> I am trying to write a package that contains one function MYF which >> requires some data (e.g. a matrix X). >> I put the matrix X in the \data folder as a myX.Rdata file. The >> function MYF makes a data(myX) call to make X available within MYF >> before doing some computations. >> The resulting package gets installed and runs fine but when I do the R > >> CMD CHECK I get "no visible binding for global variable X". >> I have two questions: >> A) is there a better way to make X visible within MYF rather than >> making a data() call >> B) wht should be done to fix the "no visible binding for global >> variable". >> >> Thanks, >> >> Adi Tarca >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at stat.math.ethz.ch >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >> > > -- > Richard D. Pearson richard.pearson at postgrad.manchester.ac.uk > School of Computer Science, http://www.cs.man.ac.uk/~pearsonr > University of Manchester, Tel: +44 161 275 6178 > Oxford Road, Mob: +44 7971 221181 > Manchester M13 9PL, UK. Fax: +44 161 275 6204 > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor >
ADD REPLY

Login before adding your answer.

Traffic: 591 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6