Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added with_device and device_dev fns #37

Closed
wants to merge 11 commits into from
6 changes: 4 additions & 2 deletions DESCRIPTION
Expand Up @@ -20,12 +20,15 @@ License: GPL (>= 2)
LazyData: true
Imports:
stats,
graphics
graphics,
grDevices
Suggests:
testthat
Collate:
'with_.R'
'collate.R'
'wrap.R'
'devices.R'
'dir.R'
'env.R'
'libpaths.R'
Expand All @@ -34,7 +37,6 @@ Collate:
'options.R'
'par.R'
'path.R'
'wrap.R'
'sink.R'
'with.R'
RoxygenNote: 5.0.0
24 changes: 23 additions & 1 deletion NAMESPACE
@@ -1,9 +1,13 @@
# Generated by roxygen2: do not edit by hand
# Generated by roxygen2 (4.1.1): do not edit by hand
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please update your roxygen2 installation?


export(with_)
export(with_bmp)
export(with_cairo_pdf)
export(with_cairo_ps)
export(with_collate)
export(with_dir)
export(with_envvar)
export(with_jpeg)
export(with_libpaths)
export(with_locale)
export(with_makevars)
Expand All @@ -12,4 +16,22 @@ export(with_options)
export(with_output_sink)
export(with_par)
export(with_path)
export(with_pdf)
export(with_png)
export(with_postscript)
export(with_svg)
export(with_temp_libpaths)
export(with_tiff)
export(with_xfig)
importFrom(grDevices,bmp)
importFrom(grDevices,cairo_pdf)
importFrom(grDevices,cairo_ps)
importFrom(grDevices,dev.cur)
importFrom(grDevices,dev.off)
importFrom(grDevices,jpeg)
importFrom(grDevices,pdf)
importFrom(grDevices,png)
importFrom(grDevices,postscript)
importFrom(grDevices,svg)
importFrom(grDevices,tiff)
importFrom(grDevices,xfig)
98 changes: 98 additions & 0 deletions R/devices.R
@@ -0,0 +1,98 @@
# Internal *_dev functions ------------------------------------------------

#' @importFrom grDevices dev.cur
#' @include wrap.R
NULL

#' @importFrom grDevices bmp
bmp_dev <- wrap(bmp, NULL, dev.cur())

#' @importFrom grDevices cairo_pdf
cairo_pdf_dev <- wrap(cairo_pdf, NULL, dev.cur())

#' @importFrom grDevices cairo_ps
cairo_ps_dev <- wrap(cairo_ps, NULL, dev.cur())

#' @importFrom grDevices jpeg
jpeg_dev <- wrap(jpeg, antialias <- match.arg(antialias), dev.cur())

#' @importFrom grDevices pdf
pdf_dev <- wrap(pdf, NULL, dev.cur())

#' @importFrom grDevices png
png_dev <- wrap(png, antialias <- match.arg(antialias), dev.cur())

#' @importFrom grDevices postscript
postscript_dev <- wrap(postscript, NULL, dev.cur())

#' @importFrom grDevices svg
svg_dev <- wrap(svg, NULL, dev.cur())

#' @importFrom grDevices tiff
tiff_dev <- wrap(tiff, NULL, dev.cur())

#' @importFrom grDevices xfig
xfig_dev <- wrap(xfig, NULL, dev.cur())


# User-level with_* fns ---------------------------------------------------

#' Graphics devices
#'
#' Temporarily use a graphics device.
#'
#' @template with
#' @param new \code{[named character]}\cr New graphics device
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double-check documentation of this argument.

#' @seealso \code{\link[grDevices]{Devices}}
#' @examples
#' # dimensions are in inches
#' with_pdf(
#' c(file = file.path(tempdir(), "test.pdf"), width = 7, height = 5),
#' plot(runif(5))
#' )
#'
#' # dimensions are in pixels
#' with_png(
#' c(file = file.path(tempdir(), "test.png"), width = 800, height = 600),
#' plot(runif(5))
#' )
#' @aliases with_dev with_device
#' @importFrom grDevices dev.off
#' @export
with_bmp <- with_(bmp_dev, dev.off)

#' @rdname with_bmp
#' @export
with_cairo_pdf <- with_(cairo_pdf_dev, dev.off)

#' @rdname with_bmp
#' @export
with_cairo_ps <- with_(cairo_ps_dev, dev.off)

#' @rdname with_bmp
#' @export
with_jpeg <- with_(jpeg_dev, dev.off)

#' @rdname with_bmp
#' @export
with_pdf <- with_(pdf_dev, dev.off)

#' @rdname with_bmp
#' @export
with_png <- with_(png_dev, dev.off)

#' @rdname with_bmp
#' @export
with_postscript <- with_(postscript_dev, dev.off)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names "with_cairo_ps()" and "with_postscript()" look inconsistent to me.


#' @rdname with_bmp
#' @export
with_svg <- with_(svg_dev, dev.off)

#' @rdname with_bmp
#' @export
with_tiff <- with_(tiff_dev, dev.off)

#' @rdname with_bmp
#' @export
with_xfig <- with_(xfig_dev, dev.off)
97 changes: 97 additions & 0 deletions man/with_bmp.Rd
@@ -0,0 +1,97 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/devices.R
\name{with_bmp}
\alias{with_bmp}
\alias{with_cairo_pdf}
\alias{with_cairo_ps}
\alias{with_dev}
\alias{with_device}
\alias{with_jpeg}
\alias{with_pdf}
\alias{with_png}
\alias{with_postscript}
\alias{with_svg}
\alias{with_tiff}
\alias{with_xfig}
\title{Graphics devices}
\usage{
with_bmp(new, code, width = 480, height = 480, units = "px",
pointsize = 12, bg = "white", res = NA, family = "sans",
restoreConsole = TRUE, type = c("windows", "cairo"),
antialias = c("default", "none", "cleartype", "gray", "subpixel"))

with_cairo_pdf(new, code, width = 7, height = 7, pointsize = 12,
onefile = FALSE, family = "sans", bg = "white",
antialias = c("default", "none", "gray", "subpixel"),
fallback_resolution = 300)

with_cairo_ps(new, code, width = 7, height = 7, pointsize = 12,
onefile = FALSE, family = "sans", bg = "white",
antialias = c("default", "none", "gray", "subpixel"),
fallback_resolution = 300)

with_jpeg(new, code, width = 480, height = 480, units = "px",
pointsize = 12, quality = 75, bg = "white", res = NA,
family = "sans", restoreConsole = TRUE, type = c("windows", "cairo"),
antialias = c("default", "none", "cleartype", "gray", "subpixel"))

with_pdf(new, code, width, height, onefile, family, title, fonts, version,
paper, encoding, bg, fg, pointsize, pagecentre, colormodel, useDingbats,
useKerning, fillOddEven, compress)

with_png(new, code, width = 480, height = 480, units = "px",
pointsize = 12, bg = "white", res = NA, family = "sans",
restoreConsole = TRUE, type = c("windows", "cairo", "cairo-png"),
antialias = c("default", "none", "cleartype", "gray", "subpixel"))

with_postscript(new, code, onefile, family, title, fonts, encoding, bg, fg,
width, height, horizontal, pointsize, paper, pagecentre, print.it, command,
colormodel, useKerning, fillOddEven)

with_svg(new, code, width = 7, height = 7, pointsize = 12,
onefile = FALSE, family = "sans", bg = "white",
antialias = c("default", "none", "gray", "subpixel"))

with_tiff(new, code, width = 480, height = 480, units = "px",
pointsize = 12, compression = c("none", "rle", "lzw", "jpeg", "zip",
"lzw+p", "zip+p"), bg = "white", res = NA, family = "sans",
restoreConsole = TRUE, type = c("windows", "cairo"),
antialias = c("default", "none", "cleartype", "gray", "subpixel"))

with_xfig(new, code, onefile = FALSE, encoding = "none",
paper = "default", horizontal = TRUE, width = 0, height = 0,
family = "Helvetica", pointsize = 12, bg = "transparent",
fg = "black", pagecentre = TRUE, defaultfont = FALSE,
textspecial = FALSE)
}
\arguments{
\item{new}{\code{[named character]}\cr New graphics device}

\item{code}{\code{[any]}\cr Code to execute in the temporary environment}
}
\value{
\code{[any]}\cr The results of the evaluation of the \code{code}
argument.
}
\description{
Temporarily use a graphics device.
}
\examples{
# dimensions are in inches
with_pdf(
c(file = file.path(tempdir(), "test.pdf"), width = 7, height = 5),
plot(runif(5))
)

# dimensions are in pixels
with_png(
c(file = file.path(tempdir(), "test.png"), width = 800, height = 600),
plot(runif(5))
)
}
\seealso{
\code{\link{withr}} for examples

\code{\link[grDevices]{Devices}}
}