Skip to content

Commit

Permalink
updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwenseleers committed Jul 23, 2015
1 parent e842bd8 commit c9cb4fb
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 149 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
^.*\.Rproj$
examples
^\.Rproj\.user$
13 changes: 7 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ Maintainer: Tom Wenseleers <[email protected]>
Depends:
R (>= 3.0),
ReporteRs (>= 0.7),
ReporteRsjars (>= 0.0.2),
ReporteRsjars (>= 0.0.2)
Imports:
stats,
graphics,
grDevices,
utils,
datasets,
methods
Imports:
methods,
rJava,
rtable (>= 0.1),
xtable (>= 1.7),
tikzDevice (>= 0.8),
stargazer (>= 5.1)
stargazer (>= 5.1),
graphics,
grDevices
Suggests:
lattice,
latticeExtra,
Expand Down
6 changes: 5 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export(table2tex)
export(table2tex2)
import(ReporteRs)
import(ReporteRsjars)
import(datasets)
import(grDevices)
import(graphics)
import(methods)
import(rJava)
import(rtable)
import(stargazer)
import(stats)
import(utils)
import(tikzDevice)
import(xtable)
14 changes: 7 additions & 7 deletions R/graph2bitmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ graph2bitmap = function(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JP
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

plotsize = dev.size() # also works if no graphics device is open
plotsize = grDevices::dev.size() # also works if no graphics device is open
w = plotsize[[1]]
h = plotsize[[2]]
plotaspectr = plotsize[[1]]/plotsize[[2]]
Expand All @@ -72,7 +72,7 @@ graph2bitmap = function(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JP
w = w*scaling/100; h = h*scaling/100;

if (type == "PNG") {
png( filename = file,
grDevices::png( filename = file,
type = ifelse(cairo,"cairo-png","windows"),
units = "in",
width = w,
Expand All @@ -81,11 +81,11 @@ graph2bitmap = function(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JP
res = dpi,
bg = bg, ...)
myplot()
invisible(dev.off())
invisible(grDevices::dev.off())
}

if (type == "TIF") {
tiff( filename = file,
grDevices::tiff( filename = file,
type = ifelse(cairo,"cairo","windows"),
compression = tiffcompression,
units = "in",
Expand All @@ -95,11 +95,11 @@ graph2bitmap = function(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JP
res = dpi,
bg = bg, ... )
myplot()
invisible(dev.off())
invisible(grDevices::dev.off())
}

if (type == "JPEG") {
jpeg( filename = file,
grDevices::jpeg( filename = file,
#type = ifelse(cairo,"cairo","windows"),
quality = jpegquality,
units = "in",
Expand All @@ -109,7 +109,7 @@ graph2bitmap = function(x = NULL, file = "Rplot", fun = NULL, type = c("PNG","JP
res = dpi,
bg = bg, ... )
myplot()
invisible(dev.off())
invisible(grDevices::dev.off())
}

message(paste0("Exported graph as ",file))
Expand Down
14 changes: 8 additions & 6 deletions R/graph2office.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#' Save the currently active R graph or a graph passed as an object or function
#' to Microsoft Office / LibreOffice format with sensible defaults
#'
#' @import datasets
#' @import graphics
#' @import rJava
#' @import ReporteRs
#' @import ReporteRsjars
#' @import grDevices
#' @import utils
#' @aliases graph2office graph2doc graph2ppt
#' @param x given \code{ggplot2} plot or \code{lattice} plot object to export; if
#' set to \code{NULL} the currently active R graph will be exported; not
Expand Down Expand Up @@ -89,7 +91,7 @@ graph2office = function(x = NULL, file = "Rplot", fun = NULL, type = c("PPT","DO
function(pl = p) print(pl) else fun
#myplot()

plotsize = dev.size() # also works if no graphics device is open
plotsize = grDevices::dev.size() # also works if no graphics device is open
w = plotsize[[1]]
h = plotsize[[2]]
plotaspectr = plotsize[[1]]/plotsize[[2]]
Expand Down Expand Up @@ -201,10 +203,10 @@ sizes=(5:1)[1:length(landscA)]
portrA=lapply(landscA,rev) # size of A5 to A1 landscape PPT/DOC templates
#w=8.9;h=6.7;
if (orient=="auto") orient=ifelse(w>=h,"landscape","portrait")
bestpagesize=suppressWarnings(ifelse( orient=="landscape", sizes[max( min(which(w<=unlist(lapply(landscA,head,n=1)))),
min(which(h<=unlist(lapply(landscA,tail,n=1)))) )],
sizes[max( min(which(w<=unlist(lapply(portrA,head,n=1)))),
min(which(h<=unlist(lapply(portrA,tail,n=1)))) )] ))
bestpagesize=suppressWarnings(ifelse( orient=="landscape", sizes[max( min(which(w<=unlist(lapply(landscA,utils::head,n=1)))),
min(which(h<=unlist(lapply(landscA,utils::tail,n=1)))) )],
sizes[max( min(which(w<=unlist(lapply(portrA,utils::head,n=1)))),
min(which(h<=unlist(lapply(portrA,utils::tail,n=1)))) )] ))
if (is.na(bestpagesize)) bestpagesize=min(sizes)
return(paste0("A",bestpagesize,"_",orient)) }

Expand Down
7 changes: 4 additions & 3 deletions R/graph2tex.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#'
#'
#' @import grDevices
#' @import tikzDevice
#' @aliases graph2tex graph2tex2
#' @param x given \code{ggplot2} plot or \code{lattice} plot object to export; if
#' set to \code{NULL} the currently active R graph will be exported; not
Expand Down Expand Up @@ -50,7 +51,7 @@ graph2tex = function(x = NULL, file = "Rplot", fun = NULL,
myplot = if (is.null(fun)) function(pl = p) print(pl) else fun

if (!is.na(font)) {if (font=="sans-serif"|font=="Helvetica"|font=="Arial"|font=="Verdana"|font=="Tahoma") font="sans"}
plotsize = dev.size() # also works if no graphics device is open
plotsize = grDevices::dev.size() # also works if no graphics device is open
w = plotsize[[1]]
h = plotsize[[2]]
plotaspectr = plotsize[[1]]/plotsize[[2]]
Expand All @@ -64,14 +65,14 @@ graph2tex = function(x = NULL, file = "Rplot", fun = NULL,
if ((!is.null(width))&(!is.null(height))) { w = width; h = height }
w = w*scaling/100; h = h*scaling/100;

tikzDevice::tikz(file = file,
tikz(file = file,
height = h,
width = w,
bg = bg,
standAlone = standAlone,
... )
myplot()
dev.off()
grDevices::dev.off()

if (!is.null(font)) { if ("sans" %in% font) {
out = readLines(file)
Expand Down
18 changes: 9 additions & 9 deletions R/graph2vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ graph2vector = function(x = NULL, file = "Rplot", fun = NULL, type = "SVG",
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

plotsize = dev.size() # also works if no graphics device is open
plotsize = grDevices::dev.size() # also works if no graphics device is open
w = plotsize[[1]]
h = plotsize[[2]]
plotaspectr = plotsize[[1]]/plotsize[[2]]
Expand All @@ -75,20 +75,20 @@ graph2vector = function(x = NULL, file = "Rplot", fun = NULL, type = "SVG",
w = w*scaling/100; h = h*scaling/100;

if (type == "SVG") {
svg(filename = file,
grDevices::svg(filename = file,
height = h,
width = w,
family = font,
onefile = FALSE,
bg = bg,
... )
myplot()
dev.off()
grDevices::dev.off()
}

if (type == "PDF") {
#cairo_surface_set_fallback_resolution() # check cairoSurfaceSetFallbackResolution in library(RGtk2)
if (!cairo) { pdf(file = file, # also check cairo_pdf
if (!cairo) { grDevices::pdf(file = file, # also check cairo_pdf
height = h,
width = w,
family = font,
Expand All @@ -97,7 +97,7 @@ graph2vector = function(x = NULL, file = "Rplot", fun = NULL, type = "SVG",
colormodel = colormodel,
useDingbats = FALSE,
... ) } else {
cairo_pdf(filename = file, # also check cairo_pdf
grDevices::cairo_pdf(filename = file, # also check cairo_pdf
height = h,
width = w,
family = font,
Expand All @@ -106,19 +106,19 @@ graph2vector = function(x = NULL, file = "Rplot", fun = NULL, type = "SVG",
... )
}
myplot()
dev.off()
grDevices::dev.off()
}

if (type == "EPS") {
if (!cairo) { postscript(file = file,
if (!cairo) { grDevices::postscript(file = file,
height = h,
width = w,
family = font,
onefile = FALSE,
bg = bg,
colormodel = colormodel,
... ) } else {
cairo_ps(filename = file,
grDevices::cairo_ps(filename = file,
height = h,
width = w,
family = font,
Expand All @@ -127,7 +127,7 @@ graph2vector = function(x = NULL, file = "Rplot", fun = NULL, type = "SVG",
... )
}
myplot()
dev.off()
grDevices::dev.off()
}

message(paste0("Exported graph as ",file))
Expand Down
26 changes: 13 additions & 13 deletions R/table2ppt.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#' Export currently showing R stats object or stats object obj to a Microsoft
#' Powerpoint / LibreOffice table
#'
#' @import rJava
#' @import ReporteRs
#' @import xtable
#' @import rtable
#' @import methods
#' @import stats
#' @import grDevices
#' @import utils
#' @aliases table2ppt
#' @param x given R stats object to export; if set to \code{NULL} the output of the
#' previous R command will be exported.
Expand Down Expand Up @@ -56,7 +56,7 @@ table2ppt = function(x = NULL, file = "Rtable", append = FALSE, digits = 2,
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")
supobjects = c(as.character(gsub("xtable.", "", methods(xtable))), "xtabs", "ftable")
supobjects = c(as.character(gsub("xtable.", "", utils::methods(xtable))), "xtabs", "ftable")
# objects supported by xtable
# 'anova' 'aov' 'aovlist' 'coxph' 'data.frame' 'glm' 'lm'
# 'matrix' 'prcomp' 'summary.aov' 'summary.aovlist' 'summary.glm'
Expand All @@ -68,7 +68,7 @@ table2ppt = function(x = NULL, file = "Rtable", append = FALSE, digits = 2,
# tempfile = tempfile(pattern='rhistory_', fileext='.txt')
# savehistory(tempfile)
# myhistory = readLines(tempfile)
# h = tail(h, 5) # last 5 commands eval(parse(text=h[length(h)-1]))
# h = stats::tail(h, 5) # last 5 commands eval(parse(text=h[length(h)-1]))
# exec last command but 1 see
# also http://stackoverflow.com/questions/20959561/accessing-r-history-from-r-code

Expand All @@ -94,12 +94,12 @@ table2ppt = function(x = NULL, file = "Rtable", append = FALSE, digits = 2,

# deal with specific classes of objects not supported by xtable
if ("summary.merMod" %in% class(outp))
outp = data.frame(coef(summary(outp)), check.names = F)
outp = data.frame(stats::coef(summary(outp)), check.names = F)
if ("xtabs" %in% class(outp))
outp = ftable(outp)
supobjects = as.character(gsub("xtable.", "", methods(xtable)))
outp = stats::ftable(outp)
supobjects = as.character(gsub("xtable.", "", utils::methods(xtable)))
if ("ftable" %in% class(outp))
tab = ftable(outp) else {
tab = stats::ftable(outp) else {
if (length(intersect(class(outp), supobjects)) >= 1)
tab = xtable2(outp, ...) else outp = data.frame(outp, check.names = F)
}
Expand All @@ -108,10 +108,10 @@ table2ppt = function(x = NULL, file = "Rtable", append = FALSE, digits = 2,
# see https://davidgohel.github.io/ReporteRs/formatting_properties.html
flextab = as.FlexTable(x = tab, add.rownames = add.rownames)
#if (zebra) flextab=setZebraStyle( flextab, odd = odd, even = even)
flextab[] = ReporteRs::textProperties(font.size=pointsize, font.family=font)
flextab[] = textProperties(font.size=pointsize, font.family=font)
# flextab[1,, to="header"] = textProperties(color=headertext,
# font.size=pointsize, font.weight = "bold", font.family=font)
flextab[1,, to="header"] = ReporteRs::textProperties(font.size=pointsize,
flextab[1,, to="header"] = textProperties(font.size=pointsize,
font.weight = "bold", font.family=font)
# flextab[1,, to="header"] = parProperties(shading.color=header)
# change to setFlexTableBackgroundColors
Expand All @@ -120,8 +120,8 @@ table2ppt = function(x = NULL, file = "Rtable", append = FALSE, digits = 2,
file = sub("^(.*)[.].*", "\\1", file) # remove extension if given
file = paste0(file, ext) # add extension

if (append & file.exists(file)) { doc = ReporteRs::pptx(template = file) } else { doc = ReporteRs::pptx() }
doc = ReporteRs::addSlide(doc, slide.layout = "Blank")
if (append & file.exists(file)) { doc = pptx(template = file) } else { doc = pptx() }
doc = addSlide(doc, slide.layout = "Blank")
pagesize = dim(doc)$slide.dim

tblaspectr = flextab$numcol * 0.7/flextab$numrow # guess table aspect ratio
Expand All @@ -141,9 +141,9 @@ table2ppt = function(x = NULL, file = "Rtable", append = FALSE, digits = 2,
if (!is.null(height))
h = height

doc = ReporteRs::addFlexTable(doc, flextab, offx = offx, offy = offy, width = w, height = h)
doc = addFlexTable(doc, flextab, offx = offx, offy = offy, width = w, height = h)

ReporteRs::writeDoc(doc, file)
writeDoc(doc, file)

message(paste0("Exported table as ",file))

Expand Down
12 changes: 6 additions & 6 deletions R/table2tex.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' Export currently showing R stats object or stats object obj to a
#' Microsoft Word / LibreOffice, HTML or Latex table
#' @import stats
#' @import utils
#' @import grDevices
#' @import stargazer
#' @aliases table2tex table2tex2 table2html table2doc
#' @param x given R stats object or list of stats objects to export; if set to \code{NULL}
#' the output of the previous R command will be exported.
Expand Down Expand Up @@ -65,15 +65,15 @@ table2tex = function(x = NULL, file = "Rtable", type="TEX", digits = 2, summary=
}
}

if ("aov" %in% class(obj)) {obj=as.data.frame(anova(obj));summary=FALSE}
if ("aov" %in% class(obj)) {obj=as.data.frame(stats::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 ("xtabs" %in% class(outp)) outp = stats::ftable(outp)
# if ("ftable" %in% class(outp)) XXX

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

# objects supported by stargazer
Expand All @@ -95,8 +95,8 @@ table2tex = function(x = NULL, file = "Rtable", type="TEX", digits = 2, summary=
# not ftable, xtab, CrossTable?

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

# export to HTML/Word/Latex
Expand Down
Loading

0 comments on commit c9cb4fb

Please sign in to comment.