R手册(Communicate)--R Markdown


R Markdown是通过R语言制作动态文档的文件格式
Cheat Sheet: R Markdown

Overview

Installation

  • 如果你在RStudio中工作,那么你可以简单地安装当前版本的RStudio(包括rmarkdown包和pandoc)。
  • 如果您想在RStudio之外使用rmarkdown软件包
    install.packages("rmarkdown")

Workflow

  1. 编辑 R Markdown文件(Rmd 或 纯文本):
    Rstudio菜单 File ▶ New File ▶ R Markdown
  2. 渲染:use knitr button or render() to knit
  3. 发布到网站(可选)
  4. 重复使用

Rmd Structure

R Markdown主要由三部分组成:

  1. YAML标头:文档开始部分,由---包围的key:value
  2. Text:主要由Markdown语法书写的文本
  3. Knitr 处理 R代码块
    ```{r}
    code
    ```

YAML Header

Parameters

  1. Add parameters: 在YAML Header中定义
  2. Call parameters: 在代码块中以params$<name>格式引用
  3. Set parameters: Knit或者render() 时重设参数
# YAML Header
---
params:
 num: 100
 date: !r Sys.Date()
---
# R Markdown文本
Today’s date is `r params$d`
# 渲染
render("doc.Rmd", params = list(n = 1,d = as.Date("2015-01-01"))

Set render options with YAML

---
output: html_document
---
Body
output valuecreates
html_documenthtml
pdf_documentpdf (requires Tex )
word_documentMicrosof Word (.docx)
odt_documentOpenDocument Text
rtf_documentRich Text Format
md_documentMarkdown
github_documentGithub compatible markdown
ioslides_presentation ioslidesHTML slides
slidy_presentationslidy HTML slides
beamer_presentationBeamer pdf slides (requires Tex)

ouput 也可以设置子选项,详情请查看官网:

---
output: 
  html_document:
    code_folding: hide
    toc: TRUE
    theme: united
    highlight: zenburn
---
Body

初始文档信息

title, author, and date information,etc

---
title: "Sample Document"
author: "Hadley Wickham"
---

Text

更多语法请参考 GitHub Flavored Markdown 规范

Embed code with knitr syntax

Inline code
`r <code>`格式插入,将以文本的格式显示

Code chunks
以 ```{r options} code ```围绕的一行或多行代码

chunks options说明
cache为knits缓存结果 (default = FALSE)
cache.path缓存结果的保存路径 (default = “cache/”)
childfile(s) to knit and then include (default =NULL)
collapse把所有的输出汇聚到单个块中(default = FALSE)
comment结果的每一行加前缀(default = ‘#’)
dependsonchunk dependencies for caching (default = NULL)
echo是否在输出中包含源代码
engine代码语言 (default =‘R’)
error是否在输出中包含错误信息(TRUE或FALSE)
eval在块中运行代码(default = TRUE)
fig.align‘lef’, ‘right’, or ‘center’ (default = ‘default’)
fig.cap图片标签 (default= NULL)
fig.height, fig.width图片宽度和高度(英寸)
highlight高亮显示源码 (default = TRUE)
include运行后是否在文档中显示块 (default = TRUE)
message是否在输出中包含参考的信息(TRUE或FALSE)
results是否输出原生结果 (default = ‘markup’){‘markup’,‘asis’,‘hide’,‘hold’}
warning是否在输出中包含警告(default = TRUE)

Global options
knitr::opts_chunk$set() 等函数设置

knitr::opts_chunk$set(echo = TRUE)

表格美化

data <- faithful[1:4, ]

knitr::kable(data, caption = "Table with kable”)
print(xtable::xtable(data, caption = "Table with xtable”),
   type = "html", html.table.attributes = "border=0")) 
stargazer::stargazer(data, type = "html", title = "Table
   with stargazer")

render(渲染)

render()knitr button
运行时先用Knitr将Rmd文件中的代码块编译成md文件
然后再用pandoc将md文件转化为HTML, PDF或 Word等最终文档(the default is HTML)

render("input.Rmd")
render("input.md")

render函数主要参数:
input : file to render
output_format: 输出格式
output_options: 要渲染的选项list (in YAML)
output_file, output_dir : 输出文件名和路径
params: list of params to use
envir : environment to evaluate code chunks in
encoding - of input file

Output Formats: str or Output Format Function
all: 输出全部格式
Output Format Functions:
html_document
pdf_document
word_document
md_document
beamer_presentation
ioslides_presentation
slidy_presentation

render("input.Rmd", html_document(toc = TRUE))
render("input.Rmd", pdf_document(latex_engine = "lualatex"))
render("input.Rmd", beamer_presentation(incremental = TRUE))
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值