Skip to content

Commit

Permalink
Update for CRAN resubmission
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwenseleers committed Oct 2, 2018
1 parent f5004bf commit 5d2a2c4
Show file tree
Hide file tree
Showing 47 changed files with 598 additions and 652 deletions.
4 changes: 2 additions & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
^.*\.Rproj$
examples
^\.Rproj\.user$
cran-comments.md
revdep
^revdep$
^.git$
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ CMakeLists.txt.user
.#*
*.autosave
*~
.orig
.orig
/revdep/.cache.rds
23 changes: 10 additions & 13 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ Authors@R: c(person("Tom", "Wenseleers", role = c("aut", "cre"),
person("Christophe", "Vanderaa", role = c("aut"),
email = "[email protected]"))
Depends: R (>= 3.0)
Imports: officer (>= 0.2.2),
rvg (>= 0.1.8),
stats,
Imports: stats,
utils,
datasets,
xtable (>= 1.8-2),
grDevices,
officer (>= 0.2.2),
rvg (>= 0.1.8),
xtable (>= 1.8-2),
flextable (>= 0.4.3),
rgl (>= 0.99.16),
xml2 (>= 1.2.0),
stargazer,
stargazer (>= 5.2.1),
openxlsx (>= 4.0.17),
broom (>= 0.4.4)
Suggests: lattice,
ggplot2 (>= 1.0),
latticeExtra (>= 0.6-28),
tikzDevice
Suggests: ggplot2 (>= 1.0)
Description: Easily export 'R' graphs and statistical output to 'Microsoft
Office' / 'LibreOffice', 'Latex' and 'HTML' Documents, using sensible defaults
that result in publication-quality output with simple, straightforward commands.
Expand All @@ -34,11 +31,11 @@ Description: Easily export 'R' graphs and statistical output to 'Microsoft
of the layout of 'R' graphs in 'Powerpoint' before final publication. Export
of graphs is flexible, and functions enable the currently showing R graph
or the currently showing 'R' stats object to be exported, but also allow the
graphs or stats output to be passed as objects. The package relies on package
'officer' for export to 'Office',and output files are also fully compatible
graphical or tabular output to be passed as objects. The package relies on package
'officer' for export to 'Office' documents,and output files are also fully compatible
with 'LibreOffice'. Base 'R', 'ggplot2' and 'lattice' plots are supported, as
well as a wide variety of 'R' stats objects, via wrappers to 'xtable', 'broom::tidy'
and 'stargazer', including 'aov', 'lm', 'glm', 'lme', 'lmer', 'glmer' and 'coxph' as
well as a wide variety of 'R' stats objects, via wrappers to xtable(), broom::tidy()
and stargazer(), including aov(), lm(), glm(), lme(), glmnet() and coxph() as
well as matrices and data frames and many more...
License: GPL-2
BugReports: https://github.com/tomwenseleers/export/issues
Expand Down
18 changes: 8 additions & 10 deletions R/graph2bitmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ graph2bitmap = function(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JP
"Arial","Helvetica")[[1]], bg = "white", cairo = TRUE,
tiffcompression = c("lzw","rle","jpeg","zip","lzw+p","zip+p"), jpegquality = 99, ...) {
type = toupper(type)
type = match.arg(type)
type = match.arg(type, c("PNG","JPG","TIF"))
if (type=="JPG") type="JPEG"
if (type=="TIFF") type="TIF"
tiffcompression = match.arg(tiffcompression)
Expand Down Expand Up @@ -104,15 +104,13 @@ graph2bitmap = function(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JP
}

if (type == "JPEG") {
jpeg( filename = file,
#type = ifelse(cairo,"cairo","windows"),
quality = jpegquality,
units = "in",
width = w,
height = h,
#family = font,
res = dpi,
bg = bg, ... )
jpeg(filename = file,
quality = jpegquality,
units = "in",
width = w,
height = h,
res = dpi,
bg = bg)#, ... )
myplot()
invisible(dev.off())
}
Expand Down
17 changes: 11 additions & 6 deletions R/graph2office.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ graph2office = function(x = NULL, file = "Rplot", fun = NULL, type = c("PPT","DO
p = obj
}
if (inherits(p,"list")) { stop("base R plots cannot be passed as objects, use ggplot2 or lattice plots instead") }
myplot = if (is.null(fun)) function(pl = p) print(pl) else fun
myplot = if (is.null(fun)){
function(pl = p) print(pl)
} else {
fun
}

### 2. Prepare the plotting region and the plot apsect
plotsize = dev.size() # also works if no graphics device is open
Expand Down Expand Up @@ -128,20 +132,21 @@ graph2office = function(x = NULL, file = "Rplot", fun = NULL, type = c("PPT","DO
### 4. Initialize the slide (ppt) or page (doc) that will contain the plot
if (type == "PPT") {
if (append & file.exists(file)){
doc <- officer::read_pptx(path = file)
doc <- read_pptx(path = file)
} else {
doc <- officer::read_pptx(path = templ)
doc <- read_pptx(path = templ)
}
doc = officer::add_slide(doc, layout = "Blank", master = "Office Theme")
doc = add_slide(doc, layout = "Blank", master = "Office Theme")
pagesize = get.slide.size(doc)
pagesize["width"]=pagesize["width"]-(margins["left"]+margins["right"])
pagesize["height"]=pagesize["height"]-(margins["top"]+margins["bottom"])
}
if (type == "DOC") {
if (append & file.exists(file)){
doc <- officer::read_docx(path = file)
doc <- read_docx(path = file)
doc <- body_add_break(doc, pos = "after")
} else {
doc <- officer::read_docx(path = templ)
doc <- read_docx(path = templ)
}
pagesize <- (doc$sect_dim$page - doc$sect_dim$margins[c(3,2)])/1440 # 1440 is a factor to convert to inches
}
Expand Down
7 changes: 5 additions & 2 deletions R/table2office.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#' for the column with p values.
#' @param digitspvals number of significant digits to show for columns with p
#' values.
#' @param width desired width of table in inches.
#' @param height desired height of table in inches.
#' @param width desired width of table in inches. If the given width exceeds the page or slide
#' width, the table width becomes the page/slide width.
#' @param height desired height of table in inches. If the given height exceeds the page or slide
#' height, the table height becomes the page/slide height.
#' @param offx x offset in inches to specify horizontal location of table (only for \code{type=="PPT"}).
#' @param offy y offset in inches to specify vertical location of table (only for \code{type=="PPT"}).
#' @param font desired font to use for output table; defaults to \code{"Arial"} on Windows
Expand Down Expand Up @@ -206,6 +208,7 @@ table2office = function(x = NULL, file = "Rtable", type = c("PPT","DOC"), append
} else if(type == "DOC"){
if (append & file.exists(file)) {
doc = read_docx(path = file)
doc = body_add_break(doc, pos = "after")
} else {
doc = read_docx()
}
Expand Down
4 changes: 2 additions & 2 deletions R/table2spreadsheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#' @param append logical value - if \code{TRUE} and \code{type="XLS"} it will add a new woorksheet to the given
#' file, where file can also be a given corporate.
#' \code{append=FALSE} any existing file will be overwritten.
#' @param sheetName a string giving the name of the new sheet that is created (only when \code{type=="XLS"} and
#' \code{append==TRUE}). It must unique (case insensitive) from any existing sheet in the file.
#' @param sheetName a string giving the name of the new sheet that is created (only for \code{type=="XLS"}).
#' It must be unique (case insensitive) from any existing sheet name in the file.
#' @param digits number of significant digits to show for all columns except
#' for the column with p values.
#' @param digitspvals number of significant digits to show for columns with p
Expand Down
70 changes: 25 additions & 45 deletions R/table2tex.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
#' the output of the previous R command will be exported.
#' @param file name of output file. The appropriate extension is added automatically.
#' @param type desired output type - \code{"TEX"} for Latex and \code{"HTML"} for HTML.
#' @param digits number of significant digits to show. A value of NA indicates that
#' no rounding should be done.
#' @param digits number of significant digits to show for all columns except
#' for the column with p values.
#' @param digitspvals number of significant digits to show for columns with p
#' values.
#' @param summary logical indicating whether or not to summarize data files.
#' @param standAlone logical indicating whether exported Latex code should be
#' standalone compilable, or whether it will be pasted into another document.
#' @param add.rownames logical indicating whether the names of the rows should be added
#' to the table (inserting a column before first column).
#' @param \dots extra options are passed on to stargazer.
#' @return \code{NULL}
#' @details Objects that can be exported are all those supported by \code{\link[stargazer]{stargazer}}:
#' @details Objects that can be exported are all those supported by \code{\link[xtable]{xtable}},
#' \code{\link[broom]{tidy}} (see \code{\link{table2office}} for an extensive list of supported
#' methods), or \code{\link[stargazer]{stargazer}}. The models supported by
#' \code{\link[stargazer]{stargazer}} are:
#' \itemize{
#'
#' \item \code{aftreg} (\code{eha})
#' \item \code{anova} (\code{stats})
#' \item \code{aov} (\code{stats})
Expand Down Expand Up @@ -95,74 +99,50 @@
#' }
#' @author Tom Wenseleers, Christophe Vanderaa
#' @example examples/table2tex.R
#' @seealso \code{\link{table2office}} ,\code{\link{table2ppt}}, \code{\link{table2doc}}
#' @seealso \code{\link{table2office}} ,\code{\link{table2ppt}}, \code{\link{table2doc}},
#' \code{\link[stargazer]{stargazer}}
#' @export
#'

table2tex = function(x = NULL, file = "Rtable", type="TEX", digits = 2, summary=FALSE, standAlone=TRUE,
add.rownames = FALSE,...) {
table2tex = function(x = NULL, file = "Rtable", type="TEX", digits = 2, digitspvals = 2,
summary=FALSE, standAlone=TRUE, add.rownames = FALSE,...) {
# Get the data that will be exported
obj=x
if (is.null(obj))
outp = .Last.value else outp = obj # capture previously shown output or use passed object
if (is.null(outp))
stop("no R stats object available to export")
obj=outp

# Match the requested file type
type=toupper(type)
type=match.arg(type,c("TEX","HTML"))

if (type=="DOC") { ext=".doc" }
if (type=="HTML") { ext=".html" }
if (type=="TEX") { ext=".tex" }
file = sub("^(.*)[.].*", "\\1", file) # remove extension if given
file = paste0(file, ext) # add extension

# define some additional methods for some objects
# not supported by default by stargazer
as.data.frame.summary.aov = function(x) {
if(length(x) == 1) {
as.data.frame(x[[1]])
} else {
lapply(unlist(x, FALSE), as.data.frame)
}
}
# Depending on the class of the data call the formating function
if (length(intersect(class(obj), as.character(gsub("xtable.", "", methods(xtable))))) >= 1) {
obj <- xtable2(x=obj, ndigits = digits, ndigitspvals = digitspvals)
obj <- as.data.frame(obj)
} else if (length(intersect(class(obj), as.character(gsub("tidy.", "", methods(tidy))))) >= 1) {
obj <- tidy2(x=obj, ndigits = digits, ndigitspvals = digitspvals)
obj <- as.data.frame(obj)
}
# Else supported objects that should be supported by stargazer

if ("aov" %in% class(obj)) {obj=as.data.frame(anova(obj));summary=FALSE}
if ("summary.aov" %in% class(obj)) {obj=as.data.frame(obj);summary=FALSE}

# CHECK support for xtabs, ftable and CrossTable - contacted original author on this
# if ("xtabs" %in% class(outp)) outp = ftable(outp)
# if ("ftable" %in% class(outp)) XXX

# object supported by stargazer?
supported=!grepl("Unrecognized object type",paste(capture.output(stargazer(obj)),collapse=""))
if (!supported) stop(paste0("Object of class ",class(obj)," is not supported by stargazer."))

# objects supported by stargazer
# aftreg (eha), anova (stats), aov (stats), aovlist (stats), arima (stats),
# betareg (betareg), binaryChoice (sampleSelection), bj (rms), brglm (brglm),
# censReg (censReg), coeftest (lmtest), coxph (survival), coxreg (eha),
# clm (ordinal), clogit (survival), cph (rms), dynlm (dynlm), ergm (ergm),
# errorsarlm (spdev), felm (lfe), gam (mgcv), garchFit (fGarch), gee (gee),
# glm (stats), Glm (rms), glmer (lme4), glmrob(robustbase), gls (nlme),
# Gls (rms), gmm (gmm), heckit (sampleSelection), hetglm (glmx), hurdle (pscl),
# ivreg (AER), lagarlm (spdep), lm(stats), lme (nlme), lmer (lme4), lmrob (robustbase),
# lrm (rms), maBina (erer), mclogit (mclogit), mlogit (mlogit), mnlogit (mnlogit),
# mlreg (eha), multinom (nnet), nlme (nlme), nlmer (lme4), ols (rms), pgmm (plm),
# phreg (eha), plm (plm), pmg (plm), polr (MASS), psm (rms), rem.dyad (relevent),
# rlm (MASS), rq (quantreg), Rq (rms), selection (sampleSelection), svyglm (survey),
# survreg (survival), tobit (AER), weibreg (eha), zeroin (pscl), relogit (zelig),
# cloglog.net (zelig), gamma.net (zelig), probit.net (zelig) and logit.net (zelig).
# not prcomp, ts or zoo?
# not ftable, xtab, CrossTable?

# preview table in viewer or browser
htmlout = capture.output(stargazer(obj, type="html", summary=summary, rownames = add.rownames, ...))
texout = capture.output(stargazer(obj, type="latex", summary=summary, rownames = add.rownames, ...))
htmlout = capture.output(stargazer(obj, type="html", summary=summary, rownames = add.rownames, digits = digits,...))
texout = capture.output(stargazer(obj, type="latex", summary=summary, rownames = add.rownames, digits = digits,...))
outp = preview(htmlout)

# export to HTML/Latex

if (standAlone) {
texheader=c("\\documentclass[10pt]{article}","\\begin{document}")
texfooter=c("\\end{document}")
Expand Down
6 changes: 3 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ xtable2 = function(x, ndigits = 2, ndigitspvals = 2, ...) {
whch = grep("\\QPr(\\E|\\Qp-value\\E|\\Qp value\\E|\\Qpadj\\E|^p$|^padj$", colnames(sm))
if (length(whch) != 0) {
digs[whch + 1] = ndigitspvals
disp[whch + 1] = "g"
disp[whch + 1] = "f"
}
whch = grep("^Df$|^df$", colnames(sm))
if (length(whch) != 0){
digs[whch + 1] = 0
disp[whch + 1] = "d"
}
digs[c(1,which(!sapply(x,is.numeric))+1)] <- 0
disp[c(1,which(!sapply(x,is.numeric))+1)] <- "s"
digs[c(1,which(!apply(sm,2,is.numeric))+1)] <- 0
disp[c(1,which(!apply(sm,2,is.numeric))+1)] <- "s"
for(i in 2:length(digs)){
if(!is.na(digs[i])) sm[,i-1] <- round(sm[,i-1], digits = digs[i])
}
Expand Down
17 changes: 17 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ Platform: x86_64-pc-linux-gnu (64-bit)

Running under: Ubuntu 17.10

## Resubmission

This is a resubmission. In this version I have:

* Changed function names in the DESCRIPTION by adding '()'.

* Reorganized the examples and replaced every '\dontrun'
satement by the '\donttest' statement. This is needed
because some examples require screen devices to be
opened which is not allowed for examples (R CMD CHECK
gives following error when the example is executed
without '\donttest': "screen devices should not be used
in examples etc"").

* Files in the examples are now written in a temporary folder
(using tempdir()) instead of the package directory in order
to comply to CRAN policy.

## R CMD check results

Expand Down
Binary file removed data/wine.rda
Binary file not shown.
34 changes: 27 additions & 7 deletions examples/graph2bitmap.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
# Create a file name
dir <- tempdir()
filen <- paste0(dir,"\\ggplot")

# Generate graphical output
library(ggplot2)
library(datasets)
x=qplot(Sepal.Length, Petal.Length, data = iris,
color = Species, size = Petal.Width, alpha = I(0.7))
x
graph2png(x=x,dpi=400)
graph2tif(x=x)
graph2jpg(x=x)
plot.fun <- function(){
print(qplot(Sepal.Length, Petal.Length, data = iris,
color = Species, size = Petal.Width, alpha = 0.7))
}

\dontrun{
# use active graph instead of passing plot as object
graph2png(aspectr=1.7)
# There are 3 ways to use graph2bitmap():
### 1. Pass the plot as an object
graph2png(x=x, file=filen, dpi=400, height = 5, aspectr=4)
graph2tif(x=x, file=filen, dpi=400, height = 5, aspectr=4)
graph2jpg(x=x, file=filen, dpi=400, height = 5, aspectr=4)
### 2. Get the plot from current screen device
\donttest{ # Because the example uses screen devices
x
graph2png(file=filen, dpi=400, height = 5, aspectr=4)
graph2tif(file=filen, dpi=400, height = 5, aspectr=4)
graph2jpg(file=filen, dpi=400, height = 5, aspectr=4)
}
### 3. Pass the plot as a function
\donttest{ # Because the example uses screen devices
graph2png(file=filen, fun=plot.fun, dpi=400, height = 5, aspectr=4)
graph2tif(file=filen, fun=plot.fun, dpi=400, height = 5, aspectr=4)
graph2jpg(file=filen, fun=plot.fun, dpi=400, height = 5, aspectr=4)
}
Loading

0 comments on commit 5d2a2c4

Please sign in to comment.