Skip to content
Prev 2221 / 12125 Next

[R-pkg-devel] nparACT package: "working directory was changed to...resetting"

On 19/12/2017 10:27 AM, Blume Christine wrote:
This is probably a bad idea.  You're creating a subdirectory in the 
user's current working directory; you might not have file system 
permission to do that, and you probably don't have authorization from 
the user to do that.  I don't remember if CRAN checks for this, but I 
wouldn't be surprised if they do, or will in the future.

Since you need to write a file, you want to be sure that it's going to a 
place that is harmless and will succeed.  That's what tempdir() is for. 
At the end of your session it will be cleaned up, so you won't leave 
behind junk for the user.

So I would set newdir using

newdir <- file.path(tempdir(), name)

The other change I would make is to unconditionally use 
dir.create(newdir, showWarnings = FALSE).  You can be almost 100% sure 
that any error creating newdir will be just that it already exists, and 
since it's just somewhere in tempdir(), you don't really care about 
that.  This will simplify your example quite a bit.

Putting these two and my earlier suggestion together, your example becomes

data(sleepstudy)

name <- "sleepstudy_example"

newdir <- file.path(tempdir(),name)

dir.create(newdir, showWarnings = FALSE)

olddir <- setwd(newdir)

write.table(sleepstudy, file = "sleepstudy.txt", row.names=FALSE, 
col.names = FALSE)

r <- nparACT_flex_loop(newdir, SR = 4/60, minutes = 435)

setwd(olddir)

Duncan Murdoch