在windows上使用Rstudio写Rmarkdown文档。Rmarkdown文档可以被转为html,word,pdf,markdown.
需要先安装miktex,配置好环境变量。https://miktex.org/download (如果没有安装,pdf输出会报错)
因为pdf的输出需要调用Latex排版系统,如果没有安装Latex就会报错。而且对中文不友好,如果文档有中文,还得安装ctex.(目前没有解决此问题,仍然无法输出中文)
新建文件first.Rmd
文件内容如下,点击Knit
按钮,
---
title: "First R markdown"
author: "Pet Zhu"
date: "2021/5/12"
output:
pdf_document: default
html_document: default
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
> ----by Lu Xun
tinytex::install_tinytex()
## Rmmarkdwon markdown
library(rmarkdown)
md_document(
variant = "markdown_strict",
preserve_yaml = FALSE,
toc = FALSE,
toc_depth = 3,
fig_width = 7,
fig_height = 5,
fig_retina = NULL,
dev = "png",
df_print = "default",
includes = NULL,
md_extensions = NULL,
pandoc_args = NULL,
ext = ".md"
)
render("test.Rmd", md_document())
#render("test.Rmd", md_document(variant = "markdown_github"))
Rmarkdown可以输出的格式有
- beamer_presentation
- context_document
- github_document
- html_document
- ioslides_presentation
- latex_document
- md_document
- odt_document
- pdf_document
- powerpoint_presentation
- rtf_document
- slidy_presentation
- word_document
需要注意:会自动生成文件夹存放,Rmd生成的图片。
同时输出上面新建的first.Rmd到html,pdf,word格式
rmarkdown::render('first.Rmd',c('html_document','pdf_document','word_document'))
rmarkdown::render('first.Rmd',"all")
取决于你在first.Rmd里yaml定义的outpput的格式,会把这里面的格式都输出。
只输出到html
rmarkdown::render('first.Rmd',c('html_document'))
输出到markdown
rmarkdown::render('helloworld.Rmd',"md_document")
输出到github格式(会同时输出html,md格式)
rmarkdown::render('helloworld.Rmd',"github_document")
R有专门bookdown
和blogdown
,可以用Rstudio快速生成电子书和博客。