html - knit sjplot as html output
问题描述
I am using 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, we can use sjtab to knit tables into a html file: (this is what I want)
```{r}
library(dplyr)
library(sjPlot)
library(sjmisc)
data(efc)
efc %>%
group_by(e16sex, c172code) %>%
select(e42dep, n4pstu, e16sex, c172code) %>%
sjtab(fun = "xtab")
```
I don't know how to incorporate this sjtab into my user function to generate the plots. I also looked through this documentation, but could not find the solution. Right now, I have to take a screen shot of each table that appears my browser and paste the tables together. :(
Problem: I want to have all the tables in a single file (html, pdf or word) so that I can scroll down the file to view all the tables at once.
Could someone please let me know how I can render each of the tables into a single html file?
Here is the code from my rmarkdown file. It should run with no error.
---
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()
,因为它始终是一整html
页。而$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')
```
推荐阅读
- python - 为什么从另一个创建新对象的程序执行函数然后访问它会返回错误
- java - 在 jhipster java 中使用 JDL 更改数据库结构
- html - Firefox 和 Chrome 中的 div 对齐方式不同
- symfony - 默认语言环境在 symfony 4 中没有改变
- bash - Bash - 传递给 rm 一个变量中的文件名,但带有 ! 选项
- android - 如何在Hashmap中放置两个值
- c# - Unity C#将图像传输到另一个场景
- python - 使用 virtualenv 和 WSGI 在 apache 上设置 Django2、python3
- javascript - 如何在nodejs内部调用导出的函数?
- c# - c# 通过字符串名称获取和设置静态嵌套类中的变量