Comments (9)
Does with_png <- with_(png, dev.off)
work for you?
The other two suggestions seem to require some global stack that stores the old
values.
from withr.
@krlmlr
This definition of with_png
gives me an error:
with_png <- with_(png, dev.off)
with_png("test.png", plot(1:10))
## Error in match.arg(antialias, aa.win) : 'arg' must be of length 1
The problem occurs on the line list(bitmap.aa.win = match.arg(antialias, aa.win))
, where aa.win
is a constant in the grDevices
package. The problem is that if the first argument to match.arg
isn't a formal argument of a function, then match.arg
wants it to be a single string rather than a character vector.
A similar function for PDFs gives a different error.
with_pdf <- with_(pdf, dev.off)
with_pdf("test.pdf", plot(1:10))
## Error in if (which == 1) stop("cannot shut down device 1 (the null device)") :
## argument is of length zero
The plot does get written to file, but R is trying to shut down the wrong device. If I call dev.off()
after the error, then the plot file is correctly generated.
from withr.
There was some previous discussion about the device functions at #16 (comment)
from withr.
I forgot. Perhaps:
withr_ <- function(set, reset = set, transform = identity, envir = parent.frame()) { ... }
?
Then:
with_png <- with_(png, dev.off, function(x) dev.cur())
from withr.
Yeah I like that approach
from withr.
@richierocks: Once #27 is merged, devices will be easy, e.g., for the pdf() device:
pdf_dev <- wrap(pdf, NULL, dev.cur())
with_pdf <- with_(pdf_dev, dev.off)
Would you like to contribute?
A similar definition of with_png() doesn't show the error you mentioned before (#29 (comment)), but I'm on Ubuntu, and things might be different on your system.
from withr.
@krlmlr @jimhester OK, I'm happy with the with_device
functions now, so feel free to merge at your leisure.
Still not quite sure how to go about the with_connection
/with_db_connection
functions.
from withr.
Feedback to your queries
Could you please update your roxygen2 installation?
Done.
The names "with_cairo_ps()" and "with_postscript()" look inconsistent to me.
Agreed, but there is a reason. cairo_ps
and postscript
are the names of the devices in the grDevices
package, so I'm consistently following that package's inconsistencies.
Please double-check documentation of this argument.
I'd copied and pasted from the with_locale
function. Now updated to use the template suggested by Jim.
antialias <- match.arg(antialias) for jpeg_dev() and png_dev(), why is this necessary?
There are different implementations of grDevices::png
and grDevices::jpeg
depending upon your OS. On Windows, the functions use a non-standard way of matching the possible values of antialias
, and jpeg_dev
and png_dev
throw an error unless you call match.arg
first.
grDevices has man/unix and man/windows folders, it is possible for
external packages to do the same thing?
Not as far as I know, but I've asked the roxygen people.
See Issue r-lib/roxygen2#438
we have been avoiding @importFrom declarations
Fixed.
Another problem
The documentation for the useDingbats
argument to grDevices::pdf
differs between Unix and Windows: the Unix version has an extra sentence
See the βNoteβ for a possible fix for some viewers.
When I build withr
on Windows, instead of that sentence I get
#ifdef unix not active
and lots of warnings because R can't properly parse the rest of the Rd file.
The same issue occurs with the command
argument to grDevices::postscript
.
from withr.
I think we have included all of these ideas now, thanks again!
from withr.
Related Issues (20)
- defer() run immediately when used inside Rmd HOT 1
- with_language() not working in RStudio IDE for Windows 10 HOT 2
- on.exit calls HOT 11
- Something weird with withr 2.5.0 HOT 1
- Problem with `with_db_connection`
- with_trace()? HOT 1
- Should the parameter `tmpdir` be superseded by a new `tempdir` parameter? HOT 2
- local_assign()? HOT 2
- Provide more control over `defer_parent()` messages
- `withr::local_`: error `could not find function` when `withr` is not attached
- How to undo call to `local_dir()` or `local_options()` in global environment? HOT 2
- unclear documentation for lines= argument of local_tempfile
- Support UTF-8 content in local_tempfile(lines = ) HOT 2
- Provide `local_bindings()`? HOT 3
- with_language() fails to reset the cache on Alpine Linux HOT 5
- `local_tempfile()` should use a binary connection for `lines`
- Add short vignette on writing your own withr-like functions
- LIGHT MODE: Move 'stats' from Imports to Suggests HOT 8
- Working directory inside with_dir/local_dir does not match provided new directory
- with_s3_method HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from withr.