Recently, because of the need to export the output results to word, I saw this article
How to export your R statistical results to Word elegantly?
But when I load " library(flextable) ", I keep reporting the following error
Error: package or namespace load failed for 'flextable' in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]): namespace 'xfun' 0.38 is already loaded, but >= 0.39 is required Traceback:
I thought I install.packages("xfun")
could update this package to solve this problem. When updating, it shows "permission denied", and then I still can't run it as an administrator.
Finally found this answer , first remove in install
remove.packages("xfun")
install.packages("xfun")
#或者install_version("xfun", version = "0.39",repos = "http://cran.us.r-project.org")
Take the data iris (it seems to be the built-in data, no need to load other packages, or try to build a data frame by yourself) as an example
library(xtable)
library(flextable)
library(officer)
m4 = as_flextable(xtable(iris))
doc = read_docx()
doc = body_add_flextable(doc,m4)
print(doc,"./m4.docx")
The export result is as follows.
I wrote it as a function and set the default output to three decimal places.
set_flextable_defaults(digits = 3)#这里设置默认输出三位小数
word<-function(x){
m4 = as_flextable(xtable(x)
print(m4)
doc = read_docx()
doc = body_add_flextable(doc,m4)
print(doc,"./m4.docx")
}
The output is as follows:
quite satisfactory