How to get current working directory in R? - r

How to get current working dir?
I guess there should be a command like getcwd(), however, I can't find something like this in the documens.
How to change to another dir?
How to set default working dir when I start R?
version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10)
nickname Spring Dance
Thanks!

It is getwd()
It is setwd("path/to/new/dir")
Either via a Windows property, or via .Rprofile etc, or (as I recall) by defining $HOME which Windows does not set by default. See help(Startup).

As a complement to #Dirk's answer, I would like to add something that might be useful and is frequently overlooked.
It is possible to save the current working directory and set the new one at the same time.
Function setwd returns the working directory so all you have to do is to save its value. This can be useful if you, at a later moment, need to return to the original working directory.
old_dir <- setwd("/path/to/new/dir")
#[run the code you want]
setwd(old_dir)

Related

zip files without including parent directories

When I run this
zip('C:\\path\\to\\dir\\out'
, files = paste0('C:\\path\\to\\dir\\', c('one.xlsx', 'two.xlsx')))
it zips the files, but unzipping them gives a folder that contains the "path" folder, which contains the "to" folder, etc (the full directory path). So after unzipping, the files are now in C:\\path\\to\\dir\\out\\path\\to\\dir\\.
How can I zip the files without the resulting zip file containing all the parent directories?
I'm using Windows 10
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 4.2
year 2017
month 09
day 28
svn rev 73368
language R
version.string R version 3.4.2 (2017-09-28)
nickname Short Summer
According to the documentation, the R zip function's utilizes R_ZIPCMD, which is set in etc/Rcmd_environ. This is set to the command line zip windows utility by default. The R function provides the input parameter flags to pass additional input parameters to the underlying command line zip function. The manual which describes
flags can be downloaded here.
The -j flag allows just the file names to be stored rather than the full file path.
f_path <- 'C:\\path\\to\\dir\\out'
zip(f_path,
files = paste0(f_path, c('one.xlsx', 'two.xlsx')),
flags = '-r9Xj')
The -r9X portion of the flags input are the default parameters passed to underlying utility function and specify that the zip command should recursively search sub-directories, use maximum compression, and remove depreciated file fields.
This has only been tested using the windows zip utility. The necessary flag(s) may differ when using the unix utility.

Command to see 'R' path that RStudio is using

Original Question
This seems easy and has likely been asked before, but I could not find it via a search.
I have a few flavors of R installed. I simply want to know, when I run RStudio, which flavor of R is it pointing to. So, I need a command -- within RStudio itself, ideally -- that can tell me the underlying R executable that is being used for this RStudio window that I am currently working with.
To be clear, I do not need / want to know the version of R that I'm using (e.g., R version 3.2.2 (2015-08-14) -- 'Fire Safety'). Instead, I want to know the actual path that RStudio is using to get to R -- looking at it from within RStudio -- so that I know "for reals" which version it's using. (E.g., /usr/local/bin/R.)
Edit & Answer
There are a lot of great discussions here, and some are OS-specific. I have a Mac. In my case, I found that:
> system("type R")
R is /usr/local/bin/R
> R.home()
[1] "/usr/local/Cellar/r/3.2.2_1/R.framework/Resources"
> file.path(R.home("bin"), "R")
[1] "/usr/local/Cellar/r/3.2.2_1/R.framework/Resources/bin/R"
As those of you familiar can see, I am using brew. If I look for /usr/local/bin/R outside of R, I see:
$ ls -l /usr/local/bin/R
lrwxr-xr-x 1 mike admin 25 Nov 14 17:31 /usr/local/bin/R -> ../Cellar/r/3.2.2_1/bin/R
which eventually resolves (2 symbolic links) to:
/usr/local/Cellar/r/3.2.2_1/R.framework/Resources/bin/R
as the final destination.
So on my system (Mac OS X), file.path(R.home("bin"), "R") was the most accurate.
(Edited to reflect fact that this is apparently a Windows-specific solution.)
Here on Windows, I'd use the following, for reasons discussed here by Henrik Bengtsson near the start of a long thread on the subject.
file.path(R.home("bin"), "R")
This is better than using file.path(R.home(), "bin", "R") in several settings alluded to in the "Value" section of this snippet from help(R.home):
Details:
The R home directory is the top-level directory of the R
installation being run.
[...]
Value:
A character string giving the R home directory or path to a
particular component. Normally the components are all subdirectories
of the R home directory, but this may not be the case in a Unix-like
installation. [...] The return value for "modules" and on Windows
"bin" is to a sub-architecture-specific location.

Older version of R in mac OS X 10.10.5

I have a problem downloading R in my mac which I have been dealing with for many hours now.
My purpose is to use propensity score matching in SPSS (from source forge). I am using SPSS version 22 on my mac OS X yosemite 10.10.5.
After reading a lot on the different sites I need to use R.2.15 for SPSS 22 in order to use the psmatching 3.03 on source forge.
However, I cannot seem to install R 2.15 when I run the installation package. When I am going to select destination on the macintosh HD, it says that R2.15.0 for Mac OS X 10.5 or higher can’t be installed on this disk. How is this possible? My OS X version is higher than the demanded version for R 2.15.
Any ideas how to make the installation?
I've run into this problem myself: SPSS versions require very specific versions of R to be installed for the R Integration plug-in to work. An older or newer version of R will not suffice.
I think the installation error is due to the old R installer's attempt to verify a sufficiently high OS X system version. But it doesn't recognize 10.10+ as being higher than 10.9 (or 10.5 for that matter).
The link below has information about disabling OS X El Capitan's System Integrity Protection, in order to change the OS X file /System/Library/CoreServices/SystemVersion.plist, so that a false system version can be reported to the installer.
https://forums.developer.apple.com/thread/4138
A safer method might be to manually place the files from R's 2.15 installer. There's an apparent complication with my attempt at that, though. I opened the R 2.15.pkg installer file, found a file 'payload' inside, used 'tar -xzvf Payload' to extract contents, and moved those contents into /Library/Frameworks/R.framework/Versions/2.15. In retrospect, it may be most useful to temporarily re-alias "Current" in the R.frameworks folder so that it points to 2.15 version while the installer completes. Instead, I did the following to get around installer error messages I found in the installer log file, which worked for me:
The SPSS Essentials for R installer reported that the install location does not contain R 2.15. Examining the error log files led me to believe that the installer's attempt to verify the R version inappropriately navigated to the "Current" version (aliased to a specific version, likely not R 2.15 if 2.15 was installed manually), even if the directory for 2.15 was specifically entered into the installer. It then seeks a couple of i386 subdirectories that no longer exist as of R 3.2.2. So (for my case) I manually created "/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/exec/i386" and copied version 2.15 of the R unix executable, and did similarly for "/Library/Frameworks/R.framework/Versions/3.2/Resources/etc/i386", copying in the ldpaths file that the installer seeks. Doing so finally allowed the SPSS Essentials for R installer to complete. In retrospect, temporarily re-aliasing "Current" in the R.frameworks folder might have been a more direct approach, but I haven't tried that as of right now.
For what it's worth, I was able to install an older version of R (for a different purpose, but I found this page while trying to figure it out), without having to temporarily change the version of OS X (or macOS) on my system. That seemed dangerous to me, and I wanted a simpler fix.
What worked for me was extracting the installer pkg, modifying the version check in there, and then packaging it up again.
Downloading the pkg installer from CRAN
open Terminal and cd to the directory where the installer is: e.g.,
cd ~/Downloads
use pkgutil to expand the pkg file you downloaded
pkgutil --expand R-2.15.0.pkg R-2.15.0
This creates a folder named R-2.15.0 (You can name it whatever you want in the command above). In that folder is a text file called Distribution.
Open the Distribution file in a text editor (e.g., TextEdit), and
modify the function at the beginning that checks the version of OS
X. for example, if it requires Leopard, there will be a line that
looks like this:
if(!(my.target.systemVersion.ProductVersion >= '10.5.0')) {
...
}
Notice that version string for comparison is a string (not a number!), so any version past 10.9 (10.10 and up) will fail that check ('10.10' sorts between '10.1' and '10.2', which is less than '10.5').
All I did was change the minimum version here ('10.5.0') to '10.1'. So the same line reads:
if(!(my.target.systemVersion.ProductVersion >= '10.1')) {
Save the Distribution file.
Go back to terminal and re-package the directory you created in step 3:
pkgutil --flatten R-2.15.0 R-2.15.0-HACKED.pkg
You can call the new pkg whatever you want, as long as you will recognize it.
The resulting package (R-2.15.0-HACKED.pkg, in this example) will now run on any version of OS X higher than or equal to the version you specified (in this case, 10.1 and up).
Disclaimer: I actually tested this with R-2.12.0 (and it worked), but I provided example code for R-2.15.0 to answer the original question. It should be similar with any R installer pkg for OS X that uses a similar version string to check for compatibility before installation.

How to create MacOS/Linux link for reading data file?

I have a huge raw data file which I do not intend to change or copy. And I have two projects in RStudio and both need to be able to access it.
I originally created the alias (in MacOS) in the following way
right click the file ~/A/data.csv in finder, and click "make alias". Then copy the alias to ~/B/ and rename it ~/B/data.csv
I also tried the following command later
ln -s ~/A/data.csv ~/B
For project A, I put the actual data file in A/data/data.csv.
For project B, I created an alias under B/data/.
But when I try fread('B/data/data.csv'), it complains:
sh: ./data/data.csv: Too many levels of symbolic links
Error in fread("./data/data.csv") :
File is empty: /var/folders/4h/2jg64xk52mv3fyq4sb7s371w0000gn/T//Rtmp7cWNN3/filebf3013ad9194
I think I can use a hard link to solve this issue, but just want to see if I can use alias to make it work.
=====
I don't think it matters, but for completeness, see the following for my OS and R version:
platform x86_64-apple-darwin10.8.0
arch x86_64
os darwin10.8.0
system x86_64, darwin10.8.0
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10)
nickname Spring Dance
I'm not entirely sure why using aliases in this specific case:
note that for small files (e.g. < 1 MB), the alias can have a way bigger memory footprint. For example, for a simple text file containing "test" (echo "test" > test.txt) the alias will be 274k times bigger:
test.txt: 5 bytes
test.txt alias: 1372636 bytes
since RStudio is good at using absolute paths, why not directly link to ~/A/data.csv directly rather than linking to it's alias?
Two alternative solutions (not directly answering question) could be to (1) copy the file or (2) create a kind of symbolic link.
Copying the file
#!/bin/bash
mkdir ~/B/data/
cp ~/A/data.csv ~/B/data/
Or in R, using system (on Mac):
system("mkdir ~/B/data/")
system("cp ~/A/data.csv ~/B/data/")
Creating a symbolic link
This can be done by simply saving the path of the file ~/A/data.csv in ~/B/data/.
In shell:
#!/bin/bash
mkdir ~/B/data/
echo "~/A/data.csv" > ~/B/data/data.csv
(this part can be also done in R using system() as above)
And then, in R:
## Reading path in B/data/
PATH <- scan(file = "~/B/data/data.csv", what = character())
## Opening the file (~/A/data/data.csv)
my_csv <- read.csv(PATH)

Which R version my PC currently Uses?

Im currently using R through Java, and im not sure which version im using.
I've stetted in R studio my R version into version 2.15.3 which is what i need:
And so is my enviorment variable setting (R_home):
But typing in the command line : "R":
So which version in being used?
Based on what you describe, you have the most updated version of R installed and RStudio is able to detect it. However the version of R that is currently in your OS environment variable (and hence used by Java) is not the most recent,
=> you need to update your PATH environment variable.
The procedure might be a bit different whether you are on Unix or Windows.
I am sure you'll find plenty of ressources on Internet to help you do that How to update PATH variable permanently from cmd? Windows

Resources