[R-pkg-devel] Issue with use of par() in package
Jan van der Laan
rhe|p @end|ng |rom eoo@@dd@@n|
Thu Aug 21 10:45:04 CEST 2025
Someone asked for the response I got from CRAN. I included it below:
---
[... another issue where I forgot a \value{} in the documentation of one
of the functions]
Please make sure that you do not change the user's options, par or
working directory. If you really have to do so within functions, please
ensure with an *immediate* call of on.exit() that the settings are reset
when the function is exited.
e.g.:
...
oldpar <- par(no.readonly = TRUE) # code line i
on.exit(par(oldpar)) # code line i + 1
...
par(mfrow=c(2,2)) # somewhere after
...
e.g.: -> R/tgp.R
If you're not familiar with the function, please check ?on.exit. This
function makes it possible to restore options before exiting a function
even if the function breaks. Therefore it needs to be called immediately
after the option change within a function.
For more details:
<https://contributor.r-project.org/cran-cookbook/code_issues.html#change-of-options-graphical-parameters-and-working-directory>
Please fix and resubmit.
---
I understand why this is asked and I also know how to handle this in
usual cases. I am not sure how to handle this in the specific case here.
Perhaps I missed some alternative way of handling this.
Best,
Jan
On 8/20/25 21:31, Jan van der Laan wrote:
> I recently submitted a package to CRAN that creates a device that
> outputs into the terminal (for terminals that support the Terminal
> Graphics Protocol (and hopefully in the future also terminals that
> support sixel)) [1].
>
> It introduces a 'tgp' function that does the following:
>
> 1. It opens a ragg::agg_capture device.
>
> 2. When term_col = TRUE: sets the background and foreground colors of
> the device to those of the terminal (when these can be detected).
>
> 3. Every time the ragg:agg_capture device is updated: updates the plot
> in the terminal.
>
> Point 2 raises an issue from CRAN as par() changes some properties and
> these are not set back at the and of the function call. This is
> intentional, as otherwise setting them would be useless; it is the
> intention that the colors of subsequent plot calls are changed. I am
> not sure how I can set the colors of the device to those in the terminal
> (as requested by the user) without violating this rule.
>
> Are there alternative ways of helping the user to set the colors of the
> device without violating this issue?
>
> Could this be an exception to the rule as the argument intentionally
> changes par()? This could be made more explicit by changing the default
> value to FALSE. I guess this question is better asked at the CRAN
> maintainers, but perhaps there are other package that do something
> similar that I might look at.
>
> Instead of having this functionality in the tgp() function, I could
> create a 'set_figure_colours_to_terminal()` function (perhaps with a
> corresponding reset function). However, this function would run into the
> same issue.
>
> I appreciate any suggestions/help that yoy could give.
>
> Best,
> Jan
>
>
>
>
> [1] The current version of the code can be found here: https://
> codeberg.org/djvanderlaan/terminalgraphics/src/branch/dev
>
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
More information about the R-package-devel
mailing list