Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以RMarkdown/knitr格式打印边距_R_Rstudio_Knitr_R Markdown - Fatal编程技术网

以RMarkdown/knitr格式打印边距

以RMarkdown/knitr格式打印边距,r,rstudio,knitr,r-markdown,R,Rstudio,Knitr,R Markdown,在RStudio中,我的绘图看起来很完美,但是当我通过“编织HTML”按钮生成HTML时,我的边距被切断了 我正在使用基本图形包创建一个简单的条形图 我有一个较大的下页边距,用于容纳x轴上的垂直标签,还有一个较宽的左页边距,用于将y轴标题保留在一些大数字的左侧,例如: ```{r set-graph-options} par(mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2) ``` x轴和y轴标签/标题都会受到影响;使用默认的mgp设

在RStudio中,我的绘图看起来很完美,但是当我通过“编织HTML”按钮生成HTML时,我的边距被切断了

我正在使用基本图形包创建一个简单的条形图

我有一个较大的下页边距,用于容纳x轴上的垂直标签,还有一个较宽的左页边距,用于将y轴标题保留在一些大数字的左侧,例如:

```{r set-graph-options}
par(mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2)
```
x轴和y轴标签/标题都会受到影响;使用默认的
mgp
设置my
ylab
设置看起来很好,但是使用较大的值时,它似乎被推离了“画布”(或R中的任何术语)

我还注意到knitr/rmarkdown无法识别全局设置
par()
选项。例如,在设置上述选项后,
barplot(injury_top12,ylab=“Injuries”)
将无法识别
mar
mgp
las
选项,但如果我将这些选项复制到
barplot()
调用自身,
las=2
mgp=c(4,1,0)
开始工作,但是仍然无法识别
mar

我曾尝试使用命令
R-e“rmarkdown::render('Readme.Rmd')”
从命令行生成HTML,这显示了相同的问题

我使用的是R Studio 0.98.1103,knitr是1.11,rmarkdown是0.8,操作系统是ubuntu linux

示例.Rmd:

```{r echo = F, example-set-par}
library(knitr)
opts_knit$set(cache = FALSE, verbose = TRUE, global.par = TRUE)
par(mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2)
d <- c("This is a longer than usual label" = 355,
       "Another quite long label" = 144,
       "A third longish label for a barplot" = 222)
```

Plot one depends on values set by `par()`:

```{r echo = F, example-plot-using-par}
barplot(d, ylab = "Arbitrary numbers")
```

Plot two explicitly sets options:

```{r echo = F, example-plot-with-explicit-options}
barplot(d, ylab = "Arbitrary numbers", mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2)
```
`{r echo=F,示例集par}
图书馆(knitr)
OpthSKNKNET$SET(Cache=false,VBOSE = TRUE,GUALAL.PAL= TRUE)
par(mar=c(12,6,4,2)+0.1,mgp=c(4,1,0),las=2)

d对于希望直接将答案隐藏在评论和tweet中的人,您的
.Rmd
文件应该如下所示:

---
title: exemple
header of your Rmd
---

```{r}
library(knitr)
opts_knit$set(global.par = TRUE)
```
The important think in the previous chunk is  to put global.par=TRUE 

Plot one depends on values set by `par()`:

```{r}
par(mar=c(5,5,0,0)) #it's important to have that in a separate chunk
``` 
Now we just plot a point with the good margin set by the global `par()`

```{r}
plot(1,1,main="a plot with the margin as set globally")
```
如果不想在输出中包含用于设置全局边距的代码,只需添加:

```{r,include=FALSE}
par(mar=c(5,5,0,0))
``` 

在您不想包含的区块中。

通常在编写文档时,R代码会在新会话中进行计算,因此如果您设置
par
选项,则需要在文档中进行计算。也许你可以举一个可复制的例子?谢谢,@Gregor。当然,我正在文档中设置
par()
。我添加了一个例子。RMD和屏幕截图的输出。您可以看到感谢@ Yihui,我编辑我的帖子,以反映我的例子的当前状态。RMD,在添加“OPTSKNIKNET$SET(GUALAL.PAL= TRUE)”之后没有变化。我尝试了好几次&验证了没有涉及缓存,只是为了让自己相信这不是我这方面的愚蠢行为。您需要在单独的代码块中设置此选项,以便它可以应用于以下代码块。