1

这里的初学者问题很抱歉,如果这是一个明显的错误。我使用summary_tableR 包qwraps2中的函数生成了一个汇总统计表。当我在 markdown 中运行代码时,它会按预期生成表格,但是当我尝试将 rmd 编织到 html 以在 rpubs 上发布时,它看起来如下所示:

## |Summary Statistics               |ESS (N = 73)                    |
## |:--------------------------------|:-------------------------------|
## |**Alcohol drunk at the weekend** |                      |
## |   variable            |interval                        |

等等...

(该表应类似于这些表:https ://cran.r-project.org/web/packages/qwraps2/vignettes/summary-statistics.html )

我知道我已经将 qwraps2_markup 编码为等于 'markdown' 但我不确定如何在不这样做的情况下创建同一个表。

```{r}

options(qwraps2_markup = 'markdown')
our_summary <-
  list("Alcohol drunk at the weekend" = 
         list("variable" =~ c("interval"),
              "min" = ~ min(ESS$alcwknd),
              "median" = ~ median(ESS$alcwknd),
              "max" = ~ max(ESS$alcwknd),
              "mean (sd)" = ~ qwraps2::mean_sd(ESS$cgtsday)),
       "Cigarettes smoked" =
         list("variable" =~ c("interval"),
              "min" = ~ min(ESS$cgtsday),
              "median" = ~ median(ESS$cgtsday),
              "max" = ~ max(ESS$cgtsday),
              "mean (sd)" = ~ qwraps2::mean_sd(ESS$cgtsday)),
       "Age" = 
         list("variable" =~ c("Interval"),
              "min" = ~ min(ESS$agea),
              "median" = ~ median(ESS$agea),
              "max" = ~ max(ESS$agea),
              "mean (sd)" = ~ qwraps2::mean_sd(ESS$agea)),
       "Time helping others" =
         list("variable" =~ c("Ratio"),
              "mode" =~ time_mode),
        "Felt Depressed" =
         list("variable" =~ c("Ordinal"),
              "mode" =~ dpr_mode),
       "Main Activity" =
         list("variable" =~ c("Categorical"),
              "mode" =~ act_mode))


tab <- summary_table(ESS, our_summary)
print(tab, rtitle = "Summary Statistics")
```

任何帮助都非常感谢,并提前感谢。

4

1 回答 1

1

如问题下的评论中所述,此问题的解决方案是为 knitr 设置正确的代码块选项。knitr 中的默认设置是报告代码块的results = 'markup'结果,其结果看起来像 R 控制台。 results = "asis"将“将 R 的原始结果写入输出文档”,这意味着将 markdown 放入 .Rmd 或将 Latex 放入 .tex 文件。

```{r, results = "asis"}

options(qwraps2_markup = 'markdown')
our_summary <-
  list("Alcohol drunk at the weekend" = 
         list("variable" =~ c("interval"),
              "min" = ~ min(ESS$alcwknd),
              "median" = ~ median(ESS$alcwknd),
              "max" = ~ max(ESS$alcwknd),
              "mean (sd)" = ~ qwraps2::mean_sd(ESS$cgtsday)),
       "Cigarettes smoked" =
         list("variable" =~ c("interval"),
              "min" = ~ min(ESS$cgtsday),
              "median" = ~ median(ESS$cgtsday),
              "max" = ~ max(ESS$cgtsday),
              "mean (sd)" = ~ qwraps2::mean_sd(ESS$cgtsday)),
       "Age" = 
         list("variable" =~ c("Interval"),
              "min" = ~ min(ESS$agea),
              "median" = ~ median(ESS$agea),
              "max" = ~ max(ESS$agea),
              "mean (sd)" = ~ qwraps2::mean_sd(ESS$agea)),
       "Time helping others" =
         list("variable" =~ c("Ratio"),
              "mode" =~ time_mode),
        "Felt Depressed" =
         list("variable" =~ c("Ordinal"),
              "mode" =~ dpr_mode),
       "Main Activity" =
         list("variable" =~ c("Categorical"),
              "mode" =~ act_mode))


tab <- summary_table(ESS, our_summary)
print(tab, rtitle = "Summary Statistics")
```
于 2020-03-02T16:36:44.343 回答