[R-pkg-devel] Assignments to the Global environment

Duncan Murdoch murdoch.duncan at gmail.com
Sun Jan 7 18:37:46 CET 2018


On 07/01/2018 12:17 PM, William Dunlap wrote:
> I think that assigning something to parent.frame() is bad practice, for the
> same reasons that assigning to .GlobalEnv is bad.  You could instead make
> an environment in your package called, say, "TSEtools.env", with
>      TSETools.env <- new.env()
> in some *.R file in the package's R directory.  Export it via NAMESPACE.
>   Then use
> TSETools.env as the default value of assign.env().

Yes, that's one way to do what Saeb wants.

Another way is to define the relevant functions as local functions, and 
do the assignment in their environment.  For example,

fns <- local({
   s <- NULL
   saveS <- function( newSval ) {
     s <<- newSval
   }
   showS <- function() s
   list(saveS, showS)
})
saveS <- fns[[1]]
showS <- fns[[2]]

saveS(123)
showS()

which will return the saved value at the end.

As a general principle, writing into the global environment should 
almost never be allowed.

Duncan
> 
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Sun, Jan 7, 2018 at 6:41 AM, Hugh Parsonage <hugh.parsonage at gmail.com>
> wrote:
> 
>> I'm not CRAN, but something like this might be permissible while
>> satisfying your requirements.
>>
>>     z <- function(..., assign.env = parent.frame(1))
>> assign(as.character(s), temp3, envir = assign.env)
>>
>> The problem with assigning to the global environment is that z might
>> be called where it is expected to only have a local effect. If users
>> really want to assign to the global environment, providing an option
>> might be appropriate.
>>
>>     z <- function(y, s, assign.env = getOption("TSEtools.z.env",
>> parent.frame(1))) assign(as.character(s), temp3, envir = assign.env)
>>
>>     options("TSEtools.z.env" = .GlobalEnv)
>>
>>
>> On Sun, 7 Jan 2018 at 23:21 Saeb <ali.saeb at gmail.com> wrote:
>>>
>>> The function downloads the list of index's value and assigned them to
>>> the individual's name correspond with the indexes. If remove the
>>> .GlobalEnv, then we can not return the values in output.
>>>
>>> Since, the data is updated daily, I think that the storage on device is
>>> not user friendly enough.
>>>
>>> I already used the following code with same R report:
>>>
>>> assign(as.character(s), temp3, envir = .GlobalEnv)
>>>
>>> |
>>> |
>>>
>>>
>>> On 01/07/2018 01:30 AM, Uwe Ligges wrote:
>>>> Let me add: Frequently you can use storage in an enmvironment in yur
>>>> package, if that helps to avoid assigning into .GlobalEnv.
>>>>
>>>> Best,
>>>> Uwe Ligges
>>>>
>>>> On 06.01.2018 22:07, peter dalgaard wrote:
>>>>> You probably need to tell us what you are trying to achieve. Why do
>>>>> you want to assign temp3 to a variable with its name in s into the
>>>>> global environment? Not doing that would clearly eliminate the Note:,
>>>>> but presumably it has a function. However, writing to the global
>>>>> environment, especially to variables with arbitrary names, is
>>>>> potentially antisocial behaviour, since it may overwrite user
>> variables.
>>>>>
>>>>> Incidentally, why do you write .GlobalEnv as as.environment(1)? Is is
>>>>> as intended?
>>>>>
>>>>> -pd
>>>>>
>>>>>> On 6 Jan 2018, at 20:36 , Saeb <ali.saeb at gmail.com> wrote:
>>>>>>
>>>>>> * checking R code for possible problems ... [4s] NOTE
>>>>>> Found the following assignments to the global environment:
>>>>>> File 'TSEtools/R/getTSE.R':
>>>>>> assign(as.character(s), temp3, envir = as.environment(1))
>>>>>>
>>>>>> Please let me know, how can I eliminate this problem? I didn't find
>> out
>>>>>> any good information on websites!
>>>>>
>>>
>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-package-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>
>> ______________________________________________
>> R-package-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-package-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>



More information about the R-package-devel mailing list