[R-pkg-devel] Writing to files without altering working directory in R package

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Mon May 6 18:28:49 CEST 2019


You cannot go littering the user directories with sample files. Such examples would have to be dontrun, but in order to make some of the example runnable as is you would also need examples that use system.file paradigm. If you store the temporary file name in a variable then the user can retrieve the contents of that file briefly after running the example. Such practice of separating the call to tempfile from your function call also makes it a little clearer that the file name can be created using other methods than via system.file.

On May 6, 2019 9:16:42 AM PDT, Jim Hester <james.f.hester using gmail.com> 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()`.
>
>On Fri, May 3, 2019 at 7:59 PM Duncan Murdoch
><murdoch.duncan using 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 using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
>______________________________________________
>R-package-devel using r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-package-devel

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-package-devel mailing list