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

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)

Related

Find pandoc executable from R

rmarkdown::find_pandoc helps us to find the pandoc executable w/o the need of specifiying any environmental variable when running form within RStudio:
## in Rstudio
!is.null(rmarkdown::find_pandoc()$dir)
# [1] TRUE
However, when running the same command from a plain R console I get:
### R console
!is.null(rmarkdown::find_pandoc()$dir)
# [1] FALSE
Reading the documentation pf ?rmarkdown::find_pandoc(), explains why I am getting these results:
dir: A character vector of potential directory paths under which
‘pandoc’ may be found. If not provided, this function
searches for ‘pandoc’ from the environment variable
RSTUDIO_PANDOC (the RStudio IDE will set this variable to the
directory of Pandoc bundled with the IDE), the environment
variable PATH, and the directory ‘~/opt/pandoc/’.
I want now to write a script which can be run from the command line (specifically not from within RStudio), which needs pandoc to be found. As per the help I could set my PATH to assure that pandoc is also found from the command line, but as soon as I want a colleague of mine to use the script, I have to make sure that his/her PATH is set accordingly and I want to avoid that.
However, I do know that everybody has Rstudio installed (not at the same location though), so if I knew the location of RStudio I could derive pandoc's location too.
Is there any reliable way to get the path of Rstudio from the console (i.e. also when Rstudio is not running)?
To make a long story short: how can I find Rstudio even if it is not running`?
This could work, it is based on linux, but I guess if you are in other system, it could be adapted:
pandoc_path <- system2("find", args="/usr/lib/rstudio -name pandoc -type f", stdout = T)
path_sep <- ":"
Sys.setenv(PATH = paste0(Sys.getenv("PATH"),path_sep, sub(".pandoc$","",pandoc_path )))

How do you make RStudio remember R installations it used in the past?

I use a separate installation of R for each project.
To make RStudio use the right one I click and hold down the Control key when I start RStudio.
This opens a "Choosing R Installation" pop-up where I can select the R binary I need.
The problem is that - unlike in this RStudio support article - the pop-up shows only the very last version of R that I used.
So whenever I switch projects I have to browse to the R binary that I need.
Is there a way to make RStudio remember the other R versions I've used?
If there isn't, where does RStudio look for the last used one? If I knew that, I could probably use a script to overwrite it before launching RStudio.
Some details:
I am on Windows 7/10.
RStudio version is 1.3.1093
None of the registry keys described here are set.
Since you mentioned that you kept the registry clean and under the assumption that you are using distinct/separate folders for each of your projects one COULD be tempted to try something like
a .Renviron file containing R_HOME=C:/path/to/your/required/version or
a .Rprofile file containing Sys.setenv(R_HOME='path/to/your/version')
in your project folder(s)?
Or potentially put a .bat file in there with content like
SET R_HOME=Path/to/your/R.version
cd /D "%~dp0"
start .YourProject.Rproj
BUT NOPE!
Currently this type of functionality/behaviour is NOT available for RStudio Desktop Free (as of time of writing this is RStudio Desktop 1.3.1093) BUT can only be achieved with RStudio Server PRO - see 6.3.3 User Configurable Default Version.
#Evgenil: updated answer based on your suggestion and after I had the time to test the idea as well
I had a similar problem. The way I solved this was by utilizing portable versions of R and RStudio.
To get this to work I needed a few things.
Download portable versions of R, https://sourceforge.net/projects/rportable/files/
Download portable versions of RStudio, https://sourceforge.net/projects/rportable/files/
Download IniFile 1.92, https://www.horstmuc.de/wbat32.htm#inifile
In this example I have created three folders on my desktop.
BatchSetRandRStudio, R-Portable-4.1.2, RStudio-2022.02.3-492
Example steps:
Create a folder to house the batch file that will launch R and RStudio. e.g. C:\Users<username>\Desktop\BatchSetRandRStudio.
Place the downloaded IniFile, inifile.exe, in the folder you just created for the batch file.
Create a batch file with the example code below and change the paths to your specifications for the portable versions of R and RStudio. e.g. Batch4.1.2RLaunchRStudioRun.bat
#echo off
REM Remember to update the Rprofile.site within each R-Portable location, in the etc folder for each portable R version:
REM C:\Users\<username>\Desktop\R-Portable-4.1.2\App\R-Portable\etc
REM .First = function(){
REM .libPaths(.Library)
REM }
REM Also the portable R library location is located:
REM C:\Users\<username>\Desktop\R-Portable-4.1.2\App\R-Portable\library
:: INI editor; inifile.exe needs to be in same folder of batch file to work
:: Source: https://www.horstmuc.de/wbat32.htm#inifile
:: INIFILE filename [section] item=string
:: inifile.exe "C:\Users\<username>\AppData\Roaming\RStudio\desktop.ini" [General] RBinDir=C:/Users/<username>/Desktop/R-Portable-4.1.2/App/R-Portable/bin/x64
inifile.exe "C:\Users\<username>\AppData\Roaming\RStudio\desktop.ini" [General] RBinDir=C:/Users/<username>/Desktop/R-Portable-4.1.2/App/R-Portable/bin/x64
:: FOR R 4.1.2
:: Start Program
:: start "" "C:\Users\<project folder>\Desktop\RStudio-2022.02.3-492\bin\rstudio.exe"
start "" "C:\Users\<username>\Desktop\RStudio-2022.02.3-492\bin\rstudio.exe"
:: FOR R 4.1.2
:: Start Program and specific project
:: start "" "C:\Users\<username>\Desktop\RStudio-2022.02.3-492\bin\rstudio.exe" "C:\Users\<username>\Desktop\R-Portable-4.1.2\Projects\<project folder>\<project name>.Rproj" & :: This command starts RStudio and opens specific project
Update the Rprofile.site file within each R-Portable location (allows one to have separate package installations for each portable version you create) with the following code:
.First = function(){
.libPaths(.Library)
}
Run the batch file, RStudio should open with the version of R you have pointed it to as well as the version of RStudio you have specified. You should be able to add projects and create specific batch files to open each project separately.

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.

Running R from Mac OSX terminal

I've searched the web, and I'm still unclear on how to run R from the Mac terminal. I have Rstudio and the standalone R app installed. I thought I could just type "R" from the command line as I do with "python", but that doesn't work. Is it necessary to edit the PATH in my bash profile? If so, how do I give the correct location of R?
Thanks for any help
Edits after receiving comments
So, I'm running Sierra, and when I type "r" or "R" at the terminal, I get "-bash: R: command not found." If I type, "which R" in the terminal I do not get any output.
Here is the output from "echo $PATH": /usr/local/heroku/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/samuelcolon/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0#global/bin:/Users/samuelcolon/.rvm/rubies/ruby-2.1.0/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/samuelcolon/.rvm/bin:/Users/samuelcolon/.rvm/bin
As for the installation, I believe I downloaded it directly from cran.r-project.org a while ago. I can locate the GUI in my applications and open it--
I have version 3.13. Is it possible, I only have R.app installed but not R? Perhaps that's the reason I'm getting the 'command not found' when typing "R" into the terminal?
Generally, I've been working in RStudio, but I'd still like to access R from the terminal and also to find where things are located. I'm fine with removing and re-installing R if it's easiest to start from square one. I hope the extra detail helps, and I appreciate the responses.
An answer for those not that familiar with Terminal and Bash.
I have done a fresh update install of R from the R.org cran site as part of seeking an answer to your question.
I found this latest install version 3.4.0 installs R for access in Terminal, and also installs R.app as part of the package.
To my understanding, reading support docs, if you have an older version of R it will update that. However it will not update an installation of R installed by the anaconda package.
Where are the R files stored?
I can only assume that with a fresh install of the latest R, R will work for you in Terminal.
To learn where the R files are that are being accessed - in Terminal after starting R, and in R.app, type:
>R.home()
In my case as example:
In R.app - the R version 3.4.0 is accessed in the top directory (not my user folder):
R.home()
[1] "/Library/Frameworks/R.framework/Resources"
In Terminal - the R version 3.3.2 is accessed in the Anaconda package, again in the top level directory.
R.home()
[1] "/anaconda/lib/R"
So I have two different versions of R, and Terminal accesses a different version to R.app.
How can I ensure I access the same version in Terminal as I do in the R.app?
For someone familiar with bash, and how the whole bash command system works I am sure there is a well constructed command. All the same here are some novice solutions.
-
• First Solution:
I could update the anaconda version, however, I would prefer not to as as other elements of the anaconda package my depend on this older version of R. For those not yet familiar with Terminal and bash, not such a novice solution.
-
• Second Solution:
This solution came from mko. It provides a single use solution. From the result above, and checking the directory structure a little further to find this R file.
Finding the significant R file enables me to edit an extension of the above path shown in the R.app. So add /bin/R to enter
/Library/Frameworks/R.framework/Resources/bin/R
Entering and pressing return will start R from this version.
Alternatively, one can find this file and icon in the GUI Finder, lead by the above result, and just double click on it, and it will open Terminal and a session with R running for you. Easy!
One could also make an alias of it and put it on your desktop for easy future starts.
-
• Third Solution:
My last solution I think may be best, adding to mko's solution. Make an alias.
Being in my home directory in Terminal I open .bash_profile using the nano text editor. (If you do not already know how to do this, then best not use this solution.)
I then add the line in this env file.
alias Rv340='/Library/Frameworks/R.framework/Resources/bin/R'
I then save the changes and exit this terminal session. I then open a new Terminal window. (This is so the changes to the env above are incorporated in the new terminal session).
Then when I enter the alias:
Rv340
The version of R I want opens.
You can choose a different alias name to "Rv340".
-
• Fourth Solution:
A second more permanent solution for opening the same version of R in Terminal is as follows.
Copy the path as showing in R.app in response to the R.home() command above, and add that path to PATH in your .bash_profile. (If you do not know already how to do this, then ignore this solution.) Do so as follows.
export PATH="/Library/Frameworks/R.framework/Resources:$PATH"
To my understanding, this ensures that bash looks here for R (and anything else), then moves on to the other paths in PATH. Since this adds this path to the beginning of $PATH, an env variable, bash looks here first where it finds the newer version first, and stops looking.
When it comes to understanding PATH in the env set up in .bash_profile the following two links were helpful.
About PATH.
How to correctly add a path to PATH.
This solution may muck with anaconda's invocation of R. I have yet to check this.
First of all, you have to start terminal application. You can use either built in Terminal.app, or you can use replacement. My favorite one is iTerm2
https://www.iterm2.com
Then, you simply open terminal window and run R. Just like shown below:
Have fun with R!
Just ran into the same issue when installing R-4.0.3.pkg on my MacBook (MacOS BigSur). Can open R.app to the clunky R GUI, but typing in 'R' in terminal doesn't work.
Turns out, an R executable lives here: /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R
So I added this alias to my newly created .zshrc script:
alias R '/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R'
Now when I type in R, it opens... I swear this all happened seamlessly in earlier versions.
There is currently a bug in CRAN's R installation package that results in it not correctly installing symbolic links to R and Rscript for commandline use. I've just verified this by inspecting the postflight script in their 4.0.5 installation package. This only impacts MacOS system releases of 20 and above (you can check with uname -r).
I've included more info here, along with what the "correct" fix should be: manually creating symbolic links to /usr/local/bin that point to the R and Rscript binaries themselves. If this is the current challenge, then this would be a far better solution to creating aliases or manipulating PATH in various ways, since it's what the installation package intended to do (and presumably will again soon).
R: command not found
In short, if this is the problem, then Ashkan Mirzaee's answer (https://stackoverflow.com/a/67202173/2093929) to create the symbolic links directly is correct in form, but might not have the right link command. The 4.0.5 package intends instead to use:
mkdir -p /usr/local/bin
cd /usr/local/bin
rm -f R Rscript
ln -s /Library/Frameworks/R.framework/Resources/bin/R .
ln -s /Library/Frameworks/R.framework/Resources/bin/Rscript .
You can create a symbolic link from R and Rscript binaries to /usr/local/bin to add them to the PATH:
sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/R /usr/local/bin
sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/Rscript /usr/local/bin
Now which R should return /usr/local/bin/R and you can use R.
An easy way to open RStudio with admin privilege on macOS:
Go to Applications, then right click on RStudio
Select "Show Package Contents"
Go to Contents/MacOS
Now open terminal(in bash mode). Type sudo and drag the RStudio.exec into terminal and press on ENTER
Now RStudio will have admin access!

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.

Resources