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
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
Related
I installed the package packrat at some point, used it perhaps once and moved on with my life.
However, despite not having loaded it in months, it remains a nuisance to my regular R usage.
Seemingly at random, my R session within RStudio will fail with errors at certain operations, especially package installation. Here's the most recent error message (after running parallel::makeCluster(parallel::detectCores()):
Error in file(filename, "r", encoding = encoding) : cannot open the
connection
Calls: source -> file
In addition: Warning message:
In
file(filename, "r", encoding = encoding) : cannot open file
'packrat/init.R': No such file or directory
Execution halted
I checked all of the folders on .libPaths() and I don't even have packrat installed anymore. Why on earth is R still trying to carry out packrat operations? And how can I stop this?
My duct-tape solution so far is to simply close and reopen RStudio, which works like a charm for package installation issues.
However, I cannot seem to get around this for makeCluster(detectCores()) within just one .R script I've got. It works perfectly fine in another script for another project.
Background:
sessionInfo()
# R version 3.2.2 (2015-08-14)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 14.04.2 LTS
# locale:
# [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
# [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
# [7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8
# [10] LC_TELEPHONE=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8
# attached base packages:
# [1] parallel stats graphics grDevices utils datasets methods base
# other attached packages:
# [1] doParallel_1.0.8 iterators_1.0.7 foreach_1.4.2 geosphere_1.4-3 xlsx_0.5.7 xlsxjars_0.6.1
# [7] rJava_0.9-6 xtable_1.7-4 sandwich_2.3-3 texreg_1.35 maptools_0.8-36 sp_1.1-1
# [13] ggmap_2.5.2 ggplot2_1.0.1 data.table_1.9.5
# loaded via a namespace (and not attached):
# [1] Rcpp_0.11.6 plyr_1.8.3 tools_3.2.2 digest_0.6.8 gtable_0.1.2
# [6] lattice_0.20-33 png_0.1-7 mapproj_1.2-4 proto_0.3-10 stringr_1.0.0
# [11] RgoogleMaps_1.2.0.7 maps_2.3-11 grid_3.2.2 jpeg_0.1-8 foreign_0.8-66
# [16] RJSONIO_1.3-0 reshape2_1.4.1 magrittr_1.5 codetools_0.2-11 scales_0.2.5
# [21] MASS_7.3-43 colorspace_1.2-6 stringi_0.5-9003 munsell_0.4.2 chron_2.3-47
# [26] rjson_0.2.15 zoo_1.7-12
Update 1:
Installing packrat had no effect. Running packrat::init() resulted in an error before finishing; nothing changed.
Update 2:
I've isolated the problem by identifying that it's the working directory that's causing the issues. What in the working directory I'm using might be causing the problems? Some residual file from having run packrat previously in this directory?
Through further trial and error given the prods of #BondedDust I finally appear to have solved the issue. Having previously tried to use packrat in the particular working directory in which I was working appears to have left some vestiges despite later uninstalling packrat.
In particular, packrat edits your local .Rprofile (original credit due to #zerweck and #snaut), which is source()d on R startup in that directory.
If you use the .Rprofile to store some local configurations, you should edit the file and remove the packrat lines (or any you don't recognize); otherwise, you should just delete that file to restore your project to working as expected.
Check your HOME directory for an unintentional .Rprofile.
Packrat may have put this here if you tried to packrat::init() in HOME.
install.package() with packrat looks for .Rprofile when run. The behavior I've observed has it prioritizing the HOME .Rprofile over the getwd() one, causing the
cannot open file 'packrat/init.R': No such file or directory
I'm going through the vignette example in the openxlsx package in R Statistics, but I can't save the workbook and I can't figure out why. The code below is a modified example from the vignette, just to keep it simple.
setwd("c:/users/kenneth/documents/r/2014-04-29_openxlsx")
require(openxlsx)
require(ggplot2)
wb <- createWorkbook()
addWorksheet(wb, sheetName = "Motor Trend Car Road Tests", gridLines = FALSE)
addWorksheet(wb, sheetName = "Iris")
addWorksheet(wb, sheetName = "Conditional Formatting")
saveWorkbook(wb=wb, file="basics.xlsx", overwrite=TRUE) ## save to working directory
The error message I get is:
Warning message:
running command '"zip" -r1 "c:/users/kenneth/documents/r/2014-04-29_openxlsx/basics.xlsx" "[Content_Types].xml" "_rels" "docProps" "docProps/app.xml" "docProps/core.xml" "xl" "xl/_rels" "xl/_rels/workbook.xml.rels" "xl/charts" "xl/drawings" "xl/drawings/_rels" "xl/printerSettings" "xl/printerSettings/printerSettings1.bin" "xl/printerSettings/printerSettings2.bin" "xl/printerSettings/printerSettings3.bin" "xl/styles.xml" "xl/tables" "xl/tables/_rels" "xl/theme" "xl/theme/theme1.xml" "xl/workbook.xml" "xl/worksheets" "xl/worksheets/_rels" "xl/worksheets/_rels/sheet1.xml.rels" "xl/worksheets/_rels/sheet2.xml.rels" "xl/worksheets/_rels/sheet3.xml.rels" "xl/worksheets/sheet1.xml" "xl/worksheets/sheet2.xml" "xl/worksheets/sheet3.xml" ' had status 127
This is my sessioninfo:
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Danish_Denmark.1252 LC_CTYPE=Danish_Denmark.1252
[3] LC_MONETARY=Danish_Denmark.1252 LC_NUMERIC=C
[5] LC_TIME=Danish_Denmark.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_0.9.3.1 openxlsx_1.0.3
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 digest_0.6.4 grid_3.1.0 gtable_0.1.2
[5] MASS_7.3-31 munsell_0.4.2 plyr_1.8.1 proto_0.3-10
[9] Rcpp_0.11.1 reshape2_1.4 scales_0.2.4 stringr_0.6.2
[13] tools_3.1.0
I have updated to the most recent version of R, devtools and Rtools.
I think the issue here is that R can not find the zip application that comes with Rtools.
You can check if the Rtools bin directory is in the system PATH from R with the command
shell("PATH")
The output should look something like this (depending on where you installed Rtools):
PATH=c:\Rtools\bin;c:\Rtools\gcc-4.6.3\bin;C:\Windows\system32;C:\... etc
If the Rtools\bin directory doesn't appear anywhere in the string add it in.
Instructions to edit system path can be found here:
http://www.java.com/en/download/help/path.xml")
If this doesn't work email me and I'll help you out further (email is in the vignette)
I have a data frame "df1" of the following kind:
set.seed(2)
df1 = data.frame(Var1=rep(c('a','b','c','d'),3),
Var2=rep(c('aa','bb','cc'),each=4),
value=rnorm(12))
I try to create this plot:
ggplot(df1) + geom_bar(aes(x=Var2,y=value),stat="identity") + facet_wrap(~Var1)
and I get the following error message that I don't understand:
formal classes cannot be used without the methods package
What's wrong ? I can't find a package called methods by the way.
Here is my sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] fr_CH.UTF-8/fr_CH.UTF-8/fr_CH.UTF-8/C/fr_CH.UTF-8/fr_CH.UTF-8
attached base packages:
[1] utils graphics methods stats base
other attached packages:
[1] agricolae_1.1-4 reshape2_1.2.2 lattice_0.20-15 ggplot2_0.9.3.1 data.table_1.8.8
loaded via a namespace (and not attached):
[1] colorspace_1.2-2 datasets_3.0.1 dichromat_2.0-0 digest_0.6.3 grDevices_3.0.1 grid_3.0.1 gtable_0.1.2
[8] labeling_0.1 lme4_0.999999-2 MASS_7.3-26 Matrix_1.0-12 munsell_0.4 nlme_3.1-109 plyr_1.8
[15] proto_0.3-10 RColorBrewer_1.0-5 scales_0.2.3 stats4_3.0.1 stringr_0.6.2 tools_3.0.1
Thank you
I encountered the same error message and found a solution but in a different context.
The error happened while running an R script on a cluster. Interestingly the same script worked fine on a laptop (during testing with a subset of the data).
The solution in my context was to import the package with which the data was preprocessed. What I mean is that I previously saved an RDS file created using the package "Seurat". When reading the RDS file in a new session the package "Seurat" would load automatically when ran on a local machine. However on the cluster I had to specifically load the package with the library() command, before reading the RDS file. This is what solved the problem and stopped the obfuscated error message.
After successfully creating a graph with this code:
qplot(ethcat3, Percent, data=g_02_1b_pov, geom="bar", stat="identity")
I would like to save the graph as a png, pdf, or jpeg file. I used the following code:
ggsave(file="test.png")
Instead of outputting the graph, I receive the following (error) message:
Saving 7.82 x 3.75 in image
Error in eval(expr, envir, enclos) : attempt to apply non-function
and no file is saved. (Instead of png, I also tried the extensions pdf, and jpeg).
I found in a forum that I should use the command: dev_mode() and has_devel()
the output of dev_mode is:
> dev_mode()
Dev mode: OFF
and
> has_devel()
/Library/Frameworks/R.framework/Resources/bin/R --vanilla CMD SHLIB foo.c
sh: make: command not found
Error: Command failed (1)
However, I don't know what to do with this feedback?
Can anyone help me with this problem? I think I used the same approach in the past on a PC and it worked without a problem. I'm wondering if it is connected to my installation on a mac?
Thanks a lot!
Addition: #DWin asked for the sessionInfo() output:
d> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] C/en_US.UTF-8/C/C/C/C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] devtools_0.8 reshape2_1.2.1 plyr_1.7.1 ggplot2_0.9.2.1
loaded via a namespace (and not attached):
[1] MASS_7.3-18 RColorBrewer_1.0-5 RCurl_1.95-3 colorspace_1.2-0
[5] dichromat_1.2-4 digest_0.5.2 evaluate_0.4.2 grid_2.15.1
[9] gtable_0.1.1 httr_0.2 labeling_0.1 memoise_0.1
[13] munsell_0.4 parallel_2.15.1 proto_0.3-9.2 scales_0.2.2
[17] stringr_0.6.1 tools_2.15.1 whisker_0.1
After running several models I need to run a system() command on my R script to shutdown my EC2 instance, but when I get to that point I get:
cannot popen 'ls', probable reason 'Cannot allocate memory'
Note: for this question I even tried ls which did not work
The flow of my script is the following
Load Model (about 2GB)
Mine documents and write to a MySQL database
The above steps are repeated around 20 times with different models with an average size of 2GB each
Terminate the instance
At this point is when I need to call system("sudo shutdown -h now") and nothing happens, but when I try system("sudo shutdown -h now",intern=TRUE) I get the allocation error.
I tried rm() for all my objects just before calling the shutdown, but the same error persists.
Here is some data on my system which is a large EC2 Ubuntu instance
R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] splines stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] RTextTools_1.3.9 tau_0.0-15 glmnet_1.8 Matrix_1.0-6
[5] lattice_0.20-10 maxent_1.3.2 Rcpp_0.9.13 caTools_1.13
[9] bitops_1.0-4.1 ipred_0.8-13 prodlim_1.3.2 KernSmooth_2.23-8
[13] survival_2.36-14 mlbench_2.1-1 MASS_7.3-21 rpart_3.1-54
[17] e1071_1.6-1 class_7.3-4 tm_0.5-7.3 nnet_7.3-4
[21] tree_1.0-31 randomForest_4.6-6 SparseM_0.96 RMySQL_0.9-3
[25] ggplot2_0.9.1 DBI_0.2-5
loaded via a namespace (and not attached):
[1] colorspace_1.1-2 dichromat_1.2-4 digest_0.5.2 grid_2.15.1
[5] labeling_0.2 memoise_0.1 munsell_0.3 plyr_1.7.1
[9] proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.1 scales_0.2.1
[13] slam_0.1-25 stringr_0.6.1
gc() returns
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1143171 61.1 5234604 279.6 5268036 281.4
Vcells 1055057 8.1 465891772 3554.5 767962930 5859.1
I noticed that if I run just 1 model instead of the 20 it works fine, so it might be that memory is not getting free after each run although I did rm() the used objects
I also noticed that if I close R and restart it and then call system() it works. If there is a way to restart R within R then maybe I can add that to my script.sh flow.
Which would be the appropriate way of cleaning all of my objects and letting the memory free for each loop so when I need to call the system() commands there is no memory issue?
Any tip in the right direction will be much appreciated!
Thanks
I'm just posting this because it's too long to fit in the comments. Since you haven't included any code, it's pretty hard to give advice. But, here is some code that maybe you can think about.
wd <- getwd()
assign('.First', function(x) {
require('plyr') #and whatever other packages you're using
file.remove(".RData") #already been loaded
rm(".Last", pos=.GlobalEnv) #otherwise won't be able to quit R without it restarting
setwd(wd)
}, pos=.GlobalEnv)
assign(".Last", function() {
system("R --no-site-file --no-init-file --quiet")
}, pos=.GlobalEnv)
save.image() #or only save the things you want to be reloaded.
q("no")
The idea is that you save the things you need in a file called .RData. You create a .Last function that will be run when you quit R. The .Last function will start a new session of R. And you create a .First function that will be run as soon as R is restarted. The .First function will load packages you need and clean up.
Now, you can quit R and it will restart loading the things you need.
(q("no") means don't save, but you already saved everything you need in .RData which will be loaded when it restarts)