某些绘图未在Rstudio、knitr、Rmarkdown中渲染
我正在使用: Ubuntu 12.04 64位, R 3.0.2, RStudio 0.98.312, knitr 1.5, 降价0.6.3, mgcv1.7-27 我有一个包含多个代码块的Rmarkdown文档。在一个块的中间有一些代码我适合GAM,总结适合和阴谋的契合。问题是第一个绘图渲染到输出文件中,而第二个绘图没有渲染到输出文件中。以下是该区块中经过清理的代码片段:某些绘图未在Rstudio、knitr、Rmarkdown中渲染,r,plot,knitr,rstudio,r-markdown,R,Plot,Knitr,Rstudio,R Markdown,我正在使用: Ubuntu 12.04 64位, R 3.0.2, RStudio 0.98.312, knitr 1.5, 降价0.6.3, mgcv1.7-27 我有一个包含多个代码块的Rmarkdown文档。在一个块的中间有一些代码我适合GAM,总结适合和阴谋的契合。问题是第一个绘图渲染到输出文件中,而第二个绘图没有渲染到输出文件中。以下是该区块中经过清理的代码片段: fit <- gam(y ~ s(x), data=j0, subset= !is.na(x)) summary(f
fit <- gam(y ~ s(x), data=j0, subset= !is.na(x))
summary(fit) # look at non-missing only
plot(fit)
fit <- gam(y ~ s(sqrt(x)), data=j0, subset= !is.na(x))
summary(fit)
plot(fit)
mean(y[is.na(x)]) - mean(y[!is.na(x)])
您只是
attach()
的又一受害者,尽管人们一直警告不要使用attach()
。使用attach()
很容易出错。您在附加(j0)
后执行了此操作:
我理解您为什么要使用attach()
——您可以在任何地方避免使用笨拙的j0$
前缀,但您需要非常小心。除了我提到的问题之外,detach()
也很糟糕,因为您没有指定要分离的环境,默认情况下,搜索路径上的第二个环境是分离的,它不一定是您附加的环境,例如,您可能已将其他包加载到搜索路径上。因此,您必须明确:detach('j0')
回到
knitr
:如果您想知道,我可以解释发生了什么,但首先,在将代码传递给knitr
之前,您必须确保代码实际工作。随着误差的消除,您观察到的奇怪现象也将消失。请发布一个可复制的示例。您的问题可能是因为您设置了chunk选项fig.keep
,但是如果没有这些信息,这纯粹是猜测。日志上怎么说?谢谢Yihui。我想我的问题更多的是关于knitr中的错误处理。您可以从我的示例中看到,我知道这个错误,并且知道在修复代码块错误时不会出现knitr问题。当由knitr执行和直接在R中执行时,相同的代码块会产生不同的输出。这使得调试R代码更加困难,并意味着一个特定的工作流(在传输到knitr之前在R中完全调试)。这表明knitr并不像理想情况下那样与执行的代码隔离。出于调试目的,您最好设置chunk选项error=FALSE
,或者全局设置opts_chunk$set(error=FALSE)
;然后,每当块中出现错误时,knitr
将停止,您将能够使用诸如traceback()之类的常用方法调试代码
Some text.
```{r setup}
require(mgcv)
mkdata <- function(n=100) {
x <- rnorm(n) + 5
y <- x + 0.3 * rnorm(n)
x[sample(ceiling(n/2), ceiling(n/10))] <- NA
x <- x^2
data.frame(x, y)
}
```
Example 1
=========
Plot 2 fails to render. (Using the same fit object for each fit.)
```{r example_1}
j0 <- mkdata()
attach(j0)
mx <- min(x, na.rm=TRUE)
fit <- gam(y ~ s(x), data=j0, subset= !is.na(x))
summary(fit)
plot(fit) # plot 1
fit <- gam(y ~ s(sqrt(x)), data=j0, subset= !is.na(x))
summary(fit)
plot(fit) #plot 2
mean(y[is.na(x)]) - mean(y[!is.na(x)]) # means calculation
# recode the missing values
j0$x.na <- is.na(x)
j0$x.c <- ifelse(x.na, mx, x) # ERROR in recode
detach()
attach(j0)
fit <- gam(y ~ s(sqrt(x.c)) + x.na, data=j0) # doesn't run because of error in recode
summary(fit) # this is actually fit 2
plot(fit) # plot 3 (this is actually fit 2)
detach()
```
Example 2
=========
Use separate fit objects for each fit. Plot 2 renders OK.
```{r example_2}
j0 <- mkdata()
attach(j0)
mx <- min(x, na.rm=TRUE)
fit1 <- gam(y ~ s(x), data=j0, subset= !is.na(x))
summary(fit1)
plot(fit1) # plot 1
fit2 <- gam(y ~ s(sqrt(x)), data=j0, subset= !is.na(x))
summary(fit2)
plot(fit2) #plot 2
mean(y[is.na(x)]) - mean(y[!is.na(x)]) # means calculation
# recode the missing values
j0$x.na <- is.na(x)
j0$x.c <- ifelse(x.na, mx, x) # ERROR in recode
detach()
attach(j0)
fit3 <- gam(y ~ s(sqrt(x.c)) + x.na, data=j0) # doesn't run because of error in recode
summary(fit3)
plot(fit3) # plot 3
detach()
```
Example 3
=========
Revert to using the same fit object for each fit. Plot 2 renders because plot 3 is commented out.
```{r example_3}
j0 <- mkdata()
attach(j0)
mx <- min(x, na.rm=TRUE)
fit <- gam(y ~ s(x), data=j0, subset= !is.na(x))
summary(fit)
plot(fit) # plot 1
fit <- gam(y ~ s(sqrt(x)), data=j0, subset= !is.na(x))
summary(fit)
plot(fit) #plot 2
mean(y[is.na(x)]) - mean(y[!is.na(x)]) # means calculation
# recode the missing values
j0$x.na <- is.na(x)
j0$x.c <- ifelse(x.na, mx, x) # ERROR in recode
detach()
attach(j0)
fit <- gam(y ~ s(sqrt(x.c)) + x.na, data=j0)
summary(fit) # this is actually fit 2
# plot(fit) # plot 3 (this is actually fit 2)
detach()
```
Example 4
=========
Plot 2 renders because later recode error is fixed.
```{r example_4}
j0 <- mkdata()
attach(j0)
mx <- min(x, na.rm=TRUE)
fit <- gam(y ~ s(x), data=j0, subset= !is.na(x))
summary(fit)
plot(fit) # plot 1
fit <- gam(y ~ s(sqrt(x)), data=j0, subset= !is.na(x))
summary(fit)
plot(fit) #plot 2
mean(y[is.na(x)]) - mean(y[!is.na(x)]) # means calculation
# recode the missing values
j0$x.na <- is.na(x)
j0$x.c <- ifelse(j0$x.na, mx, x) # error in recode fixed
detach()
attach(j0)
fit <- gam(y ~ s(sqrt(x.c)) + x.na, data=j0)
summary(fit)
plot(fit) # plot 3
detach()
```
> require(knitr); knit('reproduce.Rmd', encoding='UTF-8');
Loading required package: knitr
processing file: reproduce.Rmd
|...... | 9%
ordinary text without R code
|............ | 18%
label: setup
|.................. | 27%
ordinary text without R code
|........................ | 36%
label: example_1
|.............................. | 45%
ordinary text without R code
|................................... | 55%
label: example_2
|......................................... | 64%
ordinary text without R code
|............................................... | 73%
label: example_3
|..................................................... | 82%
ordinary text without R code
|........................................................... | 91%
label: example_4
|.................................................................| 100%
ordinary text without R code
output file: reproduce.md
[1] "reproduce.md"
j0$x.na <- is.na(x)
j0$x.c <- ifelse(x.na, mx, x) # ERROR in recode
j0$x.c <- ifelse(j0$x.na, mx, x)