在 Knitr Markdown pdf 中使用 R Hmisc summary/summaryM latex 命令
Using R Hmisc summary/summaryM latex command within Knitr Markdown pdf
我一直在尝试让 Hmisc
latex.summary
和 latex.summaryM
示例在使用 RStudio 中的 Knitr
创建的 pdf 文档中工作。但是不断收到错误消息。示例数据为:
options(digits=3)
set.seed(173)
sex <- factor(sample(c("m","f"), 500, rep=TRUE))
country <- factor(sample(c('US', 'Canada'), 500, rep=TRUE))
age <- rnorm(500, 50, 5)
sbp <- rnorm(500, 120, 12)
label(sbp) <- 'Systolic BP'
units(sbp) <- "mmHg"
treatment <- factor(sample(c("Drug","Placebo"), 500, rep=TRUE))
sbp[1] <- NA
# Generate a 3-choice variable; each of 3 variables has 5 possible levels
symp <- c('Headache','Stomach Ache','Hangnail',
'Muscle Ache','Depressed')
symptom1 <- sample(symp, 500,TRUE)
symptom2 <- sample(symp, 500,TRUE)
symptom3 <- sample(symp, 500,TRUE)
Symptoms <- mChoice(symptom1, symptom2, symptom3, label='Primary Symptoms')
我想创建一个包含表格的 pdf 文档
tab1 <- summary(sex ~ treatment + Symptoms, fun=table)
tab2 <- summaryM(age + sex + sbp + Symptoms ~ treatment,
groups='treatment', test=TRUE)
我是 运行 R 版本 3.5.2 (2018-12-20),RStudio 1.1.463,Hmisc_4.2-0,并使用 [=17= 安装了 tinytex ].
经过几个小时的反复试验,我发现了方法,并在下面发布代码以防它对其他人有所帮助。
以下代码对我有用,注意;
当 Hmisc::units
属性用于防止以下 failed to compile
错误时 relsize
乳胶包的要求。
! Undefined control sequence.
<recently read> \smaller
mylatex
函数取自 ,是删除不需要的输出所必需的。
需要选项 file = ""
来防止错误
Error in system(comd, intern = TRUE, wait = TRUE) : 'yap' not found
Calls: <Anonymous> ... print -> print.latex -> show.latex -> show.dvi -> system
使用 where = "!htbp"
选项可确保表格保持在原处,不会浮动到页面顶部(默认情况下 where = "!tbp"
)https://tex.stackexchange.com/a/2282。
---
title: "Untitled"
author: "Author"
date: "15 April 2019"
output:
pdf_document:
extra_dependencies: ["relsize"]
---
```{r setup, include=FALSE}
library(Hmisc)
library(dplyr)
mylatex <- function (...) {
o <- capture.output(latex(file = "", where = "!htbp", ...))
# this will strip /all/ line-only comments; or if you're only
# interested in stripping the first such comment you could
# adjust accordingly
o <- grep('^%', o, inv=T, value=T)
cat(o, sep='\n')
}
```
```{r data}
# As in question above ...
```
Here is the first table
```{r tab1, results = "asis"}
tab1 <- summary(sex ~ treatment + Symptoms, fun=table)
mylatex(tab1)
```
Here is the second table
```{r tab2, results = "asis"}
tab2 <- summaryM(age + sex + sbp + Symptoms ~ treatment, test=TRUE)
mylatex(tab2)
```
我一直在尝试让 Hmisc
latex.summary
和 latex.summaryM
示例在使用 RStudio 中的 Knitr
创建的 pdf 文档中工作。但是不断收到错误消息。示例数据为:
options(digits=3)
set.seed(173)
sex <- factor(sample(c("m","f"), 500, rep=TRUE))
country <- factor(sample(c('US', 'Canada'), 500, rep=TRUE))
age <- rnorm(500, 50, 5)
sbp <- rnorm(500, 120, 12)
label(sbp) <- 'Systolic BP'
units(sbp) <- "mmHg"
treatment <- factor(sample(c("Drug","Placebo"), 500, rep=TRUE))
sbp[1] <- NA
# Generate a 3-choice variable; each of 3 variables has 5 possible levels
symp <- c('Headache','Stomach Ache','Hangnail',
'Muscle Ache','Depressed')
symptom1 <- sample(symp, 500,TRUE)
symptom2 <- sample(symp, 500,TRUE)
symptom3 <- sample(symp, 500,TRUE)
Symptoms <- mChoice(symptom1, symptom2, symptom3, label='Primary Symptoms')
我想创建一个包含表格的 pdf 文档
tab1 <- summary(sex ~ treatment + Symptoms, fun=table)
tab2 <- summaryM(age + sex + sbp + Symptoms ~ treatment,
groups='treatment', test=TRUE)
我是 运行 R 版本 3.5.2 (2018-12-20),RStudio 1.1.463,Hmisc_4.2-0,并使用 [=17= 安装了 tinytex ].
经过几个小时的反复试验,我发现了方法,并在下面发布代码以防它对其他人有所帮助。
以下代码对我有用,注意;
当 Hmisc::units
属性用于防止以下 failed to compile
错误时 relsize
乳胶包的要求。
! Undefined control sequence.
<recently read> \smaller
mylatex
函数取自
需要选项 file = ""
来防止错误
Error in system(comd, intern = TRUE, wait = TRUE) : 'yap' not found
Calls: <Anonymous> ... print -> print.latex -> show.latex -> show.dvi -> system
使用 where = "!htbp"
选项可确保表格保持在原处,不会浮动到页面顶部(默认情况下 where = "!tbp"
)https://tex.stackexchange.com/a/2282。
---
title: "Untitled"
author: "Author"
date: "15 April 2019"
output:
pdf_document:
extra_dependencies: ["relsize"]
---
```{r setup, include=FALSE}
library(Hmisc)
library(dplyr)
mylatex <- function (...) {
o <- capture.output(latex(file = "", where = "!htbp", ...))
# this will strip /all/ line-only comments; or if you're only
# interested in stripping the first such comment you could
# adjust accordingly
o <- grep('^%', o, inv=T, value=T)
cat(o, sep='\n')
}
```
```{r data}
# As in question above ...
```
Here is the first table
```{r tab1, results = "asis"}
tab1 <- summary(sex ~ treatment + Symptoms, fun=table)
mylatex(tab1)
```
Here is the second table
```{r tab2, results = "asis"}
tab2 <- summaryM(age + sex + sbp + Symptoms ~ treatment, test=TRUE)
mylatex(tab2)
```