Trouble with packrat corrupting R functioning

2019-06-19 22:54发布

问题:

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?

回答1:

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.



回答2:

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