0

我似乎有 NSE、rmarkdown 和 callr 的奇怪组合,类似于Is it possible to disable `callr` for RMarkdown? .

当我为变量设置一个值以在类似于使用 NSE 通过嵌套 eval 和列表调用实现的过滤器调用中使用它时,它会严重失败,但仅在 callr 中失败。

---
title: "Test CC2 Failure Reproducibly"
author: "Robert M Flight"
output: rmarkdown::md_document
editor_options: 
  chunk_output_type: console
---


```{r run_enrichments}
set.seed(1234)
create_go_annotation = function(db, ontology = NULL){
  all_genes = keys(db)
  go_all_gene = AnnotationDbi::select(db, keys = all_genes, columns = c("GOALL", "ONTOLOGYALL"))
  
  if (!is.null(ontology)) {
    go_all_gene = go_all_gene[go_all_gene$ONTOLOGYALL == ontology, ]
    ontology_type = paste0("GO.", ontology)
  } else {
    ontology_type = "GO.all"
  }
  go_2_gene = split(go_all_gene$ENTREZID, go_all_gene$GOALL)
  go_2_gene = lapply(go_2_gene, unique)
  go_desc = AnnotationDbi::select(GO.db::GO.db, keys = names(go_2_gene), columns = "TERM", keytype = "GOID")$TERM
  names(go_desc) = names(go_2_gene)

  go_annotation = categoryCompare2::annotation(annotation_features = go_2_gene,
                                               description = go_desc,
                                               annotation_type = ontology_type,
                                               feature_type = "ENTREZID")
  go_annotation
}


library(org.Hs.eg.db)
library(GO.db)
library(categoryCompare2)
library(methods)

go_mf = create_go_annotation(org.Hs.eg.db, "MF")

all_features = unique(unlist(go_mf@annotation_features))
sig_features = sample(all_features, 500)

enrich = hypergeometric_feature_enrichment(
  new("hypergeom_features", significant = sig_features,
      universe = all_features,
      annotation = go_mf),
  p_adjust = "BH"
)

comb_enrich = combine_enrichments(sig1 = enrich)
sig_cutoff = 0.1
filter_enrich = get_significant_annotations(comb_enrich, padjust <= sig_cutoff)
```

通过调用者运行它:

r(function() rmarkdown::render(here::here("categoryCompare2_failure.Rmd"), output_file = "cc2_fails_1.md"), show = TRUE)

# Quitting from lines 13-57 (categoryCompare2_failure.Rmd)
# 
# Error in FUN(X[[i]], ...) : object 'sig_cutoff' not found
# Error: callr subprocess failed: object 'sig_cutoff' not found

# > .Last.error.trace
# Stack trace:
#   Process 221313:
#   1. callr:::r(function() rmarkdown::render(here::he ...
#     2. callr:::get_result(output = out, options)
#     3. throw(newerr, parent = remerr[[2]])
#         x callr subprocess failed: object 'sig_cutoff' not found
#   Process 221619:
#   15. (function () ...
#   16. rmarkdown::render(here::here("categoryCompare2_failure.Rmd"))
#   17. knitr::knit(knit_input, knit_output, envir = envir, quiet ...
#   18. knitr:::process_file(text, output)
#   19. base:::withCallingHandlers(if (tangle) process_tangle(gro ...
#   20. knitr:::process_group(group)
#   21. knitr:::process_group.block(group)
#   22. knitr:::call_block(x)
#   23. knitr:::block_exec(params)
#   24. knitr:::eng_r(options)
#   25. knitr:::in_dir(input_dir(), evaluate(code, envir = env, n ...
#   26. knitr:::evaluate(code, envir = env, new_device = FALSE, k ...
#   27. evaluate::evaluate(...)
#   28. evaluate:::evaluate_call(expr, parsed$src[[i]], envir = e ...
#   29. evaluate:::timing_fn(handle(ev <- withCallingHandlers(wit ...
#   30. base:::handle(ev <- withCallingHandlers(withVisible(eval( ...
#   31. base:::withCallingHandlers(withVisible(eval(expr, envir, ...
#   32. base:::withVisible(eval(expr, envir, enclos))
#   33. base:::eval(expr, envir, enclos)
#   34. base:::eval(expr, envir, enclos)
#   35. categoryCompare2:::get_significant_annotations(comb_enric ...
#   36. categoryCompare2:::get_significant_annotations(comb_enric ...
#   37. categoryCompare2:::.get_significant_combined_enrichment(i ...
#   38. base:::lapply(in_results@enriched, function(x) { ...
#   39. categoryCompare2:::FUN(X[[i]], ...)
#   40. categoryCompare2:::get_significant_annotations(x@statisti ...
#   41. categoryCompare2:::get_significant_annotations(x@statisti ...
#   42. categoryCompare2:::.get_significant_stat_results(in_resul ...
#   43. categoryCompare2:::multi_query_list(in_results@statistic_ ...
#   44. base:::lapply(queries, eval, list_to_query)
#   45. base:::FUN(X[[i]], ...)
#   46. base:::FUN(X[[i]], ...)
#   47. base:::.handleSimpleError(function (e) ...
#   48. h(simpleError(msg, call))
#     x object 'sig_cutoff' not found

可以看到它的抱怨sig_cutoff是没有找到,但它存在于环境中,但似乎没有得到传递。

如果我直接运行它,它可以工作:

rmarkdown::render(here::here("categoryCompare2_failure.Rmd"), output_file = "cc2_works.md")

我已将所有代码放在github repo中。

有趣的是,这绝对是调用者的问题,因为目标也有同样的问题。

调用者 v 1.0.7,rmarkdown v 2.11,R 4.1.0

其他包版本在renv.lock文件中。

4

0 回答 0