【发布时间】:2015-08-11 18:55:40
【问题描述】:
我在 Shiny R 应用程序中遇到问题,当应用程序应该呈现 ggplot2 绘图时,我会遇到间歇性错误。
我看到的错误包括:
Error in eval(expr, envir, enclos) :
arguments imply differing number of rows: 136, 129
和
Error in eval(expr, envir, enclos) :
Results must be all atomic, or all data frames
我在我的 Shiny 应用中看到了同样的错误。
经过大量搜索,我在这里找到了最近的一篇文章,似乎重现了相同的错误集: http://cowboyjob.com/post/6523856/Reupping-my-question-from-a-few-hours-ago-I-have-word-from-another-channel-that
我也发现了这个问题,可能是相关的:Tracing root cause for R segfault
第一个link中的代码如下:
library(ggplot2)
set.seed(12345678)
sessionInfo()
littledata = data.frame(x=1:128, y=rlnorm(128))
bigdata = data.frame(x=1:129, y=rlnorm(129))
# plots as expected
lp = ggplot(littledata, aes(x, y))+geom_histogram(stat="identity", binwidth=1)
for (i in 1:20){
print(i)
try(print(lp+ggtitle(paste("128 points", i))))
}
# always warns "position_stack requires constant width",
# intermittently throws error, hangs, or segfaults.
# See below for details.
bp = ggplot(bigdata, aes(x, y))+geom_histogram(stat="identity", binwidth=1)
for(i in 1:20){
print(i)
try(print(bp+ggtitle(paste("129 points", i))))
}
## End demo code
对于我正在使用的机器,这是该代码块的输出:
> source('~/R/testing/segfault_test.R')
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
[1] 13
[1] 14
[1] 15
[1] 16
[1] 17
[1] 18
[1] 19
[1] 20
[1] 1
Error in eval(expr, envir, enclos) : replacement has length zero
In addition: Warning messages:
1: package ‘ggplot2’ was built under R version 3.1.2
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 2
[1] 3
[1] 4
[1] 5
Error in eval(expr, envir, enclos) :
Results must be all atomic, or all data frames
In addition: Warning messages:
1: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
3: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
4: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 6
[1] 7
Error in eval(expr, envir, enclos) :
arguments imply differing number of rows: 136, 129
In addition: Warning messages:
1: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 8
Error in eval(expr, envir, enclos) :
Results must be all atomic, or all data frames
In addition: Warning message:
In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 9
[1] 10
[1] 11
Error in eval(expr, envir, enclos) :
arguments imply differing number of rows: 136, 129
In addition: Warning messages:
1: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
3: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 12
[1] 13
Error in eval(expr, envir, enclos) :
Results must be all atomic, or all data frames
In addition: Warning messages:
1: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 14
[1] 15
Error in eval(expr, envir, enclos) :
arguments imply differing number of rows: 136, 129
In addition: Warning messages:
1: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 16
[1] 17
[1] 18
Error in eval(expr, envir, enclos) :
arguments imply differing number of rows: 136, 129
In addition: Warning messages:
1: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
3: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
[1] 19
[1] 20
Warning messages:
1: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
2: In loop_apply(n, do.ply) :
position_stack requires constant width: output may be incorrect
>
从这个输出中,我们可以看到第一个循环运行没有任何问题;但是,具有 129 个点的第二个循环在某些图上会出现错误。
当我将几个 ggplot 图嵌入到闪亮的应用程序中时,我看到这些错误出现,通常会导致分段错误。
这是我在闪亮服务器上运行应用程序时在 javascript 控制台中看到的内容:
Error in eval(substitute(expr), envir, enclos) :
Results must be all atomic, or all data frames
*** caught segfault ***
address 0x2, cause 'memory not mapped'
Traceback:
1: dim(x)
2: FUN(X[[211L]], ...)
3: lapply(res, NROW)
4: unlist(lapply(res, NROW))
5: list_to_dataframe(res, attr(.data, "split_labels"), .id, id_as_factor)
6: ldply(.data = pieces, .fun = .fun, ..., .progress = .progress, .inform = .inform, .parallel = .parallel, .paropts = .paropts)
7: ddply(munched, .(group), function(df) { data.frame(solid = identical(unique(df$linetype), 1), constant = nrow(unique(df[, c("alpha", "colour", "size", "linetype")])) == 1)})
8: get(x, envir = this, inherits = inh)(this, ...)
9: GeomPath$draw(data, scales, coordinates, arrow, ...)
10: get(x, envir = this, inherits = inh)(this, ...)
11: .$draw(...)
12: get(x, envir = this, inherits = inh)(this, ...)
13: (function (...) get(x, envir = this, inherits = inh)(this, ...))(data = data, scales = scales, coordinates = cs)
14: do.call(.$geom$draw_groups, c(data = list(as.name("data")), scales = list(as.name("scales")), coordinates = list(as.name("cs")), .$geom_params))
15: get(x, envir = this, inherits = inh)(this, ...)
16: layer$make_grob(df, scales = panel$ranges[[panel_i]], cs = plot$coord)
17: .fun(piece, ...)
18: (function (i) { piece <- pieces[[i]] if (.inform) { res <- try(.fun(piece, ...)) if (inherits(res, "try-error")) { piece <- paste(capture.output(print(piece)), collapse = "\n") stop("with piece ", i, ": \n", piece, call. = FALSE) } } else { res <- .fun(piece, ...) } progress$step() res})(1L)
19: eval(substitute(expr), envir, enclos)
20: evalq((function (i) { piece <- pieces[[i]] if (.inform) { res <- try(.fun(piece, ...)) if (inherits(res, "try-error")) { piece <- paste(capture.output(print(piece)), collapse = "\n") stop("with piece ", i, ": \n", piece, call. = FALSE) } } else { res <- .fun(piece, ...) } progress$step() res})(1L), <environment>)
21: doTryCatch(return(expr), name, parentenv, handler)
22: tryCatchOne(expr, names, parentenv, handlers[[1L]])
23: tryCatchList(expr, classes, parentenv, handlers)
24: tryCatch(evalq((function (i) { piece <- pieces[[i]] if (.inform) { res <- try(.fun(piece, ...)) if (inherits(res, "try-error")) { piece <- paste(capture.output(print(piece)), collapse = "\n") stop("with piece ", i, ": \n", piece, call. = FALSE) } } else { res <- .fun(piece, ...) } progress$step() res})(1L), <environment>), error = .rcpp_error_recorder)
25: withCallingHandlers(tryCatch(evalq((function (i) { piece <- pieces[[i]] if (.inform) { res <- try(.fun(piece, ...)) if (inherits(res, "try-error")) { piece <- paste(capture.output(print(piece)), collapse = "\n") stop("with piece ", i, ": \n", piece, call. = FALSE) } } else { res <- .fun(piece, ...) } progress$step() res})(1L), <environment>), error = .rcpp_error_recorder), warning = .rcpp_warning_recorder)
aborting ...
Segmentation fault (core dumped)
我的 sessionInfo() 命令的输出如下:
R version 3.1.1 (2014-07-10)
Platform: x86_64-redhat-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_1.0.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-6 digest_0.6.8 grid_3.1.2 gtable_0.1.2 labeling_0.3 MASS_7.3-35
[7] munsell_0.4.2 plyr_1.8.2 proto_0.3-10 Rcpp_0.11.6 reshape2_1.4.1 scales_0.2.4
[13] stringr_0.6.2 tools_3.1.2
还有其他人遇到过这种情况吗?在过去的几天里,它一直让我发疯(断断续续地!)。我已经尝试过使用不同版本的 R (3.1.1 - 3.2.0)、Rcpp (0.11.6, 0.11.5)、plyr (1.8.1 - 1.8.2) 以及两个不同版本的 (Red Hat) Linux 机器,并且可以在两台机器上重现错误。
非常感谢任何帮助或建议。
编辑: 我已经在我正在使用的机器上完成了 R 和所有软件包的全新安装(基于 hadley/ggplot2 的 issue #1006 中发现的旧建议)
我正在使用版本跟踪,并且在 OSX 笔记本电脑以及两台不同的 RedHat Linux 机器上的 Shiny 应用程序具有相同的代码库。 我在 OSX 上运行没有问题;所以我使用 packrat 将包库复制到两台 RedHat 机器上,三台机器都运行 R-3.1.2,但这并不能解决问题。
Shiny 应用程序仍然崩溃并在 JS 控制台中显示以下输出:
Warning in run(timeoutMs) :
Removed 1 rows containing missing values (geom_path).
Error: Results must be all atomic, or all data frames
Execution halted
以下是 Javascript 控制台的结果:
67: try(handler(binary, message))
68: (function (handle, binary, message) { for (handler in .wsconns[[as.character(handle)]]$.messageCallbacks) { result <- try(handler(binary, message)) if (inherits(result, "try-error")) { .wsconns[[as.character(handle)]]$close() return() } }})("266303168", FALSE, "{\"method\":\"update\",\"data\":{\".clientdata_output_out035b13d236a68453_width\":386,\".clientdata_output_out035b13d236a68453_height\":400,\".clientdata_output_out45a459f761c2bcff_width\":386,\".clientdata_output_out45a459f761c2bcff_height\":400,\".clientdata_output_out0b680f21f3d73958_width\":386,\".clientdata_output_out0b680f21f3d73958_height\":400,\".clientdata_output_out785a5b0a4a8d2872_width\":386,\".clientdata_output_out785a5b0a4a8d2872_height\":400,\".clientdata_output_out4d4c261305d448a2_width\":386,\".clientdata_output_out4d4c261305d448a2_height\":400,\".clientdata_output_outae8b99c9ab2044d8_width\":386,\".clientdata_output_outae8b99c9ab2044d8_height\":400,\".clientdata_output_oute11f4c69b81158cc_width\":386,\".clientdata_output_oute11f4c69b81158cc_height\":400,\".clientdata_output_out1beb34c46b1bdebd_width\":386,\".clientdata_output_out1beb34c46b1bdebd_height\":400,\".clientdata_output_out9b9abdc2e1b58daa_width\":386,\".clientdata_output_out9b9abdc2e1b58daa_height\":400,\".clientdata_output_out035b13d236a68453_hidden\":false,\".clientdata_output_out45a459f761c2bcff_hidden\":false,\".clientdata_output_out0b680f21f3d73958_hidden\":false,\".clientdata_output_out785a5b0a4a8d2872_hidden\":false,\".clientdata_output_out4d4c261305d448a2_hidden\":false,\".clientdata_output_outae8b99c9ab2044d8_hidden\":false,\".clientdata_output_oute11f4c69b81158cc_hidden\":false,\".clientdata_output_out1beb34c46b1bdebd_hidden\":false,\".clientdata_output_out9b9abdc2e1b58daa_hidden\":false}}")
69: eval(substitute(expr), envir, enclos)
70: evalq((function (handle, binary, message) { for (handler in .wsconns[[as.character(handle)]]$.messageCallbacks) { result <- try(handler(binary, message)) if (inherits(result, "try-error")) { .wsconns[[as.character(handle)]]$close() return() } }})("266303168", FALSE, "{\"method\":\"update\",\"data\":{\".clientdata_output_out035b13d236a68453_width\":386,\".clientdata_output_out035b13d236a68453_height\":400,\".clientdata_output_out45a459f761c2bcff_width\":386,\".clientdata_output_out45a459f761c2bcff_height\":400,\".clientdata_output_out0b680f21f3d73958_width\":386,\".clientdata_output_out0b680f21f3d73958_height\":400,\".clientdata_output_out785a5b0a4a8d2872_width\":386,\".clientdata_output_out785a5b0a4a8d2872_height\":400,\".clientdata_output_out4d4c261305d448a2_width\":386,\".clientdata_output_out4d4c261305d448a2_height\":400,\".clientdata_output_outae8b99c9ab2044d8_width\":386,\".clientdata_output_outae8b99c9ab2044d8_height\":400,\".clientdata_output_oute11f4c69b81158cc_width\":386,\".clientdata_output_oute11f4c69b81158cc_height\":400,\".clientdata_output_out1beb34c46b1bdebd_width\":386,\".clientdata_output_out1beb34c46b1bdebd_height\":400,\".clientdata_output_out9b9abdc2e1b58daa_width\":386,\".clientdata_output_out9b9abdc2e1b58daa_height\":400,\".clientdata_output_out035b13d236a68453_hidden\":false,\".clientdata_output_out45a459f761c2bcff_hidden\":false,\".clientdata_output_out0b680f21f3d73958_hidden\":false,\".clientdata_output_out785a5b0a4a8d2872_hidden\":false,\".clientdata_output_out4d4c261305d448a2_hidden\":false,\".clientdata_output_outae8b99c9ab2044d8_hidden\":false,\".clientdata_output_oute11f4c69b81158cc_hidden\":false,\".clientdata_output_out1beb34c46b1bdebd_hidden\":false,\".clientdata_output_out9b9abdc2e1b58daa_hidden\":false}}"), <environment>)
71: doTryCatch(return(expr), name, parentenv, handler)
72: tryCatchOne(expr, names, parentenv, handlers[[1L]])
73: tryCatchList(expr, classes, parentenv, handlers)
74: tryCatch(evalq((function (handle, binary, message) { for (handler in .wsconns[[as.character(handle)]]$.messageCallbacks) { result <- try(handler(binary, message)) if (inherits(result, "try-error")) { .wsconns[[as.character(handle)]]$close() return() } }})("266303168", FALSE, "{\"method\":\"update\",\"data\":{\".clientdata_output_out035b13d236a68453_width\":386,\".clientdata_output_out035b13d236a68453_height\":400,\".clientdata_output_out45a459f761c2bcff_width\":386,\".clientdata_output_out45a459f761c2bcff_height\":400,\".clientdata_output_out0b680f21f3d73958_width\":386,\".clientdata_output_out0b680f21f3d73958_height\":400,\".clientdata_output_out785a5b0a4a8d2872_width\":386,\".clientdata_output_out785a5b0a4a8d2872_height\":400,\".clientdata_output_out4d4c261305d448a2_width\":386,\".clientdata_output_out4d4c261305d448a2_height\":400,\".clientdata_output_outae8b99c9ab2044d8_width\":386,\".clientdata_output_outae8b99c9ab2044d8_height\":400,\".clientdata_output_oute11f4c69b81158cc_width\":386,\".clientdata_output_oute11f4c69b81158cc_height\":400,\".clientdata_output_out1beb34c46b1bdebd_width\":386,\".clientdata_output_out1beb34c46b1bdebd_height\":400,\".clientdata_output_out9b9abdc2e1b58daa_width\":386,\".clientdata_output_out9b9abdc2e1b58daa_height\":400,\".clientdata_output_out035b13d236a68453_hidden\":false,\".clientdata_output_out45a459f761c2bcff_hidden\":false,\".clientdata_output_out0b680f21f3d73958_hidden\":false,\".clientdata_output_out785a5b0a4a8d2872_hidden\":false,\".clientdata_output_out4d4c261305d448a2_hidden\":false,\".clientdata_output_outae8b99c9ab2044d8_hidden\":false,\".clientdata_output_oute11f4c69b81158cc_hidden\":false,\".clientdata_output_out1beb34c46b1bdebd_hidden\":false,\".clientdata_output_out9b9abdc2e1b58daa_hidden\":false}}"), <environment>), error = .rcpp_error_recorder)
75: withCallingHandlers(tryCatch(evalq((function (handle, binary, message) { for (handler in .wsconns[[as.character(handle)]]$.messageCallbacks) { result <- try(handler(binary, message)) if (inherits(result, "try-error")) { .wsconns[[as.character(handle)]]$close() return() } }})("266303168", FALSE, "{\"method\":\"update\",\"data\":{\".clientdata_output_out035b13d236a68453_width\":386,\".clientdata_output_out035b13d236a68453_height\":400,\".clientdata_output_out45a459f761c2bcff_width\":386,\".clientdata_output_out45a459f761c2bcff_height\":400,\".clientdata_output_out0b680f21f3d73958_width\":386,\".clientdata_output_out0b680f21f3d73958_height\":400,\".clientdata_output_out785a5b0a4a8d2872_width\":386,\".clientdata_output_out785a5b0a4a8d2872_height\":400,\".clientdata_output_out4d4c261305d448a2_width\":386,\".clientdata_output_out4d4c261305d448a2_height\":400,\".clientdata_output_outae8b99c9ab2044d8_width\":386,\".clientdata_output_outae8b99c9ab2044d8_height\":400,\".clientdata_output_oute11f4c69b81158cc_width\":386,\".clientdata_output_oute11f4c69b81158cc_height\":400,\".clientdata_output_out1beb34c46b1bdebd_width\":386,\".clientdata_output_out1beb34c46b1bdebd_height\":400,\".clientdata_output_out9b9abdc2e1b58daa_width\":386,\".clientdata_output_out9b9abdc2e1b58daa_height\":400,\".clientdata_output_out035b13d236a68453_hidden\":false,\".clientdata_output_out45a459f761c2bcff_hidden\":false,\".clientdata_output_out0b680f21f3d73958_hidden\":false,\".clientdata_output_out785a5b0a4a8d2872_hidden\":false,\".clientdata_output_out4d4c261305d448a2_hidden\":false,\".clientdata_output_outae8b99c9ab2044d8_hidden\":false,\".clientdata_output_oute11f4c69b81158cc_hidden\":false,\".clientdata_output_out1beb34c46b1bdebd_hidden\":false,\".clientdata_output_out9b9abdc2e1b58daa_hidden\":false}}"), <environment>), error = .rcpp_error_recorder), warning = .rcpp_warning_recorder)
aborting ...
我注意到的另外一个项目;我正在运行的闪亮应用程序就像一个蒙特卡罗模拟 - 我运行的次数越多,应用程序崩溃的可能性就越大。较少的跑步往往会一直成功;似乎它可能与内存分配有关。
编辑 2: 恢复到旧版本的 Rcpp、plyr 和 ggplot 似乎可以在所有三台机器(OSX 和 Red Hat)上解决它;从现在开始,我还没有测试过是否可以升级这三个中的一个。
Rcpp 0.11.3
plyr 1.8.1
ggplot2 0.9.3
我不能发布代码,因为 SO 不允许我发布两个以上的链接,但我已经为所有 3 个包安装了以下内容:
install.packages(<<url to source tarball>>,repos = NULL,type="source")`
【问题讨论】:
-
如果您使用
ggplot(bigdata, aes(x, y))+geom_bar(stat="identity", width=1)会怎样?直方图通常是单变量图,因此指定y值是不常见的。 -
@MrFlick - 用您的建议替换
bp = ...运行没有任何问题。我在 Shiny 应用程序中发现了许多不同的绘图类型(线/点)。 -
作为进一步的更新,使用我相同的闪亮应用程序代码,我无法在使用 R 3.1.2 的 OSX 上产生错误;除非我将 Rcpp 更新为 0.11.6。使用 R 3.1.2 和完全相同的包版本,我可以在两台不同的 RedHat Linux 机器上复制错误。
-
这看起来也类似于这个问题,但它确实看起来是导致问题的包版本(例如 ggplot2)的组合:stackoverflow.com/questions/30414378/… 我只是没有缩小确切的范围组合不起作用。
-
非常感谢一直在研究这个问题的 Hadley;这是我们在 google 群组上的讨论:groups.google.com/d/msg/ggplot2/DCtDNFHgvb0/hyooD2KnivQJ
标签: r ggplot2 shiny shiny-server