【问题标题】:Using R Hmisc summary/summaryM latex command within Knitr Markdown pdf在 Knitr Markdown pdf 中使用 R Hmisc summary/summaryM 乳胶命令
【发布时间】:2019-04-15 01:44:45
【问题描述】:

我一直在尝试让 Hmisc latex.summarylatex.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。

经过几个小时的反复试验,我发现了方法,并在下面发布代码以防它对其他人有所帮助。

【问题讨论】:

    标签: r latex r-markdown knitr hmisc


    【解决方案1】:

    以下代码适用于我,注意;

    使用Hmisc::units 属性时relsize 乳胶包的要求,以防止出现以下failed to compile 错误。

    ! Undefined control sequence.
    <recently read> \smaller
    

    mylatex 函数取自 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}
    
    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)
    
    ```
    

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多