Format text inside R code chunk

2019-01-14 14:03发布

问题:

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")

```

Output is either HTML (ioslides) or PDF (Beamer)

Update:

Currently the code above will only give something like the following

6683209
1268680
8412827
9688104
6958695
9655315
3255629
8754025
3775265
2810182

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:

http://tex.aspcode.net/view/635399273629833626273734/dynamically-format-labelscolumns-of-a-latex-table-generated-in-rknitrxtable

But I don't know about text size and color.

Update 2:

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("}")
```

回答1:

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>")
```

Hope it helps.



回答2:

It sounds as if you want the output to be either PDF or HTML. One possibility is the xtable package. It produces tables either in PDF or HTML format. There's no (output-independent) way to specify colour, however. Here's an example.

xt <- xtable(data.frame(a=1:10))
print(xt, type="html")
print(xt) # Latex default

Another option is the pandoc.table function from the pander package. You need the pandoc binary installed. If you have RStudio, you have this already. The function spits out some markdown which then can be converted to HTML or PDF by pandoc.

Here's how you could use this from RStudio. Create an RMarkdown document like this:

---
title: "Untitled"
author: "You"
date: "20 November 2014"
output: html_document
---


```{r,  results='asis'}
library(pander)
tmp <- data.frame(a=1:10,b=1:10)
pandoc.table(tmp)
```

When you click "knit HTML", it will spit out a nice HTML document. If you change output to pdf_document, it will spit out a nice PDF. You can edit the options to change output - e.g.

pandoc.table(tmp, emphasize.strong.rows=c(2,4,6,8,10))

and this will work both in PDF or HTML. (Still no options to change colour though. Homework task: fix pandoc.table to allow arbitrary colours.)

Under the hood, knitr is writing markdown, and pandoc is converting the markdown to whatever you like.