编织 sjplot 作为 html 输出

knit sjplot as html output

我正在使用 sjPlot::tab_xtab to create some tables in rmarkdown (they look great!). However, the tables are generated as individual html pages in my browser, and not as a single html file. Similar to this problem here.

According to the documentation,我们可以用sjtab把tables编织成一个html文件:(这就是我想要的)

```{r}
library(dplyr)
library(sjPlot)
library(sjmisc)
data(efc)
 
efc %>% 
  group_by(e16sex, c172code) %>% 
  select(e42dep, n4pstu, e16sex, c172code) %>% 
  sjtab(fun = "xtab")
```

我不知道如何将此 sjtab 合并到我的用户函数中以生成绘图。 我也是looked through this documentation,但是没找到解决方法。现在,我必须对出现在浏览器中的每个 table 进行屏幕截图,然后将 table 粘贴在一起。 :(

问题: 我想将所有 table 放在一个文件(html、pdf 或 word)中,以便我可以向下滚动文件以一次查看所有 table。

有人可以告诉我如何将每个 table 渲染成 single html file 吗?

这是我的 rmarkdown 文件中的代码。它应该 运行 没有错误。

---
title: "testChi"
author: "g"
date: "10/15/2021"
output:
  pdf_document: default
  word_document: default
  html_document: default
---



```{r setup, include=FALSE, error=FALSE, warning=FALSE}
library(magrittr)
library(dplyr)
library(readr)
library(pander)



# cols <- svy_final %>% select(matches("Q44_[[:digit:]]")) %>% names(.)
cols <- mtcars %>% select(matches("mpg|cyl|disp")) %>% names(.)

create_plots <- function(dat,title_name) {

  # school <- enquo()
 for (i in cols) {
    plt <- sjPlot::tab_xtab(
      var.row =  dat$gear,
      var.col = dat[[i]],
      show.row.prc = TRUE,
      var.labels = c("gear size", i),
      title = title_name
    )
    # return(out)
    print(plt)
  }
}

```




```{r loop_print, results = 'asis',error=FALSE, warning=FALSE, message=FALSE}

create_plots(mtcars, 'gear and stuff') %>% pander()


```

我得到了你的降价 运行,我可以看出问题所在。

对我来说,下面的解决方法有效。 它基本上是您的代码,只是每个图都没有在循环中调用,而是保存到列表中。之后,您只需在不使用循环的情况下从列表 l[[1]]l[[2]]l[[3]] 中分别调用每个图。

每当您在循环中调用绘图时,您的问题似乎都会出现。

---
title: "testChi"
author: "g"
date: "10/15/2021"
output:
  html_document: default
---

``````{r packages, include = F}
library(magrittr)
library(dplyr)
library(readr)
library(pander)
```


```{r tables, , print = F, echo = F, results="asis"}

cols <- mtcars %>% select(matches("mpg|cyl|disp")) %>% names(.)
dat <- mtcars
title_name <- 'gear and stuff'

l = list()
i = 1;

for (x in cols) {
      plt <- sjPlot::tab_xtab(
      var.row =  dat$gear,
      var.col = dat[[x]],
      show.row.prc = TRUE,
      var.labels = c("gear size", x),
      title = title_name)
      l[[i]] <- plt
      i = i+1
}

l[[1]]
l[[2]]
l[[3]]

```

结果(所有图都在下面):

这里还有一个不用分别调用每个元素的版本:

---
title: "testChi"
author: "g"
date: "10/15/2021"
output:
  html_document: default
---

``````{r packages, include = F}
library(magrittr)
library(dplyr)
library(readr)
library(pander)
```


```{r tables, , print = F, echo = F, results="asis"}

cols <- mtcars %>% select(matches("mpg|cyl|disp")) %>% names(.)
dat <- mtcars
title_name <- 'gear and stuff'

i = 1;
c <- character()

for (x in cols) {
      plt <- sjPlot::tab_xtab(
      var.row =  dat$gear,
      var.col = dat[[x]],
      show.row.prc = TRUE,
      var.labels = c("gear size", x),
      title = title_name)
      c <-  paste(c, plt$knitr)
}

knitr::raw_html(c)


```

我认为通常您必须避免在 tab_xtab() 的 return 对象上使用单独的打印语句,因为它始终是整个 html 页面。 return 对象中的 $knitr 不是整个 html 页面,而只是表格的 html 代码。

另一个版本完全适合带有函数的初始示例:

  ---
title: "testChi"
author: "g"
date: "10/15/2021"
output:
  html_document: default
---



```{r setup, include=FALSE, error=FALSE, warning=FALSE}
library(magrittr)
library(dplyr)
library(readr)
library(pander)


cols <- mtcars %>% select(matches("mpg|cyl|disp")) %>% names(.)

create_plots <- function(dat,title_name) {

 c <- character()
 for (i in cols) {
    plt <- sjPlot::tab_xtab(
      var.row =  dat$gear,
      var.col = dat[[i]],
      show.row.prc = TRUE,
      var.labels = c("gear size", i),
      title = title_name
    )
  c <-  paste(c, plt$knitr)

 }
 knitr::raw_html(c)

}

```

```{r loop_print, results = 'asis',error=FALSE, warning=FALSE, message=FALSE}
create_plots(mtcars, 'gear and stuff')
```

```{r loop_print2, results = 'asis',error=FALSE, warning=FALSE, message=FALSE}
create_plots(mtcars, 'other stuff')
```