r - 如果加载了 ggplot2 包,则在对象上调用 `str` 会出错

标签 r debugging ggplot2

以下内容已在 Debian squeeze 上使用 R 2.15.3ggplot2 0.9.3.1gtable 0.1.2 进行测试。

在调试这个ggplot2问题的过程中 multiple calls to annotation_custom fail in certain cases ,我遇到了一些我不知道如何调试的事情。我已经在 Bitbucket 中创建了一个包含相关信息的存储库 github-ggplot2-817 .

相关README下面复制了该存储库中的内容。 如果你不想使用Mercurial,相关的 带有序列化R数据的文件可以直接从addgrob.asc.save下载.

要重现 addgrob.asc.save,您可以使用 https://bitbucket.org/faheem/gtable-debian 中的 Debian 包。 ,或者如果您愿意,只需对 gtable 进行补丁,即 gtableaddgrob.diff ,以及脚本 save.R .

问题摘要:在对象上运行 R 的 str 函数(此处称为 x) 如果加载了 ggplot2,则会出现错误,否则不会出现错误。我不确定发生了什么事 所以任何指示将不胜感激。

###############################################################

要重现此错误,请执行以下操作。

启动 R。然后

> ls()
character(0)
> load("addgrob.asc.save")
> ls()
[1] "grobs"  "layout" "x"

> str(x, max.level=1)
List of 10
 $ grobs   :List of 8
 $ layout  :'data.frame':       8 obs. of  7 variables:
   $ widths  :Class 'unit'  atomic [1:6] 1.5 6.096 0.762 1.961 0 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ heights :Class 'unit'  atomic [1:7] 1.5 2.53 1.52 6.1 6.1 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ respect : logi FALSE
 $ rownames: NULL
 $ colnames: NULL
 $ name    : chr "layout"
 $ gp      : NULL
 $ vp      : NULL
 - attr(*, "class")= chr [1:3] "gtable" "grob" "gDesc"

## Now update x
> x$grobs <- c(x$grobs, grobs)
> x$layout <- rbind(x$layout, layout)

> str(x, max.level=1)
List of 10
 $ grobs   :List of 11
 $ layout  :'data.frame':       11 obs. of  7 variables:
   $ widths  :Class 'unit'  atomic [1:6] 1.5 6.096 0.762 1.961 0 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ heights :Class 'unit'  atomic [1:7] 1.5 2.53 1.52 6.1 6.1 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ respect : logi FALSE
 $ rownames: NULL
 $ colnames: NULL
 $ name    : chr "layout"
 $ gp      : NULL
 $ vp      : NULL
 - attr(*, "class")= chr [1:3] "gtable" "grob" "gDesc"

> library(ggplot2)

> str(x, max.level=1)
List of 11
 $ grobs   :List of 11
 $ layout  :'data.frame':       11 obs. of  7 variables:
   $ widths  :Class 'unit'  atomic [1:6] 1.5 6.096 0.762 1.961 0 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ heights :Class 'unit'  atomic [1:7] 1.5 2.53 1.52 6.1 6.1 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ respect : logi FALSE
 $ rownames: NULL
 $ colnames: NULL
 $ name    : chr "layout"
 $ gp      : NULL
 $ vp      : NULL
 $ NA:Error in object[[i]] : subscript out of bounds

加载ggplot2不会导致早期版本的x出错 输出str

> ls()
character(0)
> load("addgrob.asc.save")
l> ls()
[1] "grobs"  "layout" "x"     
> library(ggplot2)
> str(x, max.level=1)
List of 8
 $ grobs   :List of 8
 $ layout  :'data.frame':       8 obs. of  7 variables:
 $ widths  :Class 'unit'  atomic [1:6] 1.5 6.096 0.762 1.961 0 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ heights :Class 'unit'  atomic [1:7] 1.5 2.53 1.52 6.1 6.1 ...
  .. ..- attr(*, "unit")= chr "mm"
  .. ..- attr(*, "valid.unit")= int 7
 $ respect : logi FALSE
 $ rownames: NULL
 $ colnames: NULL
 $ name    : chr "layout"
 - attr(*, "class")= chr [1:3] "gtable" "grob" "gDesc"

traceback() 的输出是

> traceback()
2: str.default(x, max.level = 1)
1: str(x, max.level = 1)

附录:在Python中,可以进行设置,以便将执行函数时调用的所有代码行都写入文件中。这可能相当庞大,但如果这在R中是可能的,那将有助于澄清事情。我进行了 Google 搜索,但我想到的只是各种调试实用程序。

ADDENDUM2:我创建了问题 The str function fails with error on ggplot2 objects为此。

最佳答案

可以使用以下命令更简单地重新创建错误:

x[11]

我认为发生的情况是您的“x”对象是在具有“[”-“gtable”对象方法的工作环境中创建的。现在,您将其带回最初没有的工作区,然后随着 ggplot2 的加载,现在 ggplot2 引用了某种“[”方法,即使 gtables 包仍未加载。

我无法通过加载 pkg:gtable 来解决问题。但我不相信“gtable”是一个正确形成的包,因为尽管 require(gtable) 报告成功,但 sessionInfo() 拒绝报告其注册。

> require(gtable)
Loading required package: gtable
> `[.gtable`(x, 11)
Error: could not find function "[.gtable"
No suitable frames for recover()
> x[11]
Error in if (any(index < 0)) { : missing value where TRUE/FALSE needed

Enter a frame number, or 0 to exit   

1: x[11]
2: `[.gtable`(x, 11)
3: x$heights[rows]
4: `[.unit`(x$heights, rows)

Selection: 0
> sessionInfo()

R version 3.0.0 RC (2013-03-31 r62463)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      grDevices datasets  splines   graphics  utils     stats    
[8] methods   base     

other attached packages:
 [1] gtable_0.1.2        RCurl_1.95-4.1      bitops_1.0-5       
 [4] data.table_1.8.8    gplots_2.11.0       MASS_7.3-26        
 [7] KernSmooth_2.23-10  caTools_1.14        gdata_2.12.0       
[10] gtools_2.7.1        ggplot2_0.9.3.1     gridExtra_0.9.1    
[13] HH_2.3-36           colorspace_1.2-1    reshape_0.8.4      
[16] plyr_1.8            latticeExtra_0.6-24 RColorBrewer_1.0-5 
[19] leaps_2.9           multcomp_1.2-17     mvtnorm_0.9-9994   
[22] rms_3.6-3           Hmisc_3.10-1        survival_2.37-4    
[25] sos_1.3-5           brew_1.0-6          lattice_0.20-15    

loaded via a namespace (and not attached):
 [1] cluster_1.14.4  dichromat_2.0-0 digest_0.6.3    fortunes_1.5-0 
 [5] labeling_0.1    munsell_0.4     proto_0.3-10    reshape2_1.2.2 
 [9] scales_0.2.3    stringr_0.6.2   tools_3.0.0  

(我确实安装了 gtable 0.1.2。)

附录:可以用此代码重现:

test <- data.frame(x=1:20, y=21:40, 
                  facet.a=rep(c(1,2), 10), 
                  facet.b=rep(c(1,2), each=20))
p <- qplot(data=test, x=x, y=y, facets=facet.b~facet.a)
# get gtable object
z <- ggplot_gtable(ggplot_build(p))
length(z)
#[1] 16
z[16]  # drops into browser

关于r - 如果加载了 ggplot2 包,则在对象上调用 `str` 会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17127339/

相关文章:

java - 如何将 NetBeans 配置为仅单步执行我编写的 Java 代码

删除ggplot2中x轴的空白行

r - ggplot2:主题集、pdf 导出和 Illustrator 遇到困难

c - useDynLib() 错误且compileAttributes 不返回任何内容 : embedding a C library into an R package

r - grid : How to specify limits to avoid grid. 点()绘制超出视口(viewport)?

r - Shiny 的rcharts图表的大小?

r - 如何: Create a plot for 3 categorical variables and a continuous variable in R?

r - 我应该使用 mget()、.. 还是 with=FALSE 来选择 data.table 的列?

android - 无法在 android 设备上调试应用程序 - Android Studio 2.0

python - 我想对 int() 进行输入验证,但对 str() 进行输入验证