在R代码块中格式化文本 [英] Format text inside R code chunk
问题描述
我正在按照以下说明在Rstudio中制作一些幻灯片: http://rmarkdown.rstudio.com/beamer_presentation_format.html
I am making some slides inside Rstudio following instructions here: http://rmarkdown.rstudio.com/beamer_presentation_format.html
如何在数字分成两列的同时定义文本大小,颜色和流量"?
How do I define text size, colors, and "flow" following numbers into two columns?
```{r,results='asis', echo=FALSE}
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
```
输出为HTML(幻灯片)或PDF(Beamer)
Output is either HTML (ioslides) or PDF (Beamer)
更新:
当前,上面的代码只会给出类似以下的内容
Currently the code above will only give something like the following
6683209
1268680
8412827
9688104
6958695
9655315
3255629
8754025
3775265
2810182
我无法做任何更改文本大小,颜色或将其放入表格的操作. R codechunk的输出只是纯文本.如这篇文章所述,也许确实可以将它们放在表格中:
I can't do anything to change text size, color or put them into a table. The output of R codechunk is just plain text. Maybe it is possible to put them in a table indeed, as mentioned at this post:
但是我不知道文本的大小和颜色.
But I don't know about text size and color.
更新2:
将原生HTML代码编织到R输出的想法非常有用.我还没想到但是,这仅在我要输出HTML的情况下有效.对于PDF输出,我必须将原始的Latex代码与R输出进行编织.例如,以下代码使用"knitr PDF"输出工作:
The idea weaving native HTML code to R output is very useful. I haven't thought of that. This however only works if I want to output HTML. For PDF output, I have to weave the native Latex code with R output. For example, the code following works using "knitr PDF" output:
```{r,results='asis', echo=FALSE}
cat("\\textcolor{blue}{")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
for (n in rd) {
cat(paste0(n, '\\newline \n')) }
cat("}")
```
推荐答案
您正在使用results ='asis',因此,您可以简单地使用print()
和格式化标记.如果您希望文本为红色,只需执行以下操作:
You are using results='asis', hence, you can simply use print()
and formatting markup. If you want your text to be red, simply do:
```{r,results='asis', echo=FALSE}
print("<div class='red2'>")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
print("</div>")
```
希望有帮助.
这篇关于在R代码块中格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!