我一直在尝试让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,并且已经使用tinytex::install_tinytex()安装了tinytex。
经过几个小时的试验和错误,我发现了如何做到这一点,并将下面的代码张贴出来,以防对他人有所帮助。
发布于 2019-04-15 09:44:46
请注意,以下代码适用于我;
使用Hmisc::units属性时对relsize latex包的要求,以防止以下failed to compile错误。
! Undefined control sequence.
<recently read> \smallermylatex函数取自https://stackoverflow.com/a/31443576/4241780,是删除不需要的输出所必需的。
需要使用选项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}库(Hmisc)
库(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 accordinglyo <- grep('^%', o, inv=T, value=T)cat(o, sep='\n')}
```{r data}如上所述..。
Here is the first table
```{r tab1, results = "asis"}tab1 <-摘要(性~治疗+症状,fun=table)
mylatex(tab1)
Here is the second table
```{r tab2, results = "asis"}tab2 <- summaryM(年龄+性别+收缩压+症状~治疗,test=TRUE)
mylatex(tab2)
https://stackoverflow.com/questions/55681671
复制相似问题