Where is the R default path for MAC - r

I have entered some codes on R in a MAC and before closing R, it asks "Save workspace image?" and I clicked "Save", thinking it would save my codes. But I cannot find where is my codes? So where is the R default path for MAC and how can I find my codes?
Thanks in advance

If you just executed save.image() and then hit yes, your data was saved to a file called .Rdata
In terminal, type ls -alh in the same working directory and you'll see it.
If you're not sure what dir you're in, in R type getwd()

save.image will not save the commands that created the data objects in the .Rdata file. Instead you need to look at the .Rhistory file that is automatically saved at the end of a session. The default path is the working directory. If you were using the R.app or R64.app GUI working environment then the name of the file is ".Rapp.history", but if it was a Terminal session it is named ".history". In an R session you can get the last 25 lines by typing history(). If you put a numerical argument in that history call, you can change the number of lines displayed.
?history
?getwd
Also look at the R Mac FAQ. It describes how the R.app GUI is different than what you might read in ?loadhistory. You probably also need to learn how to see "dot-files" or "system files". This has long been possible by opening Terminal and typing at the command line:
machine-name:dir username$ defaults write com.apple.finder AppleShowAllFiles YES:
Reboot or relaunch Finder. I keep my .Rhistory (those saved from a Terminal launch of R) and .Rhistory.history (those from an R-GUI launched session) files in the "root" of my User folder.

Related

RStudio keeps adding `/usr/local/bin` to the front of the PATH variable

When I run system commands from RStudio using system("..."), I would like RStudio to use a certain version of Python.
When I open RStudio (just by clicking on the icon on a Mac) and set the PATH variables with Sys.setenv(PATH="my_python_path"), this works successfully. To make this the default variable, I tried updating both my ~/.Renviron (Using PATH=...) and ~/.Rprofile (using Sys.setenv("...")) pre-pending the path to the Python I would like to use.
Updating the PATH variables in .Renviron and .Rprofile takes effect, but RStudio nevertheless keeps pre-pending /usr/local/bin to the front of the PATH variable, which directs R to the default system Python in that directory. The Python path I specified comes right after that and does not get used.
Is there a way to make RStudio respect the PATH order that I specified in my .Renviron or .Rprofile?
From the comments, it's clear that it's not RStudio messing up the path, it's Finder. RStudio does modify the path, but it puts its entries after the existing ones. When I put this code in my ~/.Rprofile file:
print(Sys.getenv("PATH"))
Sys.setenv(PATH=paste("foobar", Sys.getenv("PATH"), sep=":"))
I see this printed:
[1] "/usr/bin:/bin:/usr/sbin:/sbin"
That's the PATH Finder is setting. Then in the R session, when I run Sys.getenv("PATH") I see
[1] "foobar:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:
/usr/local/MacGPG2/bin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local
/git/bin:/Applications/RStudio.app/Contents/MacOS/postback"
showing the additions RStudio (or some code in R run after .Rprofile) has made to the end of the path.

Recent commands searchable in history but not listed after power outage

I'd been working on a project in RStudio all day when the power went out across half of San Francisco... Once power came back it appeared my commands that weren't from a saved workspace were not longer listed in my command history. HOWEVER those commands are actually searchable (and therefore living somewhere on my machine). I need to save this hidden history to a file so I can run all the commands again. Anyone encountered this problem before?
EDIT: In contrary to R, RStudio saves all history commands in history_database file, so that history can be retrieved after unexpected RStudio crashes due to power loss, etc..
The history_database file is located in
On Windows: C:\Users\username\AppData\Local\RStudio-Desktop
On Mac | Nix: ~/RStudio-Desktop
history_database file is a normal ASCII text file and it can be viewed by any text editor.
If you can search history in the current workspace, then save all those history commands in a file:
savehistory(file = "myhist.Rhistory")
Then load it whenever necessary
loadhistory(file = "myhist.Rhistory")
To find out the location of history file in the current workspace
getwd()
dir(all.files = TRUE)
Look for ".Rhistory" hidden file. This file contains all history commands.
For more info: ?"history"
If you want to set the default location of the .Rhistory file in your environment,
Get the current value:
options("R_HISTFILE")
Set the R_HISTFILE to a desired location:
options(R_HISTFILE = "file_path")
For more info: ?options

How do I change the default library path for R packages

I have attempted to install R and R studio on the local drive on my work computer as opposed to the organization network folder because anything that runs through the network is really slow. When installing, the destination path shows that it's my local C:drive. However, when I install a new package, the default path shown is my network drive and there is no option to change:
.libPaths()
[1] "\\\\The library/path/I/don't/want"
[2] "C:/Program Files/R/R-3.2.1/library"
I'm running windows 7 professional. How can I remove library path [1] and make path [2] my primary for all base packages and all new packages that I install?
Windows 7/10: If your C:\Program Files (or wherever R is installed) is blocked for writing, as mine is, then you'll get frustrated editing RProfile.site (as I did). As specified in the accepted answer, I updated R_LIBS_USER and it worked. However, even after reading the fine manual several times and extensive searching, it took me several hours to do this. In the spirit of saving someone else time...
Let's assume you want your packages to reside in C:\R\Library:
Create the folder C:\R\Library. Next I need to add this folder to the R_LIBS_USER path:
Click Start --> Control Panel --> User Accounts --> Change my environmental variables
The Environmental Variables window pops up. If you see R_LIBS_USER, highlight it and click Edit. Otherwise click New. Both actions open a window with fields for Variable and Value.
In my case, R_LIBS_USER was already there, and Value was a path to my desktop. I added to the path the folder that I created, separated by semicolon. C:\R\Library;C:\Users\Eric.Krantz\Desktop\R stuff\Packages.
(NOTE: In the last step, I could have removed the path to the Desktop location and simply left C:\R\Library).
See help(Startup) and help(.libPaths) as you have several possibilities where this may have gotten set. Among them are
setting R_LIBS_USER
assigning .libPaths() in .Rprofile or Rprofile.site
and more.
In this particular case you need to go backwards and unset whereever \\\\The library/path/I/don't/want is set.
To otherwise ignore it you need to override it use explicitly i.e. via
library("somePackage", lib.loc=.libPaths()[-1])
when loading a package.
Facing the very same problem (avoiding the default path in a network) I came up to this solution with the hints given in other answers.
The solution is editing the Rprofile file to overwrite the variable R_LIBS_USER which by default points to the home directory.
Here the steps:
Create the target destination folder for the libraries, e.g.,
~\target.
Find the Rprofile file. In my case it was at C:\Program Files\R\R-3.3.3\library\base\R\Rprofile.
Edit the file and change the definition the variable R_LIBS_USER. In my case, I replaced the this line file.path(Sys.getenv("R_USER"), "R", with file.path("~\target", "R",.
The documentation that support this solution is here
Original file with:
if(!nzchar(Sys.getenv("R_LIBS_USER")))
Sys.setenv(R_LIBS_USER=
file.path(Sys.getenv("R_USER"), "R",
"win-library",
paste(R.version$major,
sub("\\..*$", "", R.version$minor),
sep=".")
))
Modified file:
if(!nzchar(Sys.getenv("R_LIBS_USER")))
Sys.setenv(R_LIBS_USER=
file.path("~\target", "R",
"win-library",
paste(R.version$major,
sub("\\..*$", "", R.version$minor),
sep=".")
))
Windows 10 on a Network
Having your packages stored on the network drive can slow down the performance of R / R Studio considerably, and you spend a lot of time waiting for the libraries to load/install, due to the bottlenecks of having to retrieve and push data over the server back to your local host. See the following for instructions on how to create an .RProfile on your local machine:
Create a directory called C:\Users\xxxxxx\Documents\R\3.4 (or whatever R version you are using, and where you will store your local R packages- your directory location may be different than mine)
On R Console, type Sys.getenv("HOME") to get your home directory (this is where your .RProfile will be stored and R will always check there for packages- and this is on the network if packages are stored there)
Create a file called .Rprofile and place it in :\YOUR\HOME\DIRECTORY\ON_NETWORK (the directory you get after typing Sys.getenv("HOME") in R Console)
File contents of .Rprofile should be like this:
#search 2 places for packages- install new packages to first directory- load built-in packages from the second (this is from your base R package- will be different for some)
.libPaths(c("C:\Users\xxxxxx\Documents\R\3.4", "C:/Program Files/Microsoft/R Client/R_SERVER/library"))
message("*** Setting libPath to local hard drive ***")
#insert a sleep command at line 12 of the unpackPkgZip function. So, just after the package is unzipped.
trace(utils:::unpackPkgZip, quote(Sys.sleep(2)), at=12L, print=TRUE)
message("*** Add 2 second delay when installing packages, to accommodate virus scanner for R 3.4 (fixed in R 3.5+)***")
# fix problem with tcltk for sqldf package: https://github.com/ggrothendieck/sqldf#problem-involvling-tcltk
options(gsubfn.engine = "R")
message("*** Successfully loaded .Rprofile ***")
Restart R Studio and verify that you see that the messages above are displayed.
Now you can enjoy faster performance of your application on local host, vs. storing the packages on the network and slowing everything down.
I was struggling for a while with this as my work computer (with Windows 10) created the default user library on a network drive, which would slow down R and RStudio to an unusable state.
In case this helps someone, this is the easiest way I found, without requiring admin rights:
make sure the directory you want to install your packages into exists. If you want to respect the convention, use: C:\Users\username\R\win-library\rversion (for example, something like: C:\Users\janebloggs\R\win-library\3.6)
create a .Renviron file in your home directory (which might be on the network drive?), and in it, write one single line that defines the R_LIBS_USER variable to be your custom path:
R_LIBS_USER=C:\Users\janebloggs\R\win-library\3.6
(feel free to add comments too, with lines starting with #)
If a .Renviron file exists, R will read it at startup and use the variables as they are defined in there, before running the code in the .Rprofile. You can read about it in help(Startup).
Now it should be persistent between sessions!
After a couple of hours of trying to solve the issue in several ways, some of which are described here, for me (on Win 10) the option of creating a Renviron file worked, but a little different from what was written here above.
The task is to change the value of the variable R_LIBS_USER. To do this two steps needed:
Create the file named Renviron (without dot) in the folder \Program\etc\ (Program is the directory where R is installed--for example, for me it was C:\Program Files\R\R-4.0.0\etc)
Insert a line in Renviron with new path: R_LIBS_USER = "C:/R/Library"
After that, reboot R and use .libPaths() to confirm the default directory changed.
I think I tried all of the above and it didn't work for me. This worked, though:
In home directory, make a file called ".Renviron"
In that file, write:
.libPaths(new = "/my/path/to/libs")
Save and restart R if you had it open

Permanently removing objects in RStudio Mac OS?

Every time I started RStudio, I have seen this my working environment.
I can use rm(list=ls()) to remove them temporarily, but every time I restarted RStudio, they showed up again.
I use getwd() to see my working directory, but in the working directory, I did not see any .Rdata file. How can I get rid of these things ?
Your kind help will be well regarded.
I use Mac OS 10.10.
Click on RStudio in the menu bar and go to Preferences.
In the R General section, unclick the Restore .RData into workspace at startup option.
The default is to reload the working environment when you restart RStudio.
I think that you, at some point, chose to save your environment to your working directory (most likely ~, i.e. your home directory, which is the default RStudio working directory).
The easier way to clear your default environment is to remove the .RData file from your home directory. It will not appear in any Finder window, because in a Unix-like OS (like OS X), files starting with . are hidden. So do the following:
Open a terminal window
If not already there, go to your home folder: cd ~
Check if there's an .RData file: ls -lA .RData
If the file exists delete it: rm .RData (if you want, create a backup: `cp .RData ./RData_backup)

remove r workspace in ubuntu

folks
I just installed ubuntu R, but when I saved my R work space, it loaded automatically every time when i launch R from the terminal. This had disabled couple of functions in the package i wanted to use.
My questions is how to remove my r work space in ubuntu? any idea where the location of the file is ?
I tried suggestions from this site: http://r.789695.n4.nabble.com/How-to-permanently-remove-Previously-saved-workspace-restored-td3041515.html
, but i did not really give a clear solution to it.
Best.
Its a file called .RData in your working directory:
> getwd()
[1] "/home/rowlings"
> system("ls .RData")
.RData
Because it starts with a dot its not visible in the unix shell unless you do ls -a.
Try removing ~/.RData
In case that is hard for you to understand, ~/ is the Unix way of saying "your home directory", and the ~/.RData file is the "default" workspace if you didn't explicitly set a different working directory.

Resources