13

I have looked extensively to find the answer to this before asking and could not find the answer, but if its out there please point me to it. Every time I start R studio I have packages that load automatically like:

Loading required package: RMySQL
Loading required package: DBI
Loading required package: cocor
Loading required package: RMySQL

I would like these packages to stop loading automatically every time I start R studio, and have tried uninstalling and reinstalling R and R studio in addition to the following:

detach("package:RMySQL",unload=TRUE)

For all three of these packages and it doesn't work. Please help! Thank you.

sapply(ls(), function(x) class(get(x)))
named list()
smci
  • 32,567
  • 20
  • 113
  • 146
costebk08
  • 1,299
  • 4
  • 17
  • 42

3 Answers3

9

Look at this post and you probably want to find your .RProfile file. Look at ?Startup (note capital S) for more help. The .RProfile is should be under the /etc/ folder underneath wherever R is installed on your machine. Per the post above, a fast way to find its location would be to run the following:

candidates <- c( Sys.getenv("R_PROFILE"),
                 file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site"),
                 Sys.getenv("R_PROFILE_USER"),
                 file.path(getwd(), ".Rprofile") )

Filter(file.exists, candidates)
Community
  • 1
  • 1
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • 3
    you could even add `file.show(Filter(file.exists, candidates))` – rawr Jul 01 '15 at 16:49
  • That is useful; however, I still don't understand how to change this setting once I get to this point. Any further suggestions? – costebk08 Jul 01 '15 at 19:58
  • Can you post the output of your `.Rprofile` in your question? – JasonAizkalns Jul 01 '15 at 20:08
  • @costebk08 please OPEN that file in a text editor and paste its contents in your question – JasonAizkalns Jul 02 '15 at 10:43
  • `# Things you might want to change # options(papersize="a4") # options(editor="notepad") # options(pager="internal") # set the default help type # options(help_type="text") options(help_type="html") # set a site library # .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library") # set a CRAN mirror # local({r <- getOption("repos") # r["CRAN"] <- "http://my.local.cran" # options(repos=r)}) # Give a fortune cookie, but only to interactive sessions # (This would need the fortunes package to be installed.) # if (interactive()) # fortunes::fortune()` – costebk08 Jul 02 '15 at 14:23
5

From the comments you've posted, it looks like you're running Windows. The location for a user .RProfile can be shown using:

(my_rprofile <- file.path(Sys.getenv("R_USER"), ".RProfile"))

You can then check whether that file exists using:

file.exists(my_rprofile)

and if this returns TRUE, open it for editing using:

file.edit(my_rprofile)

If the file isn't there, try:

file.exists(".RProfile")

and if TRUE:

file.edit(".RProfile")

If you execute this command within RStudio, you should get a window open with the current contents of your .RProfile. I suspect it includes something along the lines of:

library("RMySQL")

which you then need to remove as appropriate before saving.

Other things to check:

Sys.getenv("R_DEFAULT_PACKAGES")
# should be blank
.First
# should give an error that .First not found

If .First is set and you don't have a .RProfile file, you might have it defined in file.path(Sys.getenv("R_USER"), ".RData") and it would be worth renaming that file (or disabling restoring .RData in the RStudio options.

Even if .First is undefined, I'd still try loading R/RStudio without restoring from .RData since it may be that you're restoring some S4 objects which depend on those packages.

Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
  • When I run `file.exists(my_rprofile)` I get `FALSE`; thus, when I run `file.edit(my_rprofile)` I just get a blank page. Any thoughts? – costebk08 Jul 22 '15 at 18:29
  • Thank you for the additional info, but what would renaming it accomplish? What would disabling it accomplish, and how would I go about doing that? I guess I am still confused how any of this affects which packages load automatically. – costebk08 Jul 22 '15 at 19:38
  • 1
    When R loads, by default (in both base R and RStudio) it loads a workspace from a file called `.RData`. If there exists within that a function called `.First` then it gets called. If that function includes data to load packages, they will be loaded. It may well be that you have no `.RData` file there or that if you do it doesn't define `.First`, but if it does that might be the explanation. – Nick Kennedy Jul 22 '15 at 19:41
  • 1
    @costebk08 I wonder if the actual issue is that you're restoring S4 objects from .RData that are defined in those packages. Both `cocor` and `DBI` use S4. Can you load R and type in `sapply(ls(), function(x) class(get(x)))` and put the output in your **question** since comments aren't really set up for that kind of output? – Nick Kennedy Jul 22 '15 at 19:53
  • I just added this now – costebk08 Jul 22 '15 at 20:41
  • @costebk08 so it's not then. What about the output of `sessionInfo()`? – Nick Kennedy Jul 22 '15 at 20:50
  • Also, does this happen using RGui or just with RStudio? – Nick Kennedy Jul 22 '15 at 20:59
  • Another thought. RStudio will load `.RProfile` from the current profile directory. Can you check for a `.RProfile` in the current working directory per the above? What happens if you switch to a new profile? – Nick Kennedy Jul 22 '15 at 21:47
  • `other attached packages: [1] RMySQL_0.10.3 DBI_0.3.1 cocor_1.1-1` This looks like the relevant output when I run `sessionInfo()` I am not sure how to switch to a different `.RProfile` I apologize – costebk08 Jul 22 '15 at 23:27
  • In RStudio choose a new project from the top right hand corner menu. – Nick Kennedy Jul 22 '15 at 23:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84024/discussion-between-costebk08-and-nick-kennedy). – costebk08 Jul 22 '15 at 23:46
  • There is also `getOption("defaultPackages")`. – Marek Jul 28 '15 at 19:43
2

The way I've dealt with this problem in the past is to simply create a new folder somewhere on your machine to be used as an alternate R library folder and move the respective package folders from your default library to that folder. Basically when R searches for these packages, it won't find it since R will not know where you moved them to, unless you tell R where to search for. Follow the steps below to do this:

  1. Create a new folder on your PC to be used as a secondary R library folder. For example I have a folder named secondRlibrary in my windows documents with the following path("C:\Users\myusername\Documents\secondRlibrary")
  2. Go to your R library folder where the packages are installed. On my windows PC, this is in "C:\Users\myusername\Documents\R\win-library\3.0.2". They sometimes can also be found in the following folders:("C:\Program Files\R\R-3.0.2\library","C:\Program Files(x86)\R\R-3.0.2\library"), depending on the R version you're running.(I'm running version 3.0.2)
  3. Find and Move(Cut & paste) the folders and all of it's contents for the respective packages to the new folder. The folders usually have the same names as the R package names. That way when R starts. Make sure to remove them from all the folders I listed above to the new folder you just created.
  4. If you start R, the packages will not load automatically since they will not be found in R's default library location(and R cannot locate them automatically unless you tell it to).
  5. If you want to load the packages individually, use the library function with the lib.loc argument to specify the path to the library containing that package(in this case, the new folder you just created). For example if I wanted to load the RMySQL package, I would use the following code

library("package:RMySQL",lib.loc = "C:/Users/myusername/Documents/secondRlibrary")

  1. Alternatively, if you don't want to use the lib.loc argument in the library function above, you can run this line at the start of your R scripts, that is if you need to use these packages in those R scripts: .libPaths(c(.libPaths(),"C:/Users/myusername/Documents/secondRlibrary")).

Then you can run the library function without the lib.loc argument. The line above will add the newly created folder to R's default list of folders for finding packages.

FelixNNelson
  • 161
  • 4