[R-pkg-devel] Assigning a variable to global environment
Avi Gross
@v|gro@@ @end|ng |rom ver|zon@net
Fri Dec 11 21:02:40 CET 2020
Tiago, this may be a dumb question but what you are trying to do (below) sounds very similar to an existing function called attach() which many suggest has some dangers in use as you can overwrite or hide user variables or it can be eclipsed by later code:
https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/attach
Your version does try to use a secondary name if there is a clash but note if it is called repeatedly, it will make a tertiary and quaternary and beyond name by appending multiple copies of "_vct" one after another. I have seen people do this when making a unique tmp file.
And you do not show if these variable stuffed into an environment are eventually removed. Consider the variant described here:
https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/with
For clarity, you would use attach or with using a subset the columns of your data.frame that had just the variables you want.
A simple question is what the purpose of your change is. You want other functions run later to see these variables in their environment. Must they be global or can they be in an environment high enough in their search path. Some methods can make the change temporary and will unwind them. Some may keep the change permanent till the end of the session.
I find that although it is tempting in base R to shorten use of names like $df$var_name to just var_name, use of a tidyverse package gets around it in a more limited way that does not so much impact what other functions see in the environment.
-----Original Message-----
From: R-package-devel <r-package-devel-bounces using r-project.org> On Behalf Of Tiago Olivoto
Sent: Friday, December 11, 2020 2:12 PM
To: Ben Bolker <bbolker using gmail.com>
Cc: R Package Devel <r-package-devel using r-project.org>
Subject: Re: [R-pkg-devel] Assigning a variable to global environment
Thanks, everyone,
Following Mark suggestion:
The problem I'm trying to solve is:
Users of my package metan (https://CRAN.R-project.org/package=metan),
sometimes need to extract 2-3 variables from a given data.frame and put them as vectors in the global environment to use in other package's functions.
Given that df has the columns, ENV, GEN, REP, I would need to run the following codes ENV <- df$ENV GEN <- df$GEN REP <- df$GEN I'm looking for a more efficient way to do that and just finished the following function
as_vector <- function(.data, ...){
if(missing(...)){
df <- select(.data, everything())
} else{
df <- select(.data, ...)
}
for(i in 1:ncol(df)){
var_name <- names(df[i])
var_name <- ifelse(exists(var_name, envir = .GlobalEnv),
paste(var_name, "_vct", sep = ""),
var_name)
assign(var_name, as.vector(df[[i]]), envir = .GlobalEnv)
}
}
Then, users could simply run as_vector(df) or as_vector(df, GEN, ENV) But I'm not sure if this fits with the CRAN policies.
Cherss,
Tiago
Em sex., 11 de dez. de 2020 às 16:03, Ben Bolker <bbolker using gmail.com>
escreveu:
> I think this solution will *not* be acceptable to CRAN. (The policy
> doesn't say "don't modify the global workspace unless you're careful",
> it says "don't modify the global workspace".) You can ask for an
> exception, but your chances of success are very low.
>
> It would be best to find a way to solve your problem without
> assigning to the global workspace. Can you assign to an environment
> that is contained within your package, which all of the functions in
> your package will also have access to?
>
> cheers
> Ben Bolker
>
> On 12/11/20 1:52 PM, Tiago Olivoto wrote:
> > Thank Matt for your response.
> > Would be an acceptable solution to check if the variable exists
> > first and creating the new variable with a suffix? Please, see the
> > following
> example
> >
> > var_name <- "name"
> > var_name <- ifelse(exists(var_name),
> > paste(var_name, "_vct", sep = ""),
> > var_name)
> > and then
> >
> > assign(var_name, 1, envir = .GlobalEnv)
> >
> > Cheers,
> > Tiago
> >
> > Em sex., 11 de dez. de 2020 às 15:20, Matt Denwood <md using sund.ku.dk>
> escreveu:
> >
> >>
> >>> On 11 Dec 2020, at 18:32, Tiago Olivoto <tiagoolivoto using gmail.com>
> wrote:
> >>>
> >>> Hi everyone,
> >>> This can be a very simple question, but I really have the
> >>> following
> >> doubt.
> >>>
> >>> CRAN Policies says that 'Packages should not modify the global
> >> environment
> >>> (user’s workspace)'. So, may I or may I not create a function that
> >>> will call internally the following code? (a simple example)
> >>>
> >>> assign("name", 1, envir = .GlobalEnv)
> >>
> >> You may not - and for good reason, as the user may have an existing
> >> variable called “name” that will be destroyed by such an action.
> >> If you run "R CMD check —as-cran" on a package containing this code
> >> you will
> get a
> >> warning (or maybe a note, I can’t remember), and the package would
> >> most likely be refused if you tried to submit it to CRAN.
> >>
> >> Cheers,
> >>
> >> Matt
> >>
> >>
> >>>
> >>> Cheers
> >>> Tiago
> >>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-package-devel using r-project.org mailing list
> >>>
> >>
> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-package-devel&data=04%7C01%7Cmd%
> 40sund.ku.dk%7C42392098d3324de389b008d89dfad39d%7Ca3927f91cda14696af89
> 8c9f1ceffa91%7C0%7C0%7C637433048371869663%7CUnknown%7CTWFpbGZsb3d8eyJW
> IjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&
> amp;sdata=NFTbsJDlZNQWajN%2F%2BeftuclrkJSGtTnM9kEtobn9VLs%3D&reser
> ved=0
> >>
> >>
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-package-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-package-devel
> >
>
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
[[alternative HTML version deleted]]
______________________________________________
R-package-devel using r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Scanned by McAfee and confirmed virus-free.
Find out more here: https://bit.ly/2zCJMrO
More information about the R-package-devel
mailing list