Giter Site home page Giter Site logo

More with_* function ideas about withr HOT 9 CLOSED

r-lib avatar r-lib commented on August 25, 2024
More with_* function ideas

from withr.

Comments (9)

krlmlr avatar krlmlr commented on August 25, 2024

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.

richierocks avatar richierocks commented on August 25, 2024

@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.

jimhester avatar jimhester commented on August 25, 2024

There was some previous discussion about the device functions at #16 (comment)

from withr.

krlmlr avatar krlmlr commented on August 25, 2024

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.

jimhester avatar jimhester commented on August 25, 2024

Yeah I like that approach πŸ‘

from withr.

krlmlr avatar krlmlr commented on August 25, 2024

@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.

richierocks avatar richierocks commented on August 25, 2024

@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.

richierocks avatar richierocks commented on August 25, 2024

@krlmlr @jimhester

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.

jimhester avatar jimhester commented on August 25, 2024

I think we have included all of these ideas now, thanks again!

from withr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.