尽管试图保留属性,但仍会丢失属性

Ale*_*ekh 5 attributes r object user-defined-types attr

make总是重建Makefile目标(make 总是重建 Makefile 目标)的问题及其调查发现了另一个问题,这是这个问题的主题。重复执行以下R代码导致对象属性丢失数据转换操作期间。

为了记录,我不得不说我已经写过这个主题(在提取/替换操作期间保留对象属性的方法),但是这个问题和答案更笼统(我不正确,简单的保存属性是有效的 - 它在撰写本文时为我工作,因为当时我还没有执行操作,这对对象的属性有潜在危险)。

以下是我的 R 代码的摘录,其中我遇到了属性丢失的问题。

##### GENERIC TRANSFORMATION FUNCTION #####

transformResult <- function (dataSource, indicator, handler) {

  fileDigest <- base64(indicator)
  rdataFile <- paste0(CACHE_DIR, "/", dataSource, "/",
                      fileDigest, RDS_EXT)
  if (file.exists(rdataFile)) {
    data <- readRDS(rdataFile)

    # Preserve user-defined attributes for data frame's columns
    # via defining new class 'avector' (see code below)). Also,
    # preserve attributes (comments) for the data frame itself.
    data2 <- data.frame(lapply(data, function(x) 
      { structure(x, class = c("avector", class(x))) } ))
    #mostattributes(data2) <- attributes(data)
    attributes(data2) <- attributes(data)

    result <- do.call(handler, list(indicator, data2))
    saveRDS(result, rdataFile)
    rm(result)
  }
  else {
    error("RDS file for \'", indicator, "\' not found! Run 'make' first.")
  }
}


## Preserve object's special attributes:
## use a class with a "as.data.frame" and "[" method

as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function (x, ...) {
  #attr <- attributes(x)
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  #attributes(r) <- attr
  return (r)
}

##### HANDLER FUNCTION DEFINITIONS #####

projectAge <- function (indicator, data) {

  # do not process, if target column already exists
  if ("Project Age" %in% names(data)) {
    message("Project Age: ", appendLF = FALSE)
    message("Not processing - Transformation already performed!\n")
    return (invisible())
  }

  transformColumn <- as.numeric(unlist(data["Registration Time"]))
  regTime <- as.POSIXct(transformColumn, origin="1970-01-01")
  prjAge <- difftime(Sys.Date(), as.Date(regTime), units = "weeks")
  data[["Project Age"]] <- as.numeric(round(prjAge)) / 4 # in months

  # now we can delete the source column
  if ("Registration Time" %in% names(data))
    data <- data[setdiff(names(data), "Registration Time")]

    if (DEBUG2) {print(summary(data)); print("")}

  return (data)
}


projectLicense <- function (indicator, data) {

  # do not process, if target column (type) already exists
  if (is.factor(data[["Project License"]])) {
    message("Project License: ", appendLF = FALSE)
    message("Not processing - Transformation already performed!\n")
    return (invisible())
  }

  data[["Project License"]] <- 
    factor(data[["Project License"]],
           levels = c('gpl', 'lgpl', 'bsd', 'other',
                      'artistic', 'public', '(Other)'),
           labels = c('GPL', 'LGPL', 'BSD', 'Other',
                      'Artistic', 'Public', 'Unknown'))

  if (DEBUG2) {print(summary(data)); print("")}

  return (data)
}


devTeamSize <- function (indicator, data) {

  var <- data[["Development Team Size"]]

  # convert data type from 'character' to 'numeric' 
  if (!is.numeric(var)) {
    data[["Development Team Size"]] <- as.numeric(var)
  }

  if (DEBUG2) {print(summary(data)); print("")}

  return (data)
}


##### MAIN #####

# construct list of indicators & corresponding transform. functions
indicators <- c("prjAge", "prjLicense", "devTeamSize")
transforms <- list(projectAge, projectLicense, devTeamSize)

# sequentially call all previously defined transformation functions
lapply(seq_along(indicators),
       function(i) {
         transformResult("SourceForge",
                         indicators[[i]], transforms[[i]])
         })
Run Code Online (Sandbox Code Playgroud)

在第二次运行此代码后,名称“Project Age”和“Project License”以及数据框的其他用户定义属性data2都将丢失。

我的问题是多方面的:

1)我的代码中哪些语句会导致属性丢失为什么;

2)什么是正确的代码行(mostattributes <- attributesattributes <- attributes/attrtransformResult()avector类定义以及为什么;

3)as.data.frame.avector <- as.data.frame.vector如果我将类属性添加avector到数据框对象并且通常更喜欢通用解决方案(不仅适用于数据框),则是否真正需要该语句;为什么或者为什么不。

4) 通过attr 在类定义中保存不起作用,它失败并出现以下错误:

Error in attributes(r) <- attr :
  'names' attribute [5] must be the same length as the vector [3]
Calls: lapply ... summary.data.frame -> lapply -> FUN -> summary.default -> [ -> [.avector
Run Code Online (Sandbox Code Playgroud)

所以,我不得不回去使用mostattributes(). 可以吗?

==========

我已阅读有关该主题的以下内容:

  1. SO 问题:如何在不丢失属性的情况下从 data.frame 中删除一行(我喜欢 Ben Barns 的解决方案,但它与 Gabor Grothendieck 和 Marc Schwartz 建议的解决方案略有不同 - 见下文);

  2. 所以问题:索引操作删除属性(虽然解决方案清晰易读,但我更喜欢一个,基于类定义/子类?/);

  3. Heinz Tuechler ( https://stat.ethz.ch/pipermail/r-help/2006-July/109148.html )建议的通用解决方案- 我需要这个吗?

  4. Brian Ripley 的解释(http://r.789695.n4.nabble.com/Losing-attributes-in-data-frame-PR-10873-tp919265p919266.html) - 我发现它有点令人困惑;

  5. Gabor Grothendieck ( https://stat.ethz.ch/pipermail/r-help/2006-May/106308.html )提出的解决方案;

  6. Marc Schwartz ( https://stat.ethz.ch/pipermail/r-help/2006-May/106351.html )对 Gabor Grothendieck 的解决方案的解释 - 非常好的解释;

  7. “R Inferno”一书(www.burns-stat.com/pages/Tutor/R_inferno.pdf)的第 8.1.28 和 8.1.29 节 - 我已经尝试了他的使用建议storage.mode(),但并没有真正解决问题,因为强制通过storage不会影响class对象(更不用说它不包括强制清除属性之外的操作,例如子集和索引;

  8. http://adv-r.had.co.nz/Data-structures.html#attributes ;

  9. http://stat.ethz.ch/R-manual/R-devel/library/base/html/attributes.html ;

  10. http://cran.r-project.org/doc/manuals/r-devel/R-lang.html#Copying-of-attributes

PS 我相信这个问题是一般性的,所以我目前还没有提供可重现的例子。我希望可以在没有这样的例子的情况下回答这个问题,但是,如果没有,请告诉我。

Ale*_*ekh 0

我正在回答我自己的问题 - 好吧,目前只是部分回答:

1)经过更深入的调查和一些代码更新后,似乎属性实际上并没有丢失(仍在尝试找出是什么变化导致了预期的行为 - 将在稍后报告)。

2)我已经弄清楚了转换后间歇性输出并丢失所有缓存数据的原因,如下。在代码的多次后续运行期间,每个转换(处理程序)函数projectAge()projectLicense()devTeamSize())的第二次运行返回 NULL,因为转换已经完成:

if (<condition>) {
  ...
  message("Not processing - Transformation already performed!\n")
  return (invisible()) # <= returns NULL
}
Run Code Online (Sandbox Code Playgroud)

然后返回的 NULL 被传递给saveRDS(),从而导致缓存数据丢失。

我通过在保存转换后的对象之前进行简单验证来解决此问题:result

# the next line is problematic due to wrong assumption of always having full data returned
result <- do.call(handler, list(indicator, data2))
if (!is.null(result)) saveRDS(result, rdataFile) # <= fixed by validating incoming data
Run Code Online (Sandbox Code Playgroud)

到目前为止就是这样,感谢您的阅读!我将更新这个答案,直到所有问题都得到澄清。