Question

I am using tempdir function to create a temporary directory where I extract .zip files with unzip function. In connection with this, I thought it's good to remove the temporary directories to avoid filling my computer up with scrap data. For this purpose I have been using unlink function. Using unlink function before ? or plot functions causes an error on my computer (OS X Mavericks). I have not tested whether this is the case for other operating systems. I did some googling and one theory for similar error message was that this problem could be related to encrypted hard disc. My hard disc is encrypted, so that could match. But I do not understand why should that matter as plot or ? functions should not be related to unlink operation in my mind. Here is an example:

location <- tempdir()
?plot
#works

unlink(location, recursive = TRUE)
?plot
# Error in file(out, "wt") : cannot open the connection

Plotting works with a warning in R and does not work in R Studio:

plot(1:10)
# Warning message:
# In file(out, "wt") :
# cannot open file #'/var/folders/wh/y62s7qxn29s2lvlx1nh9nxpr0000gq/T//RtmpdYlFVc/Rhttpd175551e9e35fa': No such file or # directory

This seems to work in R studio, but not in R:

graphics.off()
?plot
# works again
plot(1:10)
# this works now too

What is going on here? If the problem is related to my encrypted hard disc, is there a neater way extracting .zip files without generating scrap data?

PS. Here is my session information (I had the same problem with R version 3.0.2):

sessionInfo()
R version 3.0.3 (2014-03-06)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grDevices datasets  splines   grid      utils     graphics  stats     methods   base     

other attached packages:
 [1] devtools_1.4.1    roxygen2_3.0.0    gridExtra_0.9.1   data.table_1.8.10 xlsx_0.5.5        xlsxjars_0.5.0    rJava_0.9-6      
 [8] reshape2_1.2.2    ggplot2_0.9.3.1   plyr_1.8          Hmisc_3.13-0      Formula_1.1-1     survival_2.37-7   lattice_0.20-24  
[15] cluster_1.14.4   

loaded via a namespace (and not attached):
 [1] brew_1.0-6         codetools_0.2-8    colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4       evaluate_0.5.1    
 [7] gtable_0.1.2       httr_0.2           labeling_0.2       MASS_7.3-29        memoise_0.1        munsell_0.4.2     
[13] parallel_3.0.3     proto_0.3-10       RColorBrewer_1.0-5 RCurl_1.95-4.1     scales_0.2.3       stringr_0.6.2     
[19] tools_3.0.3        whisker_0.3-2     
Was it helpful?

Solution

tempdir() is R's session-wide temporary working directory, so by unlinking it you've removed the location that R uses for all kinds of temporary files. You want tempfile(). The return value is a temporary path at which you could write a file or create a directory, or...

mydir <- tempfile()
basename(mydir) %in% dir(tempdir())   # FALSE
if (!dir.create(mydir))
    stop("failed to create my temporary directory")
basename(mydir) %in% dir(tempdir())   # TRUE
## ...
unlink(mydir, recursive=TRUE)
basename(mydir) %in% dir(tempdir())   # FALSE   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top