简体   繁体   English

在 R Markdown 块中缩进(PDF 输出)

[英]Indenting in an R Markdown chunk (PDF output)

I would like to indent the result of the code output produced by an R Markdown chunk (PDF output).我想缩进由 R Markdown 块(PDF 输出)生成的代码 output 的结果。 I have many chunks in my file but I would like to increase the indent ONLY on a specific chunk output results.我的文件中有很多块,但我只想在特定块 output 结果上增加缩进。 For example, I need to alter the indent once I invoke a data.frame print-out.例如,我需要在调用 data.frame 打印输出后更改缩进。

I tried indent= " " chunk option but it doesn't work well because the graph that I also produce in the same chunk strangely dissapeared from the PDF output.我尝试了 indent=" " 块选项,但效果不佳,因为我也在同一个块中生成的图形奇怪地从 PDF output 中消失了。

I tried to add a second chunk just to print the text output with the indent=" " chunk option but it also doesn't work very well because of the following reason:我尝试添加第二个块只是为了打印带有 indent="" 块选项的文本 output 但它也不能很好地工作,原因如下:

  1. Original chunk:原始块:
```{r, warning=FALSE, fig.height = 5, message=TRUE}
library(qualityTools)
b.data <- data.frame(cause = c("Cause 1","Cause 2","Cause 3","Cause 4","Cause 5","Cause 6","Cause 7"), 
        count = c(1,1,1,1,1,1,1),
        cost = c(0.0327,0.0078,0.0026,0.0162,0.0000,0.0000,0.0158))


b.vector <- b.data$cost
names(b.vector) <- b.data$cause

data.pareto <- paretoChart(b.vector, las = 2, col = "#65BBA9", border = "#000000", main = "Pareto Chart", ylab = "EUR", percentVec = c(0, 0.2, 0.4, 0.6, 0.80, 1), showTable = FALSE)

text output:文字 output:

EUR 0.0327 0.0162 0.0158 0.0078 0.0026 0 0欧元 0.0327 0.0162 0.0158 0.0078 0.0026 0 0

Cum.暨。 EUR 0.0327 0.0489 0.0647 0.0725 0.0751 0.0751 0.0751欧元 0.0327 0.0489 0.0647 0.0725 0.0751 0.0751 0.0751

Percentage 43.5% 21.6% 21.0% 10.4% 3.5% 0.0% 0.0%百分比 43.5% 21.6% 21.0% 10.4% 3.5% 0.0% 0.0%

Cum.暨。 Percentage 43.5% 65.1% 86.2% 96.5% 100.0% 100.0% 100.0%百分比 43.5% 65.1% 86.2% 96.5% 100.0% 100.0% 100.0%

  1. Additional chunk附加块
```{r, warning=FALSE, fig.height = 5, message=TRUE, indent="      "}
data.pareto

text output:文字 output:

EUR 0.03270 0.01620 0.01580 0.00780 0.002600 0.0000 0.0000欧元 0.03270 0.01620 0.01580 0.00780 0.002600 0.0000 0.0000

Cum.暨。 EUR 0.03270 0.04890 0.06470 0.07250 0.075100 0.0751 0.0751欧元 0.03270 0.04890 0.06470 0.07250 0.075100 0.0751 0.0751

Percentage 43.54194 21.57124 21.03862 10.38615 3.462051 0.0000 0.0000百分比 43.54194 21.57124 21.03862 10.38615 3.462051 0.0000 0.0000

Cum.暨。 Percentage 43.54194 65.11318 86.15180 96.53795 100.000000 100.0000 100.0000百分比 43.54194 65.11318 86.15180 96.53795 100.000000 100.0000 100.0000

So the text output in 2) is formatted differently than text output of 1).因此,2) 中的文本 output 的格式不同于 1) 中的文本 output。

I would like to preserve the formatting of 1).我想保留 1) 的格式。

It sounds like the indent works for you, with the side effect of losing the graph.听起来缩进对你有用,但副作用是丢失了图表。 Can't you just add a separate chunk for creating the graph?你不能只添加一个单独的块来创建图表吗?

I partially solved my problem by using kable table output of the data.pareto data frame.我通过使用 data.pareto 数据帧的 kable 表 output 部分解决了我的问题。 By using the position = "left" OR position = "center" option of kable I was able to alter the indent of the print-out.通过使用 kable 的 position = "left" 或 position = "center" 选项,我能够更改打印输出的缩进。 However this is not the optimal solution because I need more fine adjustment of the indent.但是,这不是最佳解决方案,因为我需要对缩进进行更精细的调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM