[R-pkg-devel] Writing to files without altering working directory in R package
I find the idea of using setwd in examples disturbing, because I try to avoid using setwd at all as a way to delegate my choice of working directory to the operating system actions taken when starting R. In other words, I recommend to beginners that they start R in their working directory and leave it there all the time, using relative paths as much as possible. Since examples are likely to be run in the middle of an analysis in order to develop the next step in the analysis, leaving my wd elsewhere would be counterproductive. Having a leftover variable that tells me where the data were written is simpler than using setwd to and from the temp directory. More hand holding than that belongs in a tutorial or vignette. Note that if you wrap the example up in a vignette then changing to the temp directory seems fair to me (if CRAN allows it) because the context is more like a complete, self-contained analysis.
On May 7, 2019 11:32:49 AM PDT, Henrik Bengtsson <henrik.bengtsson at gmail.com> wrote:
I agree that setwd(), tempdir(), etc. clutter up examples. At the same time, I respect the CRAN policy - it's conservative approach has helped us avoid a wild-west working environment. One approach that might help bring some standardization, instead of each package developer rolls their own, and avoid having to have that tempdir code in there is to have example() handle this. For example, add a workdir=tempdir() argument to example() and have example() change to that directory temporarily, and report on the working directory at the end.
example(foo)
...
Any output files produced by this example may be found in the
temporary working directory '/tmp/alice/Rtmp4faDCv'.
This could be a message or a warning. One could also imagine using
workdir=getOption("example.workdir", tempdir()) as the default. If
properly done, this would allow the user to set
options(example.workdir = quote(getwd())) if they'd like to write to
the current directory.
What it does not solve is when a user cut'n'paste code, but maybe that
could be considered acceptable because in that case the user is not
running/source code, but calling it themself.
My $.02
/Henrik
These messages can be skipped if called with example(...,
workdir=getwd()).
On Tue, May 7, 2019 at 10:57 AM Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
On 06/05/2019 12:16 p.m., Jim Hester wrote:
For what it's worth, the recommendation to use `tempfile()` is very confusing to R users. Often users (particularly new users) jump directly to examples when reading documentation and when you have these more complicated examples they do not realize they can just use a simple string literal. See https://github.com/tidyverse/readr/issues/635 for an issue
where
multiple users explicitly request examples which do _not_ use `tempfile()`.
I think beginners rarely like the help pages, and that's really to be expected: help pages need to be complete and correct, and beginners really need a small subset of possibilities. Beginners need
tutorials.
In the Github issue, you mentioned pollution of the working
directory,
but I think a bigger problem is destruction of user data, e.g. if a
user
has a file "wine.csv", and the example writes to that file. So you need to do something to protect users. I think making
examples a
little complicated by using setwd(tempdir()) with a literal filename,
or
writing to tempfile() is worth it. Duncan Murdoch
On Fri, May 3, 2019 at 7:59 PM Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
On 03/05/2019 6:33 p.m., Jarrett Phillips wrote:
Hello, My R package has a function with an argument to specify whether
numerical
results should be outputted to a CSV file. CRAN policy stipulates verbatim that Packages should not write in the user?s home filespace (including clipboards), nor anywhere else on the file system apart from the
R
session?s temporary directory (or during installation in the
location
pointed to by TMPDIR: and such usage should be cleaned up).
Installing into
the system?s R installation (e.g., scripts to its bin directory)
is not
allowed. I know I should use tempdir() within my package function, but
I've not seen
any examples on how this is best done within existing R packages. Within my package documentation examples for my function, I have
the lines:
\dontshow{.oldwd <- setwd(tempdir())}
... some R code ...
\dontshow{setwd(.oldwd)}
but I have been informed that this is not the accepted way.
Any ideas from the community on how do do this properly?
Use the tempfile() function to generate a filename in the
temporary
directory. You might want to use the "pattern" or "fileext"
arguments,
but don't change the "tmpdir" argument. Then write to that file. For example, filename <- tempfile(fileext = ".csv") write.csv(df, filename) It's a good idea to clean up afterwards using unlink(filename)
______________________________________________ R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
______________________________________________ R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
______________________________________________ R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Sent from my phone. Please excuse my brevity.