Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

markdown - Format text inside R code chunk

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

```

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 
')) } 
cat("}")
```
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 = "
")
print("</div>")
```

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...