2016-10-20 1 views
3

私は自分自身の関数を作り、Rでdata.cubeをサブセット化し、構築しようとしているいくつかの定義済みのプロットに対して自動的に結果をフォーマットします。subsettingカスタム関数内で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()) 

を試してみたときに。

これは何が起こっているのですか?プロットのためにサブセットを編集する私のアプローチのために他の方法で進めるべきですか?

答えて

2

これは、Rユーザーが非標準評価を処理する際に直面する標準的な問題です。これはの結果です。言語のコンピューティング
[.data.cube関数は、渡された引数の柔軟性を拡張しますが、いくつかの制限があります。その側面では、ラッパー関数から式を[サブセット演算子に渡すときは、[.data.tableに似ています。再現性を高めるためにダミーの例を追加しました。

私はすでにdata.cube-oopブランチを使用していますので、他の読者のために明確にしています。 data.cube-oopブランチは、マスターブランチよりも先に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 

あなたは/その代わりeval(expr)print(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?