函数中的环境变量 [英] Environment variables within a function

查看:133
本文介绍了函数中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在所使用的函数中获得一致的结果.但是,由于数组不记得时区信息,所以这比我预期的要难一些.

I am trying to have consistent results within a function that I am using. However, since an array does not remember timezone info, this is a little harder than I expected.

> Sys.setenv(TZ = "")
> ISOdate(2015,1,1,1,tz='UTC')
[1] "2015-01-01 01:00:00 UTC"
> c(ISOdate(2015,1,1,1,tz='UTC'))
[1] "2015-01-01 02:00:00 CET"
> tz(c(ISOdate(2015,1,1,1,tz='UTC')))
[1] ""

如您所见,该数组将删除时区信息.这很烦人,因为诸如lubridate的day()之类的其他函数会根据此时区信息更改行为.

As you can see, the array drops the timezone information. This is annoying since other functions like day() of lubridate change behaviour depending on this timezone information.

因此,我尝试了以下实验:

Therefore I tried the following experiment:

> Sys.setenv(TZ = "")
> Sys.getenv('TZ')
[1] ""
> x <- function(){
      used_timezone <- Sys.getenv('TZ')
      Sys.setenv(TZ = "UTC")
      return(5)
      Sys.setenv(TZ = used_timezone)
  }
> Sys.getenv('TZ')
[1] ""
> x()
[1] 5
> Sys.getenv('TZ')
[1] "UTC"

结果是,仅当您在return语句之前重设时区时,它才有效.

Turns out, it only works if you reset the timezone before the return statement.

是否有一种快速的方法可以仅在函数中设置环境变量,而无需读取当前变量并在每次返回之前将其重置?

Is there a quick way to set an environment variable only within a function without reading the current one and resetting it just before every return?

推荐答案

我认为您需要阅读一些R入门资料以及所使用功能的帮助.

I think you need to read some introductory R material and the help on the functions you are using.

ISOdate()不使用环境变量"TZ"来选择时区:

ISOdate() does not use the environment variable 'TZ' to choose time zone:

> Sys.getenv('TZ')
[1] ""
> Sys.timezone(location=FALSE)
[1] "BST"
> ISOdate(2015, 1, 1, 1)
[1] "2015-01-01 01:00:00 GMT"

ISOdate()产生具有属性tzone的日期时间对象:

ISOdate() produces a date-time object with an attribute tzone:

> attributes(ISOdate(2015,1,1,1))
$class
[1] "POSIXct" "POSIXt" 

$tzone
[1] "GMT"

我不认识tz()功能,我不认为它在base中.

I don't recognize the tz() function, I don't think it is in base.

在R中,向量(例如1:3)不是数组:

In R a vector (e.g. 1:3) is not an array:

> is.array(1:3)
[1] FALSE

c()组合了其参数,但是(从帮助中)有时由于删除名称以外的属性而产生副作用".通过将ISOdate()包裹在c()中,您已删除了时区信息.

c() combines its arguments but (from the help) "is sometimes used for its side effect of removing attributes except names". By wrapping ISOdate() in c() you have removed the time zone information.

如果您想要一个日期向量,则可以使用c()笨拙地创建向量,然后再次将属性放回去:

If you want a vector of dates then you could either, clumsily use c() to create the vector then put the attribute back again:

> aDate <- ISOdate(2015,1,1,1, tz="cet")
> aZone <- attr(aDate, "tzone")
> aObj <- c(aDate)
> aObj 
[1] "2015-01-01 GMT"
> attr(aObj, "tzone") <- aZone
> aObj
[1] "2015-01-01 01:00:00 CET"

...或更好,请使用ISOdate()直接从参数向量生成日期对象向量:

... or better, use ISOdate() to produce a vector of date objects directly from vector(s) of arguments:

> ISOdate(2015, 1, 1:3, tz='cet')
[1] "2015-01-01 12:00:00 CET" "2015-01-02 12:00:00 CET"
[3] "2015-01-03 12:00:00 CET"

这篇关于函数中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆