【问题标题】:subsetting data.cube inside custom function自定义函数中的子集 data.cube
【发布时间】:2017-03-02 09:50:37
【问题描述】:

我正在尝试创建一个我自己的函数来对 R 中的 data.cube 进行子集化,并为我打算构建的一些预定义图自动格式化结果。

这是我的功能。

require(data.table)
require(data.cube)

secciona <- function(cubo  = NULL, 
                     fecha_valor = list(), 
                     loc_valor = list(), 
                     prod_valor = list(), 
                     drop = FALSE){

    cubo[fecha_valor, loc_valor, prod_valor, drop = drop]

    ## The line above will really be an asignment of type y <- format(cubo[...drop])
    ## Rest of code which will end up plotting the subset of the function
}

问题是我不断收到错误:Error in eval(expr, envir, enclos) : object 'fecha_valor' not found

对我来说最奇怪的是,在控制台上一切正常,但在我的子集函数中定义时却不行。

在控制台中:

> dc[list(as.Date("2013/01/01"))]
> dc[list(as.Date("2013/01/01")),]
> dc[list(as.Date("2013/01/01")),,]
> dc[list(as.Date("2013/01/01")),list(),list()]

全部给出结果:

<data.cube>
fact:
  5627 rows x 2 dimensions x 1 measures (0.32 MB)
dimensions:
  localizacion : 4 entities x 3 levels (0.01 MB)
  producto : 153994 entities x 3 levels (21.29 MB)
total size: 21.61 MB

但每当我尝试时

secciona(dc)
secciona(dc, fecha_valor = list(as.Date("2013/01/01")))
secciona(dc, fecha_valor = list())

我总是收到上面提到的错误。

任何想法为什么会发生这种情况?我应该以其他方式进行编辑子集以进行绘图的方法吗?

【问题讨论】:

    标签: r function data.table subset data.cube


    【解决方案1】:

    这是R用户在处理非标准评估时会面临的标准问题。这是计算语言 R 语言特性的结果。
    [.data.cube 函数希望以交互方式使用,这扩展了传递给它的参数的灵活性,但有一些限制.在这方面,当将表达式从包装函数传递到 [ 子集运算符时,它类似于 [.data.table。我添加了虚拟示例以使其可重现。

    我看到你已经在使用data.cube-oop 分支,所以只是为了向其他读者澄清。 data.cube-oop 分支是在 master 分支之前 92 次提交,安装使用以下。

    install.packages("data.cube", repos = paste0("https://", c(
        "jangorecki.gitlab.io/data.cube",
        "Rdatatable.github.io/data.table",
        "cran.rstudio.com"
    )))
    

    library(data.cube)
    set.seed(1)
    ar = array(rnorm(8,10,5), rep(2,3), 
               dimnames = list(color = c("green","red"), 
                               year = c("2014","2015"), 
                               country = c("IN","UK"))) # sorted
    dc = as.data.cube(ar)
    
    f = function(color=list(), year=list(), country=list(), drop=FALSE){
        expr = substitute(
            dc[color=.color, year=.year, country=.country, drop=.drop],
            list(.color=color, .year=year, .country=country, .drop=drop)
        )
        eval(expr)
    }
    f(year=list(c("2014","2015")), country="UK")
    #<data.cube>
    #fact:
    #  4 rows x 3 dimensions x 1 measures (0.00 MB)
    #dimensions:
    #  color : 2 entities x 1 levels (0.00 MB)
    #  year : 2 entities x 1 levels (0.00 MB)
    #  country : 1 entities x 1 levels (0.00 MB)
    #total size: 0.01 MB
    

    您只需将print(expr) 放在/而不是eval(expr) 之前即可跟踪表达式。

    阅读有关非标准评估的更多信息:
    - R Language Definition: Computing on the language
    - Advanced R: Non-standard evaluation
    - manual of substitute function
    以及一些相关的 SO 问题:
    - Passing on non-standard evaluation arguments to the subset function
    - In R, why is [ better than subset?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多