Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将R标记中的三向列联表打印到docx_R_Markdown_R Markdown - Fatal编程技术网

将R标记中的三向列联表打印到docx

将R标记中的三向列联表打印到docx,r,markdown,r-markdown,R,Markdown,R Markdown,我正在尝试使用R markdown创建一个包含三向列联表的报告。我可以使用ftable()非常轻松地创建表,但它们的格式不适合在markdown中打印 输出必须是DOCX文档。因此,任何设计为HTML或LaTeX格式的东西对我都没有用处 knitr中的kable()函数不适用于ftables,许多其他选项只将其打印为数据框。当将ftable转换为数据帧时,会弄乱它的布局,因此这些也没有帮助 我创建了一个使用ftable创建的三向表的可复制示例,该表的大小应大致与我需要输出的表的大小相同: lib

我正在尝试使用R markdown创建一个包含三向列联表的报告。我可以使用ftable()非常轻松地创建表,但它们的格式不适合在markdown中打印

输出必须是DOCX文档。因此,任何设计为HTML或LaTeX格式的东西对我都没有用处

knitr中的kable()函数不适用于ftables,许多其他选项只将其打印为数据框。当将ftable转换为数据帧时,会弄乱它的布局,因此这些也没有帮助

我创建了一个使用ftable创建的三向表的可复制示例,该表的大小应大致与我需要输出的表的大小相同:

library(tidyverse)
# this data set is included in dplyr
data("starwars")
data=starwars
#the next couple of lines are just me trimming down the information to be used in the table
data=data %>% filter(species=="Human" | species=="Droid" | species=="Zabrak" | species=="Mirialan" | species=="Wookiee")
tabledata=data[,c(8:10)]
#here's the actual table
table1 = ftable(tabledata,col.vars=c(3,1))
table1
如果可能的话,我希望有一个相当简单的解决方案(可能是一个为我做这件事的软件包,或者别的什么)。此报告将包含许多表,因此执行过于复杂的操作将花费太长时间(而且,我最近才开始使用R)


提前感谢你们提供的任何帮助

您可以使用
htmlTable
(例如在中)做更多漂亮的事情,但这里有一个快速而肮脏的解决方案,可以在R标记中使用
format\u html
from
memisc
包:

```{r test, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(tidyverse)
library(memisc)
library(knitr)

tabledata <- structure(list(gender = c("male", NA, NA, "male", "female", "male", 
"female", NA, "male", "male", "male", "male", "male", "male", 
"male", "male", "male", "male", "none", "male", "male", "female", 
"male", "male", "male", "female", "male", "male", "male", "male", 
"female", "male", "female", "female", "female", "male", "male", 
"male", "female", "male", "male", "male", "female", "male", "none", 
"female"), homeworld = c("Tatooine", "Tatooine", "Naboo", "Tatooine", 
"Alderaan", "Tatooine", "Tatooine", "Tatooine", "Tatooine", "Stewjon", 
"Tatooine", "Eriadu", "Kashyyyk", "Corellia", "Corellia", "Bestine IV", 
"Naboo", "Kamino", NA, "Socorro", "Bespin", "Chandrila", NA, 
NA, "Coruscant", "Tatooine", "Dathomir", "Haruun Kal", "Iridonia", 
"Naboo", "Naboo", "Tatooine", "Mirial", "Mirial", "Naboo", "Serenno", 
"Alderaan", "Concord Dawn", "Coruscant", "Kashyyyk", "Alderaan", 
NA, NA, NA, NA, "Naboo"), species = c("Human", "Droid", "Droid", 
"Human", "Human", "Human", "Human", "Droid", "Human", "Human", 
"Human", "Human", "Wookiee", "Human", "Human", "Human", "Human", 
"Human", "Droid", "Human", "Human", "Human", "Human", "Human", 
"Human", "Human", "Zabrak", "Human", "Zabrak", "Human", "Human", 
"Human", "Mirialan", "Mirialan", "Human", "Human", "Human", "Human", 
"Human", "Wookiee", "Human", "Human", "Human", "Human", "Droid", 
"Human")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-46L))

table1 = ftable(tabledata,col.vars=c(3,1))

knitr::asis_output(htmltools::htmlPreserve(
   format_html(table1,
               show.titles=TRUE,
               digits=0,
               format="f",
               toprule=2,midrule=1,bottomrule=2,
               split.dec=TRUE,
               style = ftable_format_stdstyle,
               margin="2ex auto", 
               varontop,varinfront)
))
```
`{r测试,echo=FALSE,message=FALSE,warning=FALSE,results='asis'}
图书馆(tidyverse)
图书馆(memisc)
图书馆(knitr)

tabledata您是否愿意接受稍微不同的ftable表示形式?例如,这里有一个使用flextable的解决方案。这在Word中呈现得非常好。。。去年,我使用flextable在一个500页的文档中构建了这些表

library(flextable)
library(dplyr)
library(magrittr)

table1 = ftable(tabledata,col.vars=c(3,1))

data.frame(table1) %>% spread(species, Freq) %>% 
     regulartable %>% merge_v(j = ~homeworld) %>% autofit()

这似乎会以HTML格式输出一个表,这对我没有帮助。不幸的是,我需要这是一个docx文件格式。这工作!哈哈,现在我只需要学习如何使用这个软件包。