├── .travis.yml ├── DESCRIPTION ├── NAMESPACE ├── R ├── inFigure.R ├── inoffice.R ├── misc.R ├── plot.R ├── toFigure.R ├── toTable.R ├── toffice.R ├── tohtml.R └── zzz.R ├── README.md ├── _config.yml ├── eoffice.Rproj ├── man ├── col2hex.Rd ├── convertplot.Rd ├── dot-getext.Rd ├── dot-getfilename.Rd ├── dot-setcolnames.Rd ├── dot-theme_blank.Rd ├── indocx.Rd ├── infigure.Rd ├── inoffice.Rd ├── inpptx.Rd ├── todocx.Rd ├── toffice.Rd ├── tofigure.Rd ├── tohtml.Rd ├── topptx.Rd ├── totable.Rd └── toxlsx.Rd └── vignettes └── eoffice.Rmd /.travis.yml: -------------------------------------------------------------------------------- 1 | #---------------------------------------------------------------- 2 | # Travis-CI configuration for R packages 3 | # 4 | # REFERENCES: 5 | # * Travis CI: https://docs.travis-ci.com/user/languages/r# 6 | # YAML validated using http://www.yamllint.com/ 7 | #---------------------------------------------------------------- 8 | language: r 9 | sudo: false 10 | cache: packages 11 | warnings_are_errors: false 12 | r_check_args: --as-cran 13 | latex: false 14 | 15 | matrix: 16 | include: 17 | - os: osx 18 | r_check_args: '--ignore-vignettes' 19 | r_build_args: '--no-build-vignettes' 20 | brew_packages: 21 | - imagemagick@6 22 | - poppler 23 | - dist: linux 24 | r_check_args: '--ignore-vignettes' 25 | r_build_args: '--no-build-vignettes' 26 | addons: 27 | apt: 28 | update: true 29 | sources: 30 | - sourceline: 'ppa:cran/poppler' 31 | packages: 32 | - poppler-data 33 | - libmagick++-dev 34 | - librsvg2-dev 35 | - libwebp-dev 36 | - libpoppler-cpp-dev 37 | - libtesseract-dev 38 | - libleptonica-dev 39 | - tesseract-ocr-eng 40 | - r-cran-rgdal 41 | - libfftw3-dev 42 | - cargo 43 | - gsfonts 44 | 45 | addons: 46 | apt: 47 | update: true 48 | sources: 49 | - sourceline: 'ppa:cran/imagemagick' 50 | packages: 51 | - poppler-data 52 | - libmagick++-dev 53 | - librsvg2-dev 54 | - libwebp-dev 55 | - libpoppler-cpp-dev 56 | - libtesseract-dev 57 | - libleptonica-dev 58 | - tesseract-ocr-eng 59 | - r-cran-rgdal 60 | - libfftw3-dev 61 | - cargo 62 | - gsfonts 63 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: eoffice 2 | Type: Package 3 | Title: Export or Graph and Tables to 'Microsoft' Office and Import 4 | Figures and Tables 5 | Version: 0.2.1 6 | Authors@R: person("Kai", "Guo", email = "guokai8@gmail.com", 7 | role = c("aut", "cre")) 8 | Description: Provides wrap functions to export and import graphics and 9 | data frames in R to 'microsoft' office. And This package also 10 | provide write out figures with lots of different formats. 11 | Since people may work on the platform without GUI support, the package 12 | also provide function to easily write out figures 13 | to lots of different type of formats. Now this package provide function 14 | to extract colors from all types of figures and pdf files. 15 | License: GPL-2 16 | Imports: officer, rvg, flextable, broom, dplyr, magrittr, ggplotify, 17 | R.devices, devEMF, magick, ggplot2, htmlwidgets, plotly 18 | Depends: 19 | Encoding: UTF-8 20 | LazyData: true 21 | Suggests: knitr, markdown, rmarkdown 22 | VignetteBuilder: knitr 23 | RoxygenNote: 7.1.1 24 | NeedsCompilation: no 25 | Packaged: 2020-11-18 16:14:34 UTC; bioguo 26 | Author: Kai Guo [aut, cre] 27 | Maintainer: Kai Guo 28 | Repository: CRAN 29 | Date/Publication: 2020-11-18 21:40:02 UTC 30 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(.setcolnames) 4 | export(convertplot) 5 | export(indocx) 6 | export(infigure) 7 | export(inoffice) 8 | export(inpptx) 9 | export(todocx) 10 | export(toffice) 11 | export(tofigure) 12 | export(tohtml) 13 | export(topptx) 14 | export(totable) 15 | export(toxlsx) 16 | importFrom(R.devices,devEval) 17 | importFrom(broom,tidy) 18 | importFrom(devEMF,emf) 19 | importFrom(dplyr,filter_) 20 | importFrom(dplyr,select_) 21 | importFrom(flextable,align) 22 | importFrom(flextable,autofit) 23 | importFrom(flextable,body_add_flextable) 24 | importFrom(flextable,bold) 25 | importFrom(flextable,empty_blanks) 26 | importFrom(flextable,flextable) 27 | importFrom(flextable,font) 28 | importFrom(flextable,hline) 29 | importFrom(flextable,theme_booktabs) 30 | importFrom(ggplot2,aes) 31 | importFrom(ggplot2,aes_string) 32 | importFrom(ggplot2,element_blank) 33 | importFrom(ggplot2,geom_bar) 34 | importFrom(ggplot2,geom_text) 35 | importFrom(ggplot2,ggplot) 36 | importFrom(ggplot2,last_plot) 37 | importFrom(ggplot2,scale_fill_manual) 38 | importFrom(ggplot2,theme) 39 | importFrom(ggplotify,as.ggplot) 40 | importFrom(ggplotify,as.grob) 41 | importFrom(grDevices,col2rgb) 42 | importFrom(grDevices,dev.cur) 43 | importFrom(grDevices,dev.off) 44 | importFrom(grDevices,dev.size) 45 | importFrom(grDevices,hcl) 46 | importFrom(grDevices,recordPlot) 47 | importFrom(grDevices,rgb) 48 | importFrom(graphics,plot) 49 | importFrom(htmlwidgets,saveWidget) 50 | importFrom(magick,image_ggplot) 51 | importFrom(magick,image_raster) 52 | importFrom(magick,image_read) 53 | importFrom(magick,image_read_pdf) 54 | importFrom(magick,image_read_svg) 55 | importFrom(magick,image_write) 56 | importFrom(magrittr,"%>%") 57 | importFrom(officer,add_slide) 58 | importFrom(officer,body_add_img) 59 | importFrom(officer,body_add_par) 60 | importFrom(officer,docx_summary) 61 | importFrom(officer,fp_border) 62 | importFrom(officer,ph_location) 63 | importFrom(officer,ph_location_type) 64 | importFrom(officer,ph_with) 65 | importFrom(officer,pptx_summary) 66 | importFrom(officer,read_docx) 67 | importFrom(officer,read_pptx) 68 | importFrom(officer,read_xlsx) 69 | importFrom(officer,slide_size) 70 | importFrom(plotly,as_widget) 71 | importFrom(plotly,ggplotly) 72 | importFrom(rvg,dml) 73 | importFrom(rvg,xl_add_vg) 74 | importFrom(stats,formula) 75 | importFrom(utils,type.convert) 76 | -------------------------------------------------------------------------------- /R/inFigure.R: -------------------------------------------------------------------------------- 1 | ##' import figures and extract the colors used in the figures 2 | ##' @importFrom magick image_read_svg 3 | ##' @importFrom magick image_read_pdf 4 | ##' @importFrom magick image_read 5 | ##' @importFrom magick image_raster 6 | ##' @importFrom magick image_write 7 | ##' @importFrom magick image_ggplot 8 | ##' @importFrom ggplot2 geom_bar 9 | ##' @importFrom ggplot2 geom_text 10 | ##' @importFrom ggplot2 ggplot 11 | ##' @importFrom ggplot2 aes 12 | ##' @importFrom ggplot2 aes_string 13 | ##' @importFrom ggplot2 scale_fill_manual 14 | ##' @importFrom grDevices dev.size 15 | ##' @importFrom graphics plot 16 | ##' @name infigure 17 | ##' @param filename input filename 18 | ##' @param format format of input file 19 | ##' @param exclude_col vector of colors to be excluded from the analysis 20 | ##' @param topn display the most frequent colors 21 | ##' @param showfig display the figure or not (default: FALSE) 22 | ##' @param showcol display extracted colors or not (default: FALSE) 23 | ##' @param savegg save the figure as ggplot2 object or not (default: FALSE) 24 | ##' @param density resolution to render pdf 25 | ##' @param pages integer vector with page numbers (pdf file). Defaults to all pages. 26 | ##' @examples 27 | ##' if(interactive()){ 28 | ##' require(ggplot2) 29 | ##' p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 30 | ##' tofigure(p,filename = file.path(tempdir(), "mtcars.pdf")) 31 | ##' pp <- infigure(filename = file.path(tempdir(), "mtcars.pdf"), exclude_col="white") 32 | ##' pp 33 | ##' } 34 | ##' @export 35 | ##' @author Kai Guo 36 | infigure <- function(filename, format = NULL, exclude_col = NULL, topn = 10, 37 | showfig = FALSE, showcol =FALSE, savegg = FALSE, 38 | density = 300, pages = NULL){ 39 | if(is.null(format)){ 40 | format = .getext(filename) 41 | } 42 | format = tolower(format) 43 | if(format == "pdf"){ 44 | p = image_read_pdf(filename, density = density, pages = pages) 45 | }else if(format == "svg"){ 46 | p = image_read_svg(filename) 47 | }else{ 48 | p = image_read(filename) 49 | } 50 | rasterp <- image_raster(p) 51 | coln <- table(rasterp$col) 52 | names(coln) <- sub('ff$', '', names(coln)) 53 | if(!is.null(exclude_col)){ 54 | start <- substr(exclude_col,1,1) 55 | exclude_col <- tolower(c(col2hex(exclude_col[start != "#"]), 56 | exclude_col[start == "#"])) 57 | coln <- coln[setdiff(names(coln), exclude_col)] 58 | } 59 | coln <- sort(coln,decreasing = TRUE) 60 | coln <- names(coln[1:topn]) 61 | if(isTRUE(showfig)){ 62 | plot(p) 63 | } 64 | if(isTRUE(showcol)){ 65 | dd <- data.frame(rep(1,topn),coln) 66 | colnames(dd) <- c("name","col") 67 | pp<-ggplot(dd,aes_string("col","name",fill="col"))+geom_bar(stat="identity")+ 68 | geom_text(aes(label=col),vjust=10)+scale_fill_manual(values=coln)+ 69 | .theme_blank() 70 | print(pp) 71 | } 72 | if(isTRUE(savegg)){ 73 | return(list(ggplot = image_ggplot(p), colors = coln)) 74 | }else{ 75 | return(coln) 76 | } 77 | } 78 | 79 | 80 | -------------------------------------------------------------------------------- /R/inoffice.R: -------------------------------------------------------------------------------- 1 | ##' read tables from ppt and word 2 | ##' @importFrom officer read_docx 3 | ##' @importFrom officer docx_summary 4 | ##' @importFrom officer pptx_summary 5 | ##' @importFrom magrittr %>% 6 | ##' @importFrom dplyr filter_ 7 | ##' @importFrom dplyr select_ 8 | ##' @param filename input filename 9 | ##' @param format pptx or docx 10 | ##' @param header use first row as column name 11 | ##' @examples 12 | ##' ## use tempdir for the example 13 | ##' totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.docx")) 14 | ##' tabs <- inoffice(filename = file.path(tempdir(), "mtcars.docx"), header = TRUE) 15 | ##' tabs 16 | ##' @export 17 | ##' @author Kai Guo 18 | inoffice <- function(filename, format = NULL, header = TRUE){ 19 | if(is.null(format)){ 20 | format = .getext(filename) 21 | } 22 | format = tolower(format) 23 | if (format == "ppt" | format == "pptx") { 24 | format = "ppt" 25 | } 26 | if (format == "doc" | format == "docx") { 27 | format = "doc" 28 | } 29 | if(format == "ppt"){ 30 | doc <- read_pptx(filename) 31 | content <- pptx_summary(doc) 32 | tent <- tapply(content$id, 33 | content$content_type, 34 | function(x) length(unique(x))) 35 | tent["slides"] <- length(unique(content$slide_id)) 36 | } 37 | if(format == "doc"){ 38 | doc <- read_docx(filename) 39 | content <- docx_summary(doc) 40 | tent <- tapply(content$doc_index, 41 | content$content_type, 42 | function(x) length(unique(x))) 43 | } 44 | table_cells <- filter_(content, ~content_type %in% "table cell") 45 | if(format == "ppt"){ 46 | len <- length(unique(table_cells$slide_id)) 47 | res<-vector(len,mode="list") 48 | for(i in 1:len){ 49 | sub_table <- filter_(table_cells, ~slide_id == i) 50 | for(j in unique(sub_table$id)){ 51 | data <- filter_(table_cells, ~id == j) %>% select_(~row_id, ~cell_id, ~text) 52 | data <- tapply(data$text, 53 | list(row_id = data$row_id, 54 | cell_id = data$cell_id 55 | ), FUN = I ) 56 | res[[i]][[j]] <- data 57 | } 58 | } 59 | names(res) <- paste("slide",1:len,sep="") 60 | res <- unlist(res, recursive = FALSE) 61 | tent["table cell"] <- length(res) 62 | } 63 | if(format == "doc"){ 64 | len <- length(unique(table_cells$doc_index)) 65 | res<-vector(len,mode="list") 66 | for(i in unique(table_cells$doc_index)){ 67 | data <- filter_(table_cells, ~doc_index == i)%>%select_(~row_id, ~cell_id, ~text) 68 | data <- tapply(data$text, 69 | list(row_id = data$row_id, 70 | cell_id = data$cell_id 71 | ), FUN = I ) 72 | res[[i]] <- data 73 | } 74 | res <- Filter(Negate(is.null), res) 75 | names(res) <- paste("Table",1:length(res),sep="") 76 | } 77 | res <- lapply(res, function(x) as.data.frame(x)) 78 | if(isTRUE(header)){ 79 | res <- lapply(res, function(x).setcolnames(x)) 80 | } 81 | return(res) 82 | } 83 | 84 | ##' read table from pptx 85 | ##' @name inpptx 86 | ##' @param filename input filename 87 | ##' @param header use first row as column name 88 | ##' @examples 89 | ##' totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.pptx")) 90 | ##' tabs <- inpptx(filename = file.path(tempdir(), "mtcars.pptx"), header = TRUE) 91 | ##' tabs 92 | ##' @author Kai Guo 93 | ##' @export 94 | inpptx <- function(filename, header = FALSE){ 95 | res <- inoffice(filename, format = "pptx", header = header) 96 | return(res) 97 | } 98 | 99 | ##' read table from docx 100 | ##' @name indocx 101 | ##' @param filename input filename 102 | ##' @param header use first row as column name 103 | ##' @examples 104 | ##' totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.docx")) 105 | ##' tabs <- indocx(filename = file.path(tempdir(), "mtcars.docx"), header = TRUE) 106 | ##' tabs 107 | ##' @author Kai Guo 108 | ##' @export 109 | indocx <- function(filename, header = FALSE){ 110 | res <- inoffice(filename, format = "docx", header = header) 111 | return(res) 112 | } 113 | 114 | 115 | -------------------------------------------------------------------------------- /R/misc.R: -------------------------------------------------------------------------------- 1 | ##' get the suffix name 2 | ##' @param x filename 3 | .getext <- function(x) 4 | { 5 | pos <- regexpr("\\.([[:alnum:]]+)$", x) 6 | ifelse(pos > -1L, substring(x, pos + 1L), "") 7 | } 8 | ##' get the prefix name 9 | ##' @param x filename 10 | .getfilename <- function(x){ 11 | filename <- basename(x) 12 | pos <- regexpr("\\.([[:alnum:]]+)$", x) 13 | ifelse(pos > -1L, substring(x, 1, pos - 1L), x) 14 | } 15 | 16 | ##' set first row as column name 17 | ##' @importFrom utils type.convert 18 | ##' @param file input data frame 19 | ##' @author Kai Guo 20 | ##' @export 21 | .setcolnames <- function(file){ 22 | names(file) <- as.matrix(file[1, ]) 23 | file <- file[-1, ] 24 | file[] <- lapply(file, function(x) type.convert(as.character(x))) 25 | file 26 | } 27 | ##' convert basic plot to ggplot object 28 | ##' @importFrom ggplotify as.grob 29 | ##' @importFrom ggplotify as.ggplot 30 | ##' @importFrom stats formula 31 | ##' @param exp expression formula of basic plot 32 | ##' @return ggplot object 33 | ##' @examples 34 | ##' \donttest{ 35 | ##' p <- convertplot(plot(1:10)) 36 | ##' class(p) 37 | ##' print(p) 38 | ##' } 39 | ##' @author Kai Guo 40 | ##' @export 41 | convertplot <- function(exp){ 42 | exp <- substitute(exp) 43 | p <- as.grob(formula(paste0('~',deparse(exp)))) 44 | p <- as.ggplot(p) 45 | if(file.exists("Rplots.pdf")){ 46 | file.remove("Rplots.pdf") 47 | } 48 | return(p) 49 | } 50 | 51 | ##' blank theme 52 | ##' @importFrom ggplot2 theme 53 | ##' @importFrom ggplot2 element_blank 54 | ##' 55 | .theme_blank<-function(){ 56 | theme(panel.border=element_blank(),panel.grid=element_blank(), 57 | panel.background=element_blank(),axis.ticks=element_blank(), 58 | axis.text=element_blank(), 59 | axis.title=element_blank(),legend.position="none") 60 | } 61 | ##' col2hex gplots 62 | ##' @importFrom grDevices rgb 63 | ##' @importFrom grDevices col2rgb 64 | ##' @param cname color name 65 | col2hex <- function (cname) 66 | { 67 | colMat <- col2rgb(cname) 68 | rgb(red = colMat[1, ]/255, green = colMat[2, ]/255, blue = colMat[3, 69 | ]/255) 70 | } 71 | -------------------------------------------------------------------------------- /R/plot.R: -------------------------------------------------------------------------------- 1 | #' plot beta diversity 2 | #' 3 | #' @importFrom ggplot2 ggplot aes_string 4 | #' @importFrom ggplot2 geom_point 5 | #' @importFrom ggplot2 scale_color_manual 6 | #' @importFrom ggplot2 xlab ylab stat_ellipse 7 | #' @importFrom ggplot2 theme_light 8 | #' @importFrom phyloseq taxa_are_rows 9 | #' @param physeq A \code{phyloseq} object containing merged information of abundance, 10 | #' taxonomic assignment, sample data including the measured variables and categorical information 11 | #' of the samples, and / or phylogenetic tree if available. 12 | #' @param group (Required). Character string specifying name of a categorical variable that is preferred for grouping the information. 13 | #' information. 14 | #' @param shape shape(Optional) Character string specifying shape of a categorical variable 15 | #' @param method A character string specifying ordination method. All methods available to the \code{ordinate} function 16 | #' of \code{phyloseq} are acceptable here as well. 17 | #' @param distance A string character specifying dissimilarity index to be used in calculating pairwise distances (Default index is "bray".). 18 | #' "unifrac","wunifrac","manhattan", "euclidean", "canberra", "bray", "kulczynski", "jaccard", "gower", "altGower", 19 | #' "morisita", "horn", "mountford", "raup" , "binomial", "chao", "cao" or "mahalanobis". 20 | #' @param color user defined color for group 21 | #' @param size the point size 22 | #' @param ellipse draw ellipse or not 23 | #' @examples 24 | #' { 25 | #' data("Physeq") 26 | #' phy<-normalize(physeq) 27 | #' plotbeta(phy,group="SampleType") 28 | #' } 29 | #' @return ggplot2 object 30 | #' @author Kai Guo 31 | #' @export 32 | plotbeta<-function(physeq,group,shape=NULL,distance="bray",method="PCoA",color=NULL,size=3,ellipse=FALSE){ 33 | if(!taxa_are_rows(physeq)){ 34 | physeq <- t(physeq) 35 | } 36 | beta<-betadiv(physeq,distance = distance,method=method) 37 | df <- as.data.frame(beta$beta) 38 | PCs <- beta$PCs 39 | tab <- as(sample_data(physeq),"data.frame") 40 | df <- cbind(df[,1:4],tab[rownames(df),]) 41 | df$group<-tab[,group] 42 | if(is.null(color)){ 43 | color<-distcolor[1:length(unique(df$group))] 44 | } 45 | if(!is.null(shape)){ 46 | df$shape<-tab[,shape] 47 | p <- ggplot(df,aes_string("Axis.1","Axis.2",color="group",shape="shape")) 48 | }else{ 49 | p <- ggplot(df,aes_string("Axis.1","Axis.2",color="group")) 50 | } 51 | p<-p+geom_point(size=size)+scale_color_manual(values=color) 52 | p <- p+theme_light(base_size=15)+xlab(paste0("Axis1 (",round(PCs[1]*100,2),"%)"))+ylab(paste0("Axis2 (",round(PCs[2]*100,2),"%)")) 53 | if(isTRUE(ellipse)){ 54 | p <- p + stat_ellipse() 55 | } 56 | p 57 | } 58 | 59 | #' plot alpha diversity 60 | #' @importFrom rstatix t_test 61 | #' @importFrom rstatix wilcox_test 62 | #' @importFrom ggpubr ggboxplot 63 | #' @importFrom ggpubr ggviolin 64 | #' @importFrom ggpubr ggdotplot 65 | #' @importFrom ggpubr stat_pvalue_manual 66 | #' @importFrom ggpubr facet 67 | #' @importFrom ggplot2 xlab ylab scale_color_manual theme 68 | #' @importFrom dplyr summarise group_by 69 | #' @importFrom tidyr gather spread 70 | #' @param physeq A \code{phyloseq} object containing merged information of abundance, 71 | #' taxonomic assignment, sample data including the measured variables and categorical information 72 | #' of the samples, and / or phylogenetic tree if available. 73 | #' @param group group (Required). A character string specifying the name of a categorical variable containing grouping information. 74 | #' @param method A list of character strings specifying \code{method} to be used to calculate for alpha diversity 75 | #' in the data. Available methods are: "Observed","Chao1","ACE","Richness", "Fisher", "Simpson", "Shannon", "Evenness","InvSimpson". 76 | #' @param color A vector of character use specifying the color 77 | #' @param geom different geom to display("boxplot","violin","dotplot") 78 | #' @param pvalue pvalue threshold for significant dispersion results 79 | #' @param sig.only display the significant comparsion only(TRUE/ FALSE) 80 | #' @param padj adjust p value threshold for significant dispersion results 81 | #' @param wilcox use wilcoxon test or not 82 | #' @param show.number to show the pvalue instead of significant symbol(TRUE/FALSE) 83 | #' @examples 84 | #' { 85 | #' data("Physeq") 86 | #' plotalpha(physeq,group="SampleType") 87 | #' } 88 | #' @return Returns a ggplot object. This can further be manipulated as preferred by user. 89 | #' @author Kai Guo 90 | #' @export 91 | plotalpha<-function(physeq,group,method=c("Observed","Simpson", "Shannon"),color=NULL,geom="boxplot", 92 | pvalue=0.05,padj=NULL,sig.only=TRUE, wilcox=FALSE,show.number=FALSE){ 93 | if (!taxa_are_rows(physeq)) { 94 | physeq <- t(physeq) 95 | } 96 | rich<-richness(physeq,method = method) 97 | name<-levels(factor(colnames(rich))) 98 | tab<-as(sample_data(physeq),"data.frame") 99 | rich$group<-tab[rownames(rich),group] 100 | if(isTRUE(wilcox)){ 101 | res<-do_wilcox(rich,"group") 102 | }else{ 103 | res<-do_ttest(rich,"group") 104 | } 105 | if(sum(res$p%gather(type,val,-group)%>%group_by(type,group)%>%summarise(ma=max(val))%>%spread(group,ma) 116 | pos <- apply(res, 1, function(x)max(vals[vals$type==x[1],x[3:4]])) 117 | mpos <- apply(res, 1, function(x)min(vals[vals$type==x[1],x[3:4]])) 118 | if(geom=="boxplot"){ 119 | p<-rich%>%gather(type,val,-group)%>%ggboxplot(x="group",y="val",color="group") 120 | }else if(geom=="violin"){ 121 | p<-rich%>%gather(type,val,-group)%>%ggviolin(x="group",y="val",color="group") 122 | }else if(geom=="dotplot"){ 123 | p<-rich%>%gather(type,val,-group)%>%ggdotplot(x="group",y="val",color="group") 124 | }else{ 125 | stop("Please specify one type of boxplot,violin,dotplot") 126 | } 127 | if(!isTRUE(show.number)){ 128 | res$p.signif<-sapply(res$p,function(x).getstar(x)) 129 | res$p.adj.signif<-sapply(res$p.adj,function(x).getstar(x)) 130 | }else{ 131 | res$p.signif<-res$p 132 | res$p.adj.signif<-res$p.adj 133 | } 134 | if(is.null(color)){ 135 | color<-distcolor[1:length(unique(rich$group))] 136 | } 137 | p<-facet(p,facet.by = "type",scales = "free_y",ncol = length(method)) 138 | if(!is.null(padj)){ 139 | p<-p+stat_pvalue_manual(res,label = "p.adj.signif",y.position = pos+2*mpos/nrow(res)) 140 | }else{ 141 | p<-p+stat_pvalue_manual(res,label = "p.signif",y.position = pos+mpos/nrow(res)) 142 | } 143 | p<-p+xlab("")+ylab("")+ 144 | theme(legend.position = "none",axis.text.x=element_text(angle=90,vjust=0.5, hjust=1))+ 145 | scale_color_manual(values=color) 146 | p 147 | } 148 | 149 | 150 | #' plot bar for relative abundance for bacteria 151 | #' @importFrom phyloseq psmelt 152 | #' @importFrom ggplot2 ggplot 153 | #' @importFrom ggplot2 geom_bar 154 | #' @importFrom ggplot2 scale_fill_manual 155 | #' @importFrom ggplot2 theme 156 | #' @importFrom ggplot2 element_text 157 | #' @importFrom ggplot2 aes_string 158 | #' @importFrom ggplot2 element_blank scale_y_continuous 159 | #' @importFrom dplyr group_by_at 160 | #' @importFrom dplyr vars pull 161 | #' @importFrom dplyr one_of 162 | #' @importFrom dplyr summarise 163 | #' @importFrom dplyr mutate 164 | #' @importFrom dplyr select 165 | #' @importFrom rlang `!!` 166 | #' @importFrom utils head 167 | #' @param physeq A \code{phyloseq} object containing merged information of abundance, 168 | #' taxonomic assignment, sample data including the measured variables and categorical information 169 | #' of the samples, and / or phylogenetic tree if available. 170 | #' @param level the level to plot 171 | #' @param color A vector of character use specifying the color 172 | #' @param group group (Optional). A character string specifying the name of a categorical variable containing grouping information. 173 | #' @param top the number of most abundance bacteria to display 174 | #' @param return return the data with the relative abundance 175 | #' @param fontsize.x the size of x axis label 176 | #' @param fontsize.y the size of y axis label 177 | #' @examples 178 | #' \donttest{ 179 | #' data("Physeq") 180 | #' phy<-normalize(physeq) 181 | #' plotbar(phy,level="Phylum") 182 | #' } 183 | #' @return Returns a ggplot object. This can further be manipulated as preferred by user. 184 | #' @author Kai Guo 185 | #' @export 186 | plotbar<-function(physeq,level="Phylum",color=NULL,group=NULL,top=5,return=FALSE,fontsize.x = 5, fontsize.y = 12){ 187 | pm <- psmelt(physeq) 188 | if(is.null(color)){ 189 | len<-length(unique(pm[,level])) 190 | color<-distcolor[1:len] 191 | } 192 | if(is.null(group)){ 193 | group_var<-c("Sample",level) 194 | }else{ 195 | group_var<-c(group,level) 196 | } 197 | d<-pm%>%group_by_at(vars(one_of(group_var)))%>%summarise(su=sum(Abundance)) 198 | d <- as.data.frame(d) 199 | d[,level][is.na(d[,level])]<-"NA" 200 | dx <- pm%>%group_by_at(vars(one_of(level)))%>%summarise(su=sum(Abundance)) 201 | dx <- dx[order(dx$su,decreasing = T),] 202 | sel <- dx%>%head(top)%>%select(!!level)%>%pull(1) 203 | d <- d[d[,level]%in%sel,] 204 | if(is.null(group)){ 205 | p<-ggplot(d,aes_string("Sample","su",fill=level)) 206 | }else{ 207 | p<-ggplot(d,aes_string(group,"su",fill=level)) 208 | } 209 | p<-p+geom_bar(stat = "identity",position = "fill")+scale_fill_manual(values=color)+ 210 | theme_light()+ 211 | scale_y_continuous(expand = c(0, 0.001)) + 212 | theme(axis.text.x=element_text(angle=90,size=fontsize.x, vjust=0.5, hjust=1), 213 | axis.text.y=element_text(size=fontsize.y), 214 | panel.background = element_blank(),axis.ticks.x = element_blank())+ 215 | xlab("")+ylab("") 216 | if(isTRUE(return)){ 217 | return(pm[,c("OTU","Abundance",group_var)]) 218 | }else{ 219 | return(p) 220 | } 221 | } 222 | 223 | #' @title plot differential results 224 | #' @importFrom ggplot2 ggplot theme geom_point element_text xlab 225 | #' @importFrom ggplot2 aes_string scale_color_manual theme_light coord_flip 226 | #' @param res differential test results from diff_test 227 | #' @param level the level to plot 228 | #' @param color A vector of character use specifying the color 229 | #' @param pvalue pvalue threshold for significant results 230 | #' @param padj adjust p value threshold for significant results 231 | #' @param log2FC log2 Fold Change threshold 232 | #' @param size size for the point 233 | #' @param fontsize.x the size of x axis label 234 | #' @param fontsize.y the size of y axis label 235 | #' @param horiz horizontal or not (TRUE/FALSE) 236 | #' @examples 237 | #' \donttest{ 238 | #' data("Physeq") 239 | #' res <- difftest(physeq,group="group") 240 | #' plotdiff(res,level="Genus",padj=0.001) 241 | #' } 242 | #' @return ggplot object 243 | #' @author Kai Guo 244 | #' @export 245 | plotdiff<-function(res,level="Genus",color=NULL,pvalue=0.05,padj=NULL,log2FC=0,size=3,fontsize.x=5,fontsize.y=10,horiz=TRUE){ 246 | if(!is.null(padj)){ 247 | pval<-padj 248 | sigtab <- subset(res,padjlog2FC) 249 | }else{ 250 | pval<-pvalue 251 | sigtab <- subset(res,pvaluelog2FC) 252 | } 253 | x <- tapply(sigtab$log2FoldChange, sigtab$Phylum, function(x) max(x)) 254 | x <- sort(x, TRUE) 255 | sigtab$Phylum <- factor(as.character(sigtab$Phylum), levels=names(x)) 256 | if(is.null(color)){ 257 | len<-length(unique(sigtab$Phylum)) 258 | color<-distcolor[1:len] 259 | } 260 | # Genus order 261 | sigtab$name<-paste0(sigtab[,level],"(",rownames(sigtab),")") 262 | x <- tapply(sigtab$log2FoldChange, sigtab$name, function(x) max(x)) 263 | x <- sort(x, TRUE) 264 | sigtab$name <- factor(as.character(sigtab$name), levels=names(x)) 265 | p <- ggplot(sigtab, aes_string(x="name", y="log2FoldChange", color="Phylum"))+ 266 | geom_point(size=3) +theme_light()+xlab(level)+ 267 | theme(axis.text.x = element_text(angle = -90, hjust = 0, vjust=0.5,size=fontsize.x), 268 | axis.text.y = element_text(size=fontsize.y))+ 269 | scale_color_manual(values=color) 270 | if(isTRUE(horiz)){ 271 | p<-p+coord_flip()+theme(axis.text.x=element_text(angle=0,size=fontsize.x)) 272 | } 273 | p 274 | } 275 | 276 | #' plot LEfSe results from ldamarker function 277 | #' @importFrom ggplot2 ggplot geom_bar coord_flip theme_light element_text 278 | #' @importFrom ggplot2 scale_fill_manual xlab 279 | #' @importFrom ggplot2 aes 280 | #' @importFrom dplyr mutate 281 | #' @importFrom magrittr %>% 282 | #' @importFrom stats reorder 283 | #' @param x LEfse results from ldamarker 284 | #' @param group a vector include two character to show the group comparsion 285 | #' @param lda LDA threshold for significant biomarker 286 | #' @param pvalue pvalue threshold for significant results 287 | #' @param padj adjust p value threshold for significant results 288 | #' @param color A vector of character use specifying the color 289 | #' @param fontsize.x the size of x axis label 290 | #' @param fontsize.y the size of y axis label 291 | #' @examples 292 | #' \donttest{ 293 | #' data("Physeq") 294 | #' res <- ldamarker(physeq,group="group") 295 | #' plotLDA(res,group=c("A","B"),lda=5,pvalue=0.05) 296 | #' } 297 | #' @return ggplot2 object 298 | #' @author Kai Guo 299 | #' @export 300 | plotLDA<-function(x,group,lda=2,pvalue=0.05,padj=NULL,color=NULL,fontsize.x=4,fontsize.y=5){ 301 | x <- subset(x,LDAscore>lda) 302 | if(!is.null(padj)){ 303 | x <- subset(x,p.adj%mutate(LDA=ifelse(direction==group[1],LDAscore,-LDAscore)) 309 | p<-ggplot(x,aes(x=reorder(tax,LDA),y=LDA,fill=direction))+ 310 | geom_bar(stat="identity",color="white")+coord_flip()+ 311 | theme_light()+theme(axis.text.x = element_text(size=fontsize.x), 312 | axis.text.y = element_text(size=fontsize.y)) 313 | if(is.null(color)){ 314 | color <- distcolor[c(2:3)] 315 | } 316 | p<-p+scale_fill_manual(values=color)+xlab("") 317 | p 318 | } 319 | 320 | #' 321 | #' plot the biomarker from the biomarker function with randomForest 322 | #' @importFrom ggpubr ggdotchart 323 | #' @importFrom ggplot2 xlab ylab 324 | #' @param x biomarker results from randomForest 325 | #' @param level the bacteria level to display 326 | #' @param top the number of important biomarker to draw 327 | #' @param rotate TRUE/FALSE 328 | #' @param dot.size size for the dot 329 | #' @param label.size label size 330 | #' @param label.color label color 331 | #' @return ggplot2 object 332 | #' @examples 333 | #' \donttest{ 334 | #' data("Physeq") 335 | #' res <- biomarker(physeq,group="group") 336 | #' plotmarker(res,level="Genus") 337 | #' } 338 | #' @export 339 | #' @author Kai Guo 340 | plotmarker<-function(x,level="Genus",top=30,rotate=FALSE,dot.size=8,label.color="black",label.size=6){ 341 | x <- x[1:top,] 342 | x <- x[order(x$Value),] 343 | x$label<-paste0(x[,level],"(",x$OTU,")") 344 | p<-ggdotchart(x,x="label",y="Value",add="segments",color=I("#00AFBB"),rotate=rotate,dot.size=dot.size,sorting="descending", 345 | add.params = list(color = "#00AFBB", size = 1.5), 346 | label=round(x$Value,2),font.label = list(color = label.color, size = label.size,vjust=0.2)) 347 | if(isTRUE(rotate)){ 348 | p<-p+xlab(level)+ylab("Mean Decrease Accuracy") 349 | }else{ 350 | p<-p+xlab(level)+ylab("Mean Decrease Accuracy") 351 | } 352 | p 353 | } 354 | 355 | #' plot the quality for the fastq file 356 | #' @param file (Required). character. File path(s) to fastq or fastq.gz file(s). 357 | #' @param n (Optional). Default 500,000. The number of records to sample from the fastq file. 358 | #' @param aggregate (Optional). Default FALSE. If TRUE, compute an aggregate quality profile for all fastq files provided. 359 | #' @examples 360 | #' \donttest{ 361 | #' plotquality(system.file("extdata", "sam1F.fastq.gz", package="dada2")) 362 | #' } 363 | #' @export 364 | #' @return figure 365 | plotquality<-function(file,n = 5e+05, aggregate = FALSE){ 366 | dada2::plotQualityProfile(file,n=n,aggregate = aggregate) 367 | } 368 | -------------------------------------------------------------------------------- /R/toFigure.R: -------------------------------------------------------------------------------- 1 | ##' output figures to different formats 2 | ##' @name tofigure 3 | ##' @importFrom R.devices devEval 4 | ##' @importFrom grDevices dev.off 5 | ##' @importFrom devEMF emf 6 | ##' @importFrom grDevices recordPlot 7 | ##' @param figure output figure function, set NULL output the current figure 8 | ##' @param format output format (could be ingored) 9 | ##' @param filename output filename with different format as suffix 10 | ##' @examples 11 | ##' require(ggplot2) 12 | ##' p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 13 | ##' tofigure(p,filename = file.path(tempdir(), "mtcars.pdf")) 14 | ##' ## or use ggplot directly 15 | ##' tofigure(ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + 16 | ##' geom_point(), filename = file.path(tempdir(), "mtcars.eps")) 17 | ##' \donttest{ 18 | ##' ## if you use basic plot function or other plot function you 19 | ##' ## need first use convertplot to convert it to ggplot object 20 | ##' ## when you are working on the platform without GUI 21 | ##' p <- convertplot(plot(1:10)) 22 | ##' tofigure(p, filename = file.path(tempdir(), "mtcars.pdf")) 23 | ##' topptx(p, filename = file.path(tempdir(), "mtcars.pptx")) 24 | ##' } 25 | ##' @author Kai Guo 26 | ##' @export 27 | tofigure <- function(figure, format = NULL, filename= "temp.pdf"){ 28 | if(is.null(format)){ 29 | format = .getext(filename) 30 | } 31 | path = dirname(filename) 32 | ofile = filename 33 | filename = .getfilename(filename) 34 | format = tolower(format) 35 | if(format == "emf"){ 36 | if(missing(figure)){ 37 | emf(file=ofile) 38 | p <- recordPlot() 39 | print(p) 40 | dev.off() 41 | }else{ 42 | emf(file=ofile) 43 | print(figure) 44 | dev.off() 45 | } 46 | }else{ 47 | if(missing(figure)){ 48 | devEval(type = format, name = filename, path = path) 49 | }else{ 50 | devEval(type = format, name = filename, path = path, 51 | expr = {print(figure)}) 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /R/toTable.R: -------------------------------------------------------------------------------- 1 | ##' write table out to pptx or docx 2 | ##' @importFrom officer add_slide 3 | ##' @importFrom officer ph_with 4 | ##' @importFrom flextable flextable 5 | ##' @importFrom flextable theme_booktabs 6 | ##' @importFrom flextable body_add_flextable 7 | ##' @importFrom flextable autofit 8 | ##' @importFrom flextable bold 9 | ##' @importFrom flextable align 10 | ##' @importFrom flextable hline 11 | ##' @importFrom flextable font 12 | ##' @importFrom officer fp_border 13 | ##' @importFrom officer read_pptx 14 | ##' @importFrom officer read_docx 15 | ##' @importFrom flextable empty_blanks 16 | ##' @importFrom broom tidy 17 | ##' @importFrom magrittr %>% 18 | ##' @param data datasets 19 | ##' @param filename output filename 20 | ##' @param format pptx or docx 21 | ##' @param append append into file 22 | ##' @examples 23 | ##' tt <- t.test(wt ~ am, mtcars) 24 | ##' totable(tt, filename = file.path(tempdir(), "mtcars.pptx")) 25 | ##' totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.pptx")) 26 | ##' totable(head(mtcars), filename = file.path(tempdir(), "mtcars.docx")) 27 | ##' @author Kai Guo 28 | ##' @export 29 | totable <- function(data, filename, format = NULL, append = FALSE){ 30 | if(is.null(format)){ 31 | format = .getext(filename) 32 | } 33 | format = tolower(format) 34 | if (format == "ppt" | format == "pptx") { 35 | format = "ppt" 36 | } 37 | if (format == "doc" | format == "docx") { 38 | format = "doc" 39 | } 40 | typecl <- c("matrix","data.frame","tbl_df","tbl") 41 | if(length(intersect(class(data),typecl))==0){ 42 | data = tidy(data) 43 | }else{ 44 | if(class(data)=="matrix"){ 45 | data = as.data.frame(data) 46 | } 47 | } 48 | ft <- flextable(data = data) %>% 49 | theme_booktabs() %>% bold(part = "header") %>% 50 | font(fontname = "Times", part = "all") %>% 51 | align(align = "center", part ="all") %>% 52 | hline(border = fp_border(width = 2, color = "#007FA6"), part = "header" ) %>% 53 | empty_blanks() %>% 54 | autofit() 55 | if(format == "ppt"){ 56 | if(isTRUE(append)){ 57 | if(file.exists(filename)){ 58 | doc <- read_pptx(filename) 59 | }else{ 60 | doc <- read_pptx() 61 | doc <- add_slide(doc,"Title and Content", "Office Theme") 62 | print(doc,target=filename) 63 | } 64 | }else{ 65 | doc <- read_pptx() 66 | } 67 | doc <- add_slide(doc, "Title and Content", "Office Theme") 68 | doc <- ph_with(doc, ft, location = ph_location_type(type = "body")) 69 | print(doc,target=filename) 70 | } 71 | if(format == "doc"){ 72 | if(isTRUE(append)){ 73 | if(file.exists(filename)){ 74 | doc <- read_docx(filename) 75 | }else{ 76 | doc <- read_docx() 77 | print(doc,target=filename) 78 | } 79 | }else{ 80 | doc <- read_docx() 81 | } 82 | doc <- read_docx() 83 | doc <- body_add_flextable(doc, ft) 84 | print(doc,target=filename) 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /R/toffice.R: -------------------------------------------------------------------------------- 1 | ##' @title export graph to MS office 2 | ##' @name toffice 3 | ##' @importFrom officer read_docx 4 | ##' @importFrom officer read_pptx 5 | ##' @importFrom officer read_xlsx 6 | ##' @importFrom magrittr %>% 7 | ##' @importFrom officer add_slide ph_with ph_location 8 | ##' @importFrom officer ph_location_type 9 | ##' @importFrom officer body_add_par 10 | ##' @importFrom officer body_add_img 11 | ##' @importFrom rvg dml 12 | ##' @importFrom rvg xl_add_vg 13 | ##' @importFrom grDevices recordPlot 14 | ##' @importFrom grDevices dev.cur hcl 15 | ##' @importFrom officer slide_size 16 | ##' @importFrom devEMF emf 17 | ##' @param figure plot figure function 18 | ##' @param format file format 19 | ##' @param filename output filename 20 | ##' @param nr,nc two numbers to indicate the figures will be drawn in 21 | ##' an nr-by-nc array on the device by columns or rows, respectively. 22 | ##' @param irow,icol two number to indicate which row or column for the figure to be drawn 23 | ##' @param onsame Boolean value show to draw on same slide or not 24 | ##' @param left,top two numbers which gives the number of lines of margin to be specified 25 | ##' on the left and right sides of the plot 26 | ##' @param title title for the figure 27 | ##' @param append append or not 28 | ##' @param width width of the output figure 29 | ##' @param height height of the output figure 30 | ##' @param devsize Boolean value show use device size or not (default = FALSE) 31 | ##' @param units the units in which to return the value: inches, cm, or pixels (device units) 32 | ##' @examples 33 | ##' if(interactive()){ 34 | ##' plot(mtcars$mpg, mtcars$disp) 35 | ##' toffice(filename = file.path(tempdir(), "mtcars.pptx"), format = "pptx") 36 | ##' ## use ggplot2 37 | ##' ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 38 | ##' toffice(filename = file.path(tempdir(), "mtcars.pptx"),format = "pptx") 39 | ##' } 40 | ##' @author Kai Guo 41 | ##' @export 42 | toffice <- function(figure = NULL, format = "pptx",filename= "temp.pptx", nr=1, nc=1, 43 | irow=1,icol=1, onsame=FALSE, 44 | title="",left=0.15,top=0.15, 45 | append = FALSE, width = 4, height = 4, devsize = FALSE, 46 | units = "in"){ 47 | format = tolower(format) 48 | if (format == "ppt" | format == "pptx") { 49 | format = "ppt" 50 | } 51 | if (format == "doc" | format == "docx") { 52 | format = "doc" 53 | } 54 | if (format == "xls" | format == "xlsx"){ 55 | format = "xls" 56 | } 57 | if(is.null(figure)){ 58 | if (dev.cur() == 1) 59 | stop("No graphics device found") 60 | p <- recordPlot() 61 | }else{ 62 | if(is.function(figure)){ 63 | p <- figure() 64 | }else{ 65 | p <- figure 66 | } 67 | } 68 | if(isTRUE(devsize)){ 69 | width = dev.size(units)[1] 70 | height = dev.size(units)[2] 71 | } 72 | if(format == "ppt"){ 73 | if(isTRUE(append)){ 74 | if(file.exists(filename)){ 75 | doc <- read_pptx(filename) 76 | }else{ 77 | doc <- read_pptx() 78 | if(nr>1|nc>1){ 79 | doc <- add_slide(doc, layout = "Blank", master = "Office Theme") 80 | }else{ 81 | doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme") 82 | } 83 | print(doc, target=filename) 84 | } 85 | }else{ 86 | doc <- read_pptx() 87 | } 88 | if(nr>1|nc>1){ 89 | if(file.exists(filename)&isTRUE(onsame)){ 90 | doc <- read_pptx(filename) 91 | }else{ 92 | doc <- add_slide(doc,layout = "Blank", master = "Office Theme") 93 | } 94 | margins <- c(left=left,right=left,top=top,bottom=top) 95 | pgwidth <- slide_size(doc)[[1]]-2*left 96 | pgheight <- slide_size(doc)[[2]]-2*top 97 | #pgwidth <- slide_size(doc)[[1]] 98 | #pgheight <- slide_size(doc)[[2]] 99 | w <- width 100 | h <- height 101 | ws <- pgwidth/nc 102 | hs <- pgheight/nr 103 | ratio <- ws/hs 104 | pratio <- w/h 105 | if (ratio >= pratio) { 106 | xs = pratio/ratio 107 | ys = 1 108 | } else { 109 | xs = 1 110 | ys = ratio/pratio 111 | } 112 | w = w * xs 113 | h = h * ys 114 | if(w>ws|h>hs){ 115 | w <- w/nc 116 | h <- h/nr 117 | } 118 | offl <- (ws+margins['left']-w)/nc 119 | offt <- (hs+margins['top']-h)/nr 120 | doc <- ph_with(doc, dml(code = print(p)), 121 | location = ph_location(left=(icol-1)*ws+(icol-1)*margins['left']+offl, 122 | top=(irow-1)*hs+(irow-1)*margins['top']+offt,width = w, height = h)) 123 | }else{ 124 | doc <- add_slide(doc,layout = "Title and Content", master = "Office Theme") 125 | doc <- ph_with(doc, value = title, location = ph_location_type(type = "title")) 126 | doc <- ph_with(doc, dml(code = print(p)), location = ph_location(width = width, height = height)) 127 | } 128 | print(doc,target=filename) 129 | } 130 | if(format == "doc"){ 131 | if(isTRUE(append)){ 132 | if(file.exists(filename)){ 133 | doc <- read_docx(filename) 134 | }else{ 135 | doc <- read_docx() 136 | print(doc,target=filename) 137 | } 138 | }else{ 139 | doc <- read_docx() 140 | } 141 | temp.file <- tempfile() 142 | temp.file <- paste0(temp.file, ".emf") 143 | emf(file = temp.file, height = height, width = width, emfPlus = TRUE) 144 | print(p) 145 | dev.off() 146 | doc <- body_add_par(doc, value = title, style = "Normal" ) 147 | doc <- body_add_img(doc, src=temp.file, width = width, height = height) 148 | print(doc, target = filename) 149 | } 150 | if(format == "xls"){ 151 | doc <- read_xlsx() 152 | if(title == ""){ 153 | title = "Feuil1" 154 | } 155 | doc <- xl_add_vg(doc, sheet = title, code = print(p), width = width, height = height, left=1, top=1) 156 | print(doc, target = filename) 157 | } 158 | } 159 | 160 | ##' export figure to pptx 161 | ##' @name topptx 162 | ##' @param figure plot figure function 163 | ##' @param filename output filename 164 | ##' @param nr,nc two numbers to indicate the figures will be drawn in 165 | ##' an nr-by-nc array on the device by columns or rows, respectively. 166 | ##' @param irow,icol the number to indicate which row or column for the figure to be drawn 167 | ##' @param onsame Boolean value show to draw on same slide or not 168 | ##' @param title title for the figure 169 | ##' @param left,top two numbers which gives the number of lines of margin to be specified 170 | ##' on the left and right sides of the plot 171 | ##' @param width width of the output figure 172 | ##' @param height height of the output figure 173 | ##' @param append append or not 174 | ##' @param devsize Boolean value show use device size or not (default = FALSE) 175 | ##' @param units the units in which to return the value: inches, cm, or pixels (device units) 176 | ##' @examples 177 | ##' if(interactive()){ 178 | ##' plot(mtcars$mpg, mtcars$disp) 179 | ##' topptx(filename = file.path(tempdir(), "mtcars.pptx")) 180 | ##' ## use ggplot2 181 | ##' ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 182 | ##' topptx(filename = file.path(tempdir(), "mtcars.pptx")) 183 | ##' } 184 | ##' @author Kai Guo 185 | ##' @export 186 | topptx <- function(figure = NULL, filename = NULL,nr=1, nc=1, irow=1,icol=1, onsame=FALSE, title = "", 187 | left=0.15,top=0.15, width = 6, height = 6, 188 | append = FALSE, devsize = FALSE, units = "in"){ 189 | toffice(figure = figure, filename = filename, format = "pptx", nr=nr, nc=nc, irow=irow,icol=icol, onsame=onsame,title = title, 190 | left=left,top=top, 191 | width = width, height = height, append = append, devsize = devsize, 192 | units = units) 193 | } 194 | ##' export figure to docx 195 | ##' @name todocx 196 | ##' @param figure plot figure function 197 | ##' @param filename output filename 198 | ##' @param title title for the figure 199 | ##' @param width width of the output figure 200 | ##' @param height height of the output figure 201 | ##' @param append append or not 202 | ##' @param devsize Boolean value show use device size or not (default = FALSE) 203 | ##' @param units the units in which to return the value: inches, cm, or pixels (device units) 204 | ##' @examples 205 | ##' if(interactive()){ 206 | ##' plot(mtcars$mpg, mtcars$disp) 207 | ##' todocx(filename = file.path(tempdir(), "mtcars.docx")) 208 | ##' ## use ggplot2 209 | ##' ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 210 | ##' todocx(filename = file.path(tempdir(), "mtcars.docx"), height = 6, width = 4) 211 | ##' } 212 | ##' @author Kai Guo 213 | ##' @export 214 | todocx <- function(figure =NULL, filename = NULL, title = "", width = 6, height = 6, 215 | append = FALSE, devsize = FALSE, units = "in"){ 216 | toffice(figure = figure, filename = filename, format = "docx", title = title, 217 | width = width, height = height,append = append, devsize = devsize, 218 | units = units) 219 | } 220 | 221 | ##' export figure to pptx 222 | ##' @name toxlsx 223 | ##' @param figure plot figure function 224 | ##' @param filename output filename 225 | ##' @param width width of the output figure 226 | ##' @param height height of the output figure 227 | ##' @param devsize Boolean value show use device size or not (default = FALSE) 228 | ##' @param units the units in which to return the value: inches, cm, or pixels (device units) 229 | ##' @examples 230 | ##' if(interactive()){ 231 | ##' plot(mtcars$mpg, mtcars$disp) 232 | ##' toxlsx(filename = file.path(tempdir(), "mtcars.xlsx")) 233 | ##' ## use ggplot2 234 | ##' ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 235 | ##' toxlsx(filename = file.path(tempdir(), "mtcars.xlsx")) 236 | ##' } 237 | ##' @author Kai Guo 238 | ##' @export 239 | toxlsx <- function(figure = NULL, filename = NULL, width = 6, height = 6, 240 | devsize = FALSE, units = "in"){ 241 | toffice(figure = figure, filename = filename, format = "xlsx", title = "", 242 | width = width, height = height, devsize = devsize, 243 | units = units) 244 | } 245 | -------------------------------------------------------------------------------- /R/tohtml.R: -------------------------------------------------------------------------------- 1 | ##' export as plotly html (only support ggplot2 object) 2 | ##' @name tohtml 3 | ##' @importFrom plotly ggplotly 4 | ##' @importFrom htmlwidgets saveWidget 5 | ##' @importFrom plotly as_widget 6 | ##' @importFrom ggplot2 last_plot 7 | ##' @param figure output figure function, set NULL output the current figure 8 | ##' @param filename output format (could be ingored) 9 | ##' @param save save figure or not (default: TRUE) 10 | ##' @examples 11 | ##' if(interactive()){ 12 | ##' require(ggplot2) 13 | ##' p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 14 | ##' tohtml(p,filename = file.path(tempdir(), "mtcars.html")) 15 | ##' ## or use ggplot directly 16 | ##' ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + 17 | ##' geom_point() 18 | ##' tohtml(save = FALSE) 19 | ##' } 20 | ##' @export 21 | ##' @author Kai Guo 22 | tohtml <- function(figure, filename= "temp.html", save = TRUE){ 23 | if(missing(figure)){ 24 | p <- last_plot() 25 | }else{ 26 | p <- print(figure) 27 | } 28 | p <- ggplotly(p) 29 | if(isTRUE(save)){ 30 | saveWidget(as_widget(p),filename) 31 | }else{ 32 | print(p) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onLoad <- function(libname, pkgname) { 2 | options(stringsAsFactors = FALSE) 3 | } 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eoffice 2 | [![](https://cranlogs.r-pkg.org/badges/eoffice)](https://cran.r-project.org/package=eoffice) 3 | CRAN version 4 | 5 | [![DOI](https://zenodo.org/badge/184794096.svg)](https://zenodo.org/badge/latestdoi/184794096) 6 | [![Project Status:](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) 7 | 8 | [![](https://img.shields.io/badge/devel%20version-0.2.2-green.svg)](https://github.com/guokai8/eoffice) 9 | 10 | Export and import graphics and tables to MicroSoft office 11 | 12 | ## Description 13 | _eoffice_ provide wrap functions to export and import graphics and data.frames in R to MicroSoft office with fully editable figures and tables(docx, pptx format). 14 | And _eoffice_ also provide write out figures with lots of different formats, such as pdf, eps, emf, tiff, svg, wmf, png and jpeg. _eoffice_ also support write out or display ggplot2 type figure with plotly. Since people may work on the platform without GUI support, _eoffice_ also provide function to easily write out figures to all above formats, pptx and docx._eoffice_ is avaiable on CRAN now. _eoffice_ provides function to extract colors from figures with different formats or pdf files. For linux platform please install imageMagick to use the _infigure_ function. 15 | 16 | on Debian/Ubuntu this is called libmagick++-dev: 17 | sudo apt-get install libmagick++-dev 18 | On Fedora or CentOS/RHEL we need ImageMagick-c++-devel: 19 | sudo yum install ImageMagick-c++-devel 20 | If you can't install the ImageMagick you can go with _eoffice_ 0.1.6 version 21 | ## Installation 22 | ``` 23 | library(devtools) 24 | ## install_github("guokai8/eoffice") 25 | ## eoffice is available on CRAN 26 | install.packages("eoffice") 27 | ``` 28 | 29 | ## Software Usage 30 | 31 | ``` 32 | library(eoffice) 33 | library(ggplot2) 34 | ## make a figure with you custom function 35 | plot(mtcars$mpg, mtcars$disp) 36 | topptx(filename = "mtcars.pptx") 37 | ## You can also use ggplot 38 | ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 39 | topptx(filename = "mtcars.pptx") 40 | ## or todocx(filename = "mtcars.docx") 41 | p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 42 | topptx(p, filename = "mtcars.pptx", width = 6, height = 4) 43 | ## use above method if you want use topptx with ggplot function in a loop 44 | ## write out table to office 45 | totable(head(mtcars), filename = "mtcars.docx") 46 | totable(head(mtcars), filename = "mtcars.pptx") 47 | ## append was supported if you want add figures or tables. 48 | ## Now support direct output of common objects produced by R statistical functions 49 | tt <- t.test(wt ~ am, mtcars) 50 | totable(tt, filename = "mtcars_test.pptx") 51 | totable(t.test(wt ~ am, mtcars), filename = "mtcars_test.pptx") 52 | ## inpptx and indocx provide function read the tables in pptx or docx 53 | tabs <- inpptx(filename = "mtcars.pptx", header = TRUE) 54 | ## output different figure formats 55 | tofigure(ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point(),filename = "mtcars.pdf") 56 | tofigure(ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point(),filename = "mtcars.eps") 57 | ## display figure with plotly 58 | tohtml(p, save = FALSE) 59 | ## if you use basic plot function or other plot function you need first use convertplot to convert 60 | ## it to ggplot object when you are working on the platform without GUI 61 | p <- convertplot(plot(1:10)) 62 | tofigure(p, filename = "test.pdf") 63 | topptx(p, filename = "test.pptx") 64 | ## if you use ggplot like function you don't need to transform the format 65 | ``` 66 | ## Note 67 | The _eoffice_ just a package for funs. _eoffice_ depends on _officer_ and _rvg_ package which include lots of fantastic functions. Here, _eoffice_ provides simplified functions which could be save some time to learn the complete functions from above packages. And there are some other packages provide these functions. Comparing with these packages, _eoffice_ include own features which I think really helpful to me. Besides, _eoffice_ also provide functions to read tables from pptx and docx. Read graphics and extract colors from the figures are available now. 68 | 69 | ## Contact information 70 | 71 | For any questions please contact guokai8@gmail.com 72 | 73 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate -------------------------------------------------------------------------------- /eoffice.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /man/col2hex.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{col2hex} 4 | \alias{col2hex} 5 | \title{col2hex gplots} 6 | \usage{ 7 | col2hex(cname) 8 | } 9 | \arguments{ 10 | \item{cname}{color name} 11 | } 12 | \description{ 13 | col2hex gplots 14 | } 15 | -------------------------------------------------------------------------------- /man/convertplot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{convertplot} 4 | \alias{convertplot} 5 | \title{convert basic plot to ggplot object} 6 | \usage{ 7 | convertplot(exp) 8 | } 9 | \arguments{ 10 | \item{exp}{expression formula of basic plot} 11 | } 12 | \value{ 13 | ggplot object 14 | } 15 | \description{ 16 | convert basic plot to ggplot object 17 | } 18 | \examples{ 19 | \donttest{ 20 | p <- convertplot(plot(1:10)) 21 | class(p) 22 | print(p) 23 | } 24 | } 25 | \author{ 26 | Kai Guo 27 | } 28 | -------------------------------------------------------------------------------- /man/dot-getext.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{.getext} 4 | \alias{.getext} 5 | \title{get the suffix name} 6 | \usage{ 7 | .getext(x) 8 | } 9 | \arguments{ 10 | \item{x}{filename} 11 | } 12 | \description{ 13 | get the suffix name 14 | } 15 | -------------------------------------------------------------------------------- /man/dot-getfilename.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{.getfilename} 4 | \alias{.getfilename} 5 | \title{get the prefix name} 6 | \usage{ 7 | .getfilename(x) 8 | } 9 | \arguments{ 10 | \item{x}{filename} 11 | } 12 | \description{ 13 | get the prefix name 14 | } 15 | -------------------------------------------------------------------------------- /man/dot-setcolnames.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{.setcolnames} 4 | \alias{.setcolnames} 5 | \title{set first row as column name} 6 | \usage{ 7 | .setcolnames(file) 8 | } 9 | \arguments{ 10 | \item{file}{input data frame} 11 | } 12 | \description{ 13 | set first row as column name 14 | } 15 | \author{ 16 | Kai Guo 17 | } 18 | -------------------------------------------------------------------------------- /man/dot-theme_blank.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{.theme_blank} 4 | \alias{.theme_blank} 5 | \title{blank theme} 6 | \usage{ 7 | .theme_blank() 8 | } 9 | \description{ 10 | blank theme 11 | } 12 | -------------------------------------------------------------------------------- /man/indocx.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/inoffice.R 3 | \name{indocx} 4 | \alias{indocx} 5 | \title{read table from docx} 6 | \usage{ 7 | indocx(filename, header = FALSE) 8 | } 9 | \arguments{ 10 | \item{filename}{input filename} 11 | 12 | \item{header}{use first row as column name} 13 | } 14 | \description{ 15 | read table from docx 16 | } 17 | \examples{ 18 | totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.docx")) 19 | tabs <- indocx(filename = file.path(tempdir(), "mtcars.docx"), header = TRUE) 20 | tabs 21 | } 22 | \author{ 23 | Kai Guo 24 | } 25 | -------------------------------------------------------------------------------- /man/infigure.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/inFigure.R 3 | \name{infigure} 4 | \alias{infigure} 5 | \title{import figures and extract the colors used in the figures} 6 | \usage{ 7 | infigure( 8 | filename, 9 | format = NULL, 10 | exclude_col = NULL, 11 | topn = 10, 12 | showfig = FALSE, 13 | showcol = FALSE, 14 | savegg = FALSE, 15 | density = 300, 16 | pages = NULL 17 | ) 18 | } 19 | \arguments{ 20 | \item{filename}{input filename} 21 | 22 | \item{format}{format of input file} 23 | 24 | \item{exclude_col}{vector of colors to be excluded from the analysis} 25 | 26 | \item{topn}{display the most frequent colors} 27 | 28 | \item{showfig}{display the figure or not (default: FALSE)} 29 | 30 | \item{showcol}{display extracted colors or not (default: FALSE)} 31 | 32 | \item{savegg}{save the figure as ggplot2 object or not (default: FALSE)} 33 | 34 | \item{density}{resolution to render pdf} 35 | 36 | \item{pages}{integer vector with page numbers (pdf file). Defaults to all pages.} 37 | } 38 | \description{ 39 | import figures and extract the colors used in the figures 40 | } 41 | \examples{ 42 | if(interactive()){ 43 | require(ggplot2) 44 | p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 45 | tofigure(p,filename = file.path(tempdir(), "mtcars.pdf")) 46 | pp <- infigure(filename = file.path(tempdir(), "mtcars.pdf"), exclude_col="white") 47 | pp 48 | } 49 | } 50 | \author{ 51 | Kai Guo 52 | } 53 | -------------------------------------------------------------------------------- /man/inoffice.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/inoffice.R 3 | \name{inoffice} 4 | \alias{inoffice} 5 | \title{read tables from ppt and word} 6 | \usage{ 7 | inoffice(filename, format = NULL, header = TRUE) 8 | } 9 | \arguments{ 10 | \item{filename}{input filename} 11 | 12 | \item{format}{pptx or docx} 13 | 14 | \item{header}{use first row as column name} 15 | } 16 | \description{ 17 | read tables from ppt and word 18 | } 19 | \examples{ 20 | ## use tempdir for the example 21 | totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.docx")) 22 | tabs <- inoffice(filename = file.path(tempdir(), "mtcars.docx"), header = TRUE) 23 | tabs 24 | } 25 | \author{ 26 | Kai Guo 27 | } 28 | -------------------------------------------------------------------------------- /man/inpptx.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/inoffice.R 3 | \name{inpptx} 4 | \alias{inpptx} 5 | \title{read table from pptx} 6 | \usage{ 7 | inpptx(filename, header = FALSE) 8 | } 9 | \arguments{ 10 | \item{filename}{input filename} 11 | 12 | \item{header}{use first row as column name} 13 | } 14 | \description{ 15 | read table from pptx 16 | } 17 | \examples{ 18 | totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.pptx")) 19 | tabs <- inpptx(filename = file.path(tempdir(), "mtcars.pptx"), header = TRUE) 20 | tabs 21 | } 22 | \author{ 23 | Kai Guo 24 | } 25 | -------------------------------------------------------------------------------- /man/todocx.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/toffice.R 3 | \name{todocx} 4 | \alias{todocx} 5 | \title{export figure to docx} 6 | \usage{ 7 | todocx( 8 | figure = NULL, 9 | filename = NULL, 10 | title = "", 11 | width = 6, 12 | height = 6, 13 | append = FALSE, 14 | devsize = FALSE, 15 | units = "in" 16 | ) 17 | } 18 | \arguments{ 19 | \item{figure}{plot figure function} 20 | 21 | \item{filename}{output filename} 22 | 23 | \item{title}{title for the figure} 24 | 25 | \item{width}{width of the output figure} 26 | 27 | \item{height}{height of the output figure} 28 | 29 | \item{append}{append or not} 30 | 31 | \item{devsize}{Boolean value show use device size or not (default = FALSE)} 32 | 33 | \item{units}{the units in which to return the value – inches, cm, or pixels (device units)} 34 | } 35 | \description{ 36 | export figure to docx 37 | } 38 | \examples{ 39 | if(interactive()){ 40 | plot(mtcars$mpg, mtcars$disp) 41 | todocx(filename = file.path(tempdir(), "mtcars.docx")) 42 | ## use ggplot2 43 | ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 44 | todocx(filename = file.path(tempdir(), "mtcars.docx"), height = 6, width = 4) 45 | } 46 | } 47 | \author{ 48 | Kai Guo 49 | } 50 | -------------------------------------------------------------------------------- /man/toffice.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/toffice.R 3 | \name{toffice} 4 | \alias{toffice} 5 | \title{export graph to MS office} 6 | \usage{ 7 | toffice( 8 | figure = NULL, 9 | format = "pptx", 10 | filename = "temp.pptx", 11 | nr = 1, 12 | nc = 1, 13 | irow = 1, 14 | icol = 1, 15 | onsame = FALSE, 16 | title = "", 17 | left = 0.15, 18 | top = 0.15, 19 | append = FALSE, 20 | width = 4, 21 | height = 4, 22 | devsize = FALSE, 23 | units = "in" 24 | ) 25 | } 26 | \arguments{ 27 | \item{figure}{plot figure function} 28 | 29 | \item{format}{file format} 30 | 31 | \item{filename}{output filename} 32 | 33 | \item{nr, nc}{two numbers to indicate the figures will be drawn in 34 | an nr-by-nc array on the device by columns or rows, respectively.} 35 | 36 | \item{irow, icol}{two number to indicate which row or column for the figure to be drawn} 37 | 38 | \item{onsame}{Boolean value show to draw on same slide or not} 39 | 40 | \item{title}{title for the figure} 41 | 42 | \item{left, top}{two numbers which gives the number of lines of margin to be specified 43 | on the left and right sides of the plot} 44 | 45 | \item{append}{append or not} 46 | 47 | \item{width}{width of the output figure} 48 | 49 | \item{height}{height of the output figure} 50 | 51 | \item{devsize}{Boolean value show use device size or not (default = FALSE)} 52 | 53 | \item{units}{the units in which to return the value – inches, cm, or pixels (device units)} 54 | } 55 | \description{ 56 | export graph to MS office 57 | } 58 | \examples{ 59 | if(interactive()){ 60 | plot(mtcars$mpg, mtcars$disp) 61 | toffice(filename = file.path(tempdir(), "mtcars.pptx"), format = "pptx") 62 | ## use ggplot2 63 | ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 64 | toffice(filename = file.path(tempdir(), "mtcars.pptx"),format = "pptx") 65 | } 66 | } 67 | \author{ 68 | Kai Guo 69 | } 70 | -------------------------------------------------------------------------------- /man/tofigure.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/toFigure.R 3 | \name{tofigure} 4 | \alias{tofigure} 5 | \title{output figures to different formats} 6 | \usage{ 7 | tofigure(figure, format = NULL, filename = "temp.pdf") 8 | } 9 | \arguments{ 10 | \item{figure}{output figure function, set NULL output the current figure} 11 | 12 | \item{format}{output format (could be ingored)} 13 | 14 | \item{filename}{output filename with different format as suffix} 15 | } 16 | \description{ 17 | output figures to different formats 18 | } 19 | \examples{ 20 | require(ggplot2) 21 | p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 22 | tofigure(p,filename = file.path(tempdir(), "mtcars.pdf")) 23 | ## or use ggplot directly 24 | tofigure(ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + 25 | geom_point(), filename = file.path(tempdir(), "mtcars.eps")) 26 | \donttest{ 27 | ## if you use basic plot function or other plot function you 28 | ## need first use convertplot to convert it to ggplot object 29 | ## when you are working on the platform without GUI 30 | p <- convertplot(plot(1:10)) 31 | tofigure(p, filename = file.path(tempdir(), "mtcars.pdf")) 32 | topptx(p, filename = file.path(tempdir(), "mtcars.pptx")) 33 | } 34 | } 35 | \author{ 36 | Kai Guo 37 | } 38 | -------------------------------------------------------------------------------- /man/tohtml.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tohtml.R 3 | \name{tohtml} 4 | \alias{tohtml} 5 | \title{export as plotly html (only support ggplot2 object)} 6 | \usage{ 7 | tohtml(figure, filename = "temp.html", save = TRUE) 8 | } 9 | \arguments{ 10 | \item{figure}{output figure function, set NULL output the current figure} 11 | 12 | \item{filename}{output format (could be ingored)} 13 | 14 | \item{save}{save figure or not (default: TRUE)} 15 | } 16 | \description{ 17 | export as plotly html (only support ggplot2 object) 18 | } 19 | \examples{ 20 | if(interactive()){ 21 | require(ggplot2) 22 | p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 23 | tohtml(p,filename = file.path(tempdir(), "mtcars.html")) 24 | ## or use ggplot directly 25 | ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + 26 | geom_point() 27 | tohtml(save = FALSE) 28 | } 29 | } 30 | \author{ 31 | Kai Guo 32 | } 33 | -------------------------------------------------------------------------------- /man/topptx.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/toffice.R 3 | \name{topptx} 4 | \alias{topptx} 5 | \title{export figure to pptx} 6 | \usage{ 7 | topptx( 8 | figure = NULL, 9 | filename = NULL, 10 | nr = 1, 11 | nc = 1, 12 | irow = 1, 13 | icol = 1, 14 | onsame = FALSE, 15 | title = "", 16 | left = 0.15, 17 | top = 0.15, 18 | width = 6, 19 | height = 6, 20 | append = FALSE, 21 | devsize = FALSE, 22 | units = "in" 23 | ) 24 | } 25 | \arguments{ 26 | \item{figure}{plot figure function} 27 | 28 | \item{filename}{output filename} 29 | 30 | \item{nr, nc}{two numbers to indicate the figures will be drawn in 31 | an nr-by-nc array on the device by columns or rows, respectively.} 32 | 33 | \item{irow, icol}{the number to indicate which row or column for the figure to be drawn} 34 | 35 | \item{onsame}{Boolean value show to draw on same slide or not} 36 | 37 | \item{title}{title for the figure} 38 | 39 | \item{left, top}{two numbers which gives the number of lines of margin to be specified 40 | on the left and right sides of the plot} 41 | 42 | \item{width}{width of the output figure} 43 | 44 | \item{height}{height of the output figure} 45 | 46 | \item{append}{append or not} 47 | 48 | \item{devsize}{Boolean value show use device size or not (default = FALSE)} 49 | 50 | \item{units}{the units in which to return the value – inches, cm, or pixels (device units)} 51 | } 52 | \description{ 53 | export figure to pptx 54 | } 55 | \examples{ 56 | if(interactive()){ 57 | plot(mtcars$mpg, mtcars$disp) 58 | topptx(filename = file.path(tempdir(), "mtcars.pptx")) 59 | ## use ggplot2 60 | ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 61 | topptx(filename = file.path(tempdir(), "mtcars.pptx")) 62 | } 63 | } 64 | \author{ 65 | Kai Guo 66 | } 67 | -------------------------------------------------------------------------------- /man/totable.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/toTable.R 3 | \name{totable} 4 | \alias{totable} 5 | \title{write table out to pptx or docx} 6 | \usage{ 7 | totable(data, filename, format = NULL, append = FALSE) 8 | } 9 | \arguments{ 10 | \item{data}{datasets} 11 | 12 | \item{filename}{output filename} 13 | 14 | \item{format}{pptx or docx} 15 | 16 | \item{append}{append into file} 17 | } 18 | \description{ 19 | write table out to pptx or docx 20 | } 21 | \examples{ 22 | tt <- t.test(wt ~ am, mtcars) 23 | totable(tt, filename = file.path(tempdir(), "mtcars.pptx")) 24 | totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.pptx")) 25 | totable(head(mtcars), filename = file.path(tempdir(), "mtcars.docx")) 26 | } 27 | \author{ 28 | Kai Guo 29 | } 30 | -------------------------------------------------------------------------------- /man/toxlsx.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/toffice.R 3 | \name{toxlsx} 4 | \alias{toxlsx} 5 | \title{export figure to pptx} 6 | \usage{ 7 | toxlsx( 8 | figure = NULL, 9 | filename = NULL, 10 | width = 6, 11 | height = 6, 12 | devsize = FALSE, 13 | units = "in" 14 | ) 15 | } 16 | \arguments{ 17 | \item{figure}{plot figure function} 18 | 19 | \item{filename}{output filename} 20 | 21 | \item{width}{width of the output figure} 22 | 23 | \item{height}{height of the output figure} 24 | 25 | \item{devsize}{Boolean value show use device size or not (default = FALSE)} 26 | 27 | \item{units}{the units in which to return the value – inches, cm, or pixels (device units)} 28 | } 29 | \description{ 30 | export figure to pptx 31 | } 32 | \examples{ 33 | if(interactive()){ 34 | plot(mtcars$mpg, mtcars$disp) 35 | toxlsx(filename = file.path(tempdir(), "mtcars.xlsx")) 36 | ## use ggplot2 37 | ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 38 | toxlsx(filename = file.path(tempdir(), "mtcars.xlsx")) 39 | } 40 | } 41 | \author{ 42 | Kai Guo 43 | } 44 | -------------------------------------------------------------------------------- /vignettes/eoffice.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "An R package for exporting and importing graphics and tables of Microsoft Office" 3 | author: 4 | - name: Kai Guo 5 | affiliation: 6 | - Biomedical Sciences, University of North Dakota 7 | date: "`r Sys.Date()`" 8 | output: 9 | html_document: 10 | df_print: paged 11 | word_document: 12 | toc: yes 13 | toc_depth: '6' 14 | rmarkdown::html_vignette: default 15 | pdf_document: 16 | toc: yes 17 | toc_depth: 6 18 | vignette: | 19 | \usepackage[utf8]{inputenc} 20 | %\VignetteIndexEntry{eoffice} 21 | %\VignetteEngine{knitr::knitr} 22 | --- 23 | 24 | ## 1. Introduction 25 | 26 | _eoffice_ provide wrap functions to export and import graphics and data.frames in R to __Microsoft Office__ (docx, pptx format). And _eoffice_ also provide write out figures with lots of different formats, such as pdf, eps, emf, tiff, svg, wmf, png and jpeg. Since people may work on the platform without GUI support, _eoffice_ also provide function to easily write out figures to all above formats, pptx and docx. 27 | 28 | ## 2. Software Usage 29 | ### 2.1 Installation 30 | ``` {r install,eval = FALSE} 31 | # library(devtools) 32 | # install_github("guokai8/eoffice") 33 | ``` 34 | ### 2.2 Load package 35 | ```{r load, results = 'hide', message = FALSE} 36 | library(eoffice) 37 | library(ggplot2) 38 | ``` 39 | ### 2.3 Main Functions 40 | -- _topptx_ write figures to powerpoint file. 41 | 42 | -- _todocx_ write figures to word file. 43 | 44 | -- _totable_ write data frame or summary results of different model to powerpoint or word files 45 | 46 | -- _indocx_ read tables from word file 47 | 48 | -- _inpptx_ read tables from powerpoint file 49 | 50 | -- _tofigure_ write figures to different output formats. 51 | 52 | -- _tohtml_ write figures to interactive html file 53 | 54 | -- _infigure_ read figures or pdf files into R and extract colors from these files. 55 | ```{r eval=FALSE} 56 | Please install imageMagick to use the _infigure_ function. On Linux you need to install the ImageMagick++ library: on Debian/Ubuntu this is called libmagick++-dev: 57 | sudo apt-get install libmagick++-dev 58 | On Fedora or CentOS/RHEL we need ImageMagick-c++-devel: 59 | sudo yum install ImageMagick-c++-devel 60 | ``` 61 | ### 2.4 Quick Tour 62 | ``` {r fig2, fig.width = 6, fig.height = 5, fig.align = "center"} 63 | plot(mtcars$mpg, mtcars$disp, col = factor(mtcars$cyl), pch = 20) 64 | topptx(filename = file.path(tempdir(), "mtcars.pptx")) 65 | ## if you prefer ggplot2 66 | ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 67 | topptx(filename = file.path(tempdir(), "mtcars.pptx")) 68 | ## or todocx(filename = "mtcars.docx") 69 | p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 70 | topptx(p, filename = file.path(tempdir(), "mtcars.pptx"), width = 6, height = 4) 71 | ## use above method if you want use topptx with ggplot function in a loop 72 | ## write out table to office 73 | totable(head(mtcars), filename = file.path(tempdir(), "mtcars.pptx")) 74 | totable(head(mtcars), filename = file.path(tempdir(), "mtcars.pptx")) 75 | ## append was supported if you want add figures or tables. 76 | ## Now support direct output of common objects produced by R statistical functions 77 | tt <- t.test(wt ~ am, mtcars) 78 | totable(tt, filename = file.path(tempdir(), "mtcars.pptx")) 79 | totable(t.test(wt ~ am, mtcars), filename = file.path(tempdir(), "mtcars.pptx")) 80 | ## inpptx and indocx provide function read the tables in pptx or docx 81 | tabs <- inpptx(filename = file.path(tempdir(), "mtcars.pptx"), header = TRUE) 82 | ## output different figure formats 83 | p <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point() 84 | tofigure(p, filename = file.path(tempdir(), "mtcars.pdf")) 85 | tofigure(ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) + geom_point(), filename = file.path(tempdir(), "mtcars.eps")) 86 | ## Not support emf and eps formats 87 | infigure(file.path(tempdir(), "mtcars.pdf"),exclude_col = c("white","black")) 88 | ## if you use basic plot function or other plot function you need first use convertplot to convert 89 | ## it to ggplot object when you are working on the platform without GUI 90 | ## p <- convertplot(plot(1:10)) 91 | ## tofigure(p, filename = file.path(tempdir(), "mtcars.pdf")) 92 | ## topptx(p, filename = file.path(tempdir(), "mtcars.pptx")) 93 | ## if you use ggplot like function you don't need to transform the format 94 | ``` 95 | 96 | ## Note 97 | _eoffice_ mainly depends on _officer_ and _rvg_ package which include lots of fantastic functions. Here, _eoffice_ provides simplified functions which could be save some time to learn the complete functions from above packages. And there are some packages provide these functions. Comparing with these packages, _eoffice_ include own features which I think really helpful to me. Besides, _eoffice_ also provide functions to read tables from pptx and docx. Read graphics functions will be available soon. 98 | 99 | ## Contact information 100 | 101 | For any questions please contact guokai8@gmail.com 102 | --------------------------------------------------------------------------------