Ros*_*ler 6 plot r rstudio knitr r-markdown
我正在使用:Ubuntu 12.04 64位,R 3.0.2,RStudio 0.98.312,knitr 1.5,markdown 0.6.3,mgcv1.7-27
我有一个带有多个代码块的Rmarkdown文档.在一个块的中间有一些代码,我适合GAM,总结拟合并绘制拟合.问题是第一个绘图渲染到输出文件,但第二个绘图没有.这是来自块的已清理代码片段:
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)])
Run Code Online (Sandbox Code Playgroud)
所有内容都按预期呈现,除了输出直接从回显第二个绘图语句到回显下面的均值计算.均值计算的结果正确呈现.
如果我注释掉另一个图块,稍后会在块中调用7行,则会正确呈现丢失的图.
有没有人对这里发生的事情有任何建议?
更新下面
总结 - 在调用Plot 2之后的几行中有一些R代码生成一个执行错误(未找到变量),之后有几行代表Plot 3.如果代码错误被修复,那么渲染Plot 2.如果代码错误不固定并且对Plot 3的调用被注释掉,则渲染Plot 2.问题取决于用于存储不同拟合结果的相同变量"拟合".如果我将每个拟合分配给不同的变量,则绘图2呈现正常.
我不明白在多行成功执行的代码之后进行的更改如何(显然是回顾性地)阻止了绘图2的渲染.
可重复的例子:
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()
```
Run Code Online (Sandbox Code Playgroud)
日志文件:
> 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"
Run Code Online (Sandbox Code Playgroud)
你只是另一个受害者attach(),尽管人们一直警告不要使用attach().搞砸太容易了attach().你这样做之后attach(j0):
j0$x.na <- is.na(x)
j0$x.c <- ifelse(x.na, mx, x) # ERROR in recode
Run Code Online (Sandbox Code Playgroud)
当然,R找不到对象,x.na因为它不存在于任何地方.是的,它j0现在已经存在,但除非您拆卸j0并重新安装,否则它不会暴露给R. 换句话说,attach()当您添加更多变量时,不会自动刷新自身j0.所以简单的解决方法是:
j0$x.c <- ifelse(j0$x.na, mx, x)
Run Code Online (Sandbox Code Playgroud)
我理解你为什么要使用attach()- 你可以避免在j0$任何地方使用笨拙的前缀,但你需要非常小心.除了我提到的问题,detach()也是不好的,因为你没有指定脱离其环境,默认情况下,搜索路径上第二个被分离,这是不是一定是你连接的一个,例如,你可能已经加载其他包到搜索路径上.因此,你必须明确:detach('j0').
回到knitr:如果你想知道,我可以解释发生了什么,但首先,你必须确保你的代码在传递之前确实有效knitr.当错误被消除时,你观察到的奇怪现象也会消失.