Sublime Text 2 and R - r

I am trying to use Sublime Text 2 as an interface to the statistics software R [update/edit: Solved!].
On Windows, I have tried the following:
Installed R Tools. Turned out to be for Macintosh 64 only.
Tried to program custom build file. Failed: no output returned.
{
"cmd": "C:/Program Files/R/R-2.9.2/bin/R.exe --no-save $File"
}
Installed SublimeREPL. Failed: R menu option disabled...
[update/edit] Tried this (see wuub's reply):
{
"default_extend_env": {"PATH": "{PATH};C:\\Program Files\\R\\R-2.9.2\\bin"}
}

Open SublimeREPL's user settings like this: Preferences -> Package Settings -> SublimeREPL -> Settings - User
there set default_extend_path to point to your R installation:
{
"default_extend_env": {"PATH": "{PATH};C:\\Program Files\\R\\R-2.14.2\\bin\\i386"}
}
Running Tools -> SublimeREPL -> R should launch REPL as expected.

For windows system you can add a new build for R (Tools -> Build System -> New Build System) and put the following lines. Modify the path according to your R installation directory.
{"cmd": ["Rscript.exe", "$file"],
"path": "C:\\Program Files\\R\\R-2.15.2\\bin\\x64\\",
"selector": "source.r"}
You can execute the entire file by pressing ctrl+B instead of starting SublimeREPL.

The build system value needs to be an array so you want;
{
"cmd": ["C:/Program Files/R/R-2.9.2/bin/R.exe", "--no-save", "$File"]
}
https://docs.sublimetext.io/guide/usage/build-systems.html#file-format

For linux users, take a look:
Sublime Text 2 R build System
It supports multi selection and interactive R sessions.

Related

How can I activate a '.js' script from R?

I have a '.js' script that I usually activate from the terminal using the command node script.js. As this is part of a process where I first do some data analysis in R, I want to avoid the manual step of opening the terminal and typing the command by simply having R do it for me. My goal would be something like this:
...R analysis
write.csv(df, "data.csv")
system('node script.js')
However, when I use that specific code, I get the error:
sh: 1: node: not found
Warning message:
In system("node script.js") : error in running command
Of course, the same command runs without problem if I type it directly on the terminal.
About my Software
I am using:
Linux computer with the PopOS!
RStudio 2021.09.1+372 "Ghost Orchid"
R version 4.0.4.
The error message node: not found indicates that it couldn't find the program node. It's likely in PATH in your terminal's shell, but not in system()'s shell (sh).
In your terminal, locate node by executing which node. This will show the full path to the executable. Use that full path in system() instead.
Alternatively, run echo $PATH in your terminal, and run system('echo $PATH') or Sys.getenv('PATH') in R. Add any missing directories to R's path with Sys.setenv(PATH = <your new path string>)
Note that system2() is recommended over system() nowadays - but for reasons unimportant for your case. See ?system and ?system2 for a comparison.
Examples
Terminal
$ which node
/usr/bin/node
R
system('/usr/bin/node script.js')
# alternatively:
system2('/usr/bin/node', c('script.js'))
or adapt your PATH permanently:
Terminal
% echo $PATH
/usr/local/bin:/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
R
> Sys.getenv('PATH')
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback"
> Sys.setenv(PATH = "/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback")

Set up R in Visual Studio code: Path to shell executable "..." is not a file of a symlink

I am trying to set up R to work with Visual Studio code. When I invoke a function in an R script, I get the following message: "The terminal process failed to launch: Path to shell executable "/Library/Frameworks/R.framework/Resources" is not a file of a symlink."
I have checked the home folder for R using R.home(), which provides "/Library/Frameworks/R.framework/Resources".
To solve this problem, I have followed the advice laid out in this medium post and followed this discussion.
Under settings I have added the following lines:
"r.bracketedPaste": true,
"r.rterm.mac": "/Library/Frameworks/R.framework/Resources",
"r.lsp.debug": true,
"r.rpath.mac": "/Library/Frameworks/R.framework/Resources",
"r.lsp.diagnostics": true,
"r.rterm.option": [
"--no-save",
"--no-restore",
"--r-binary=/Library/Frameworks/R.framework/Resources"
]
However, this results in the same error.
Not sure your "r.rterm.mac" is right
Mine is
"r.rterm.windows": "C:\\Program Files\\R\\R-4.0.3\\bin\\R.exe",
and it works
You could also take a look at Setting Up Visual Studio code to work with R - "win32 can't use R"

How to add context menu option to start a program in the given working directory

When you install Git on windows, it adds a context menu option when you right-click on a folder to "Git Bash Here". The way it does this is by adding a registry key like this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\git_shell\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%1\""
Notice the cd argument at the end that passes the directory name to the program.
I would like to do something similar for R (and other programs). Unfortunately R doesn't accept a cd argument. This will launch R:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\R\command]
#="\"C:\\Program Files\\R\\R-3.4.3\\bin\\x64\\Rgui.exe\" \"--cd=%1\""
but it gives an error message saying the cd argument is not recognized, and Rgui will start with whatever the default working directory is, defeating the entire point.
What I really want it to do is the equivalent of this command:
start "R" /D %1 "C:\Program Files\R\R-3.4.3\bin\x64\Rgui.exe"
where %1 is the folder that was right-clicked on. Is this possible?
You can write R code that runs on startup and checks the commandline arguments.
You can put the following code at the end of C:\Program Files\R\R-3.4.3\etc\Rprofile.site (or any other file that gets executed at startup):
local({
processArg <- function(arg) {
parts <- strsplit(arg, "=")[[1]]
if (length(parts) == 2) {
if (parts[1] == "R_startup_wd") {
setwd(parts[2])
}
}
}
invisible(sapply(commandArgs(FALSE), processArg))
})
It checks if R was called with an argument R_startup_wd=your_working_dir and changes the working directory if yes.
You can then call R like
"C:\Program Files\R\R-3.4.3\bin\x64\Rgui.exe" "R_startup_wd=your_working_dir"
Note that the argument name is given without "--", i.e. we have R_startup_wd and not --R_startup_wd. Otherwise RGui will complain about "unknown arguments"
You can of course still use R without the argument given.

How to use Jupyter + SparkR and custom R install

I am using a Dockerized image and Jupyter notebook along with SparkR kernel. When I create a SparkR notebook, it uses an install of Microsoft R (3.3.2) instead of vanilla CRAN R install (3.2.3).
The Docker image I'm using installs some custom R libraries and Python pacakages but I don't explicitly install Microsoft R. Regardless of whether or not I can remove Microsoft R or have it side-by-side, how I can get my SparkR kernel to use a custom installation of R?
Thanks in advance
Docker-related issues aside, the settings for Jupyter kernels are configured in files named kernel.json, residing in specific directories (one per kernel) which can be seen using the command jupyter kernelspec list; for example, here is the case in my (Linux) machine:
$ jupyter kernelspec list
Available kernels:
python2 /usr/lib/python2.7/site-packages/ipykernel/resources
caffe /usr/local/share/jupyter/kernels/caffe
ir /usr/local/share/jupyter/kernels/ir
pyspark /usr/local/share/jupyter/kernels/pyspark
pyspark2 /usr/local/share/jupyter/kernels/pyspark2
tensorflow /usr/local/share/jupyter/kernels/tensorflow
Again, as an example, here are the contents of the kernel.json for my R kernel (ir)
{
"argv": ["/usr/lib64/R/bin/R", "--slave", "-e", "IRkernel::main()", "--args", "{connection_file}"],
"display_name": "R 3.3.2",
"language": "R"
}
And here is the respective file for my pyspark2 kernel:
{
"display_name": "PySpark (Spark 2.0)",
"language": "python",
"argv": [
"/opt/intel/intelpython27/bin/python2",
"-m",
"ipykernel",
"-f",
"{connection_file}"
],
"env": {
"SPARK_HOME": "/home/ctsats/spark-2.0.0-bin-hadoop2.6",
"PYTHONPATH": "/home/ctsats/spark-2.0.0-bin-hadoop2.6/python:/home/ctsats/spark-2.0.0-bin-hadoop2.6/python/lib/py4j-0.10.1-src.zip",
"PYTHONSTARTUP": "/home/ctsats/spark-2.0.0-bin-hadoop2.6/python/pyspark/shell.py",
"PYSPARK_PYTHON": "/opt/intel/intelpython27/bin/python2"
}
}
As you can see, in both cases the first element of argv is the executable for the respective language - in my case, GNU R for my ir kernel and Intel Python 2.7 for my pyspark2 kernel. Changing this, so that it points to your GNU R executable, should resolve your issue.
To use a custom R environment I believe you need to set the following application properties when you start Spark:
"spark.r.command": "/custom/path/bin/R",
"spark.r.driver.command": "/custom/path/bin/Rscript",
"spark.r.shell.command" : "/custom/path/bin/R"
This is more completely documented here: https://spark.apache.org/docs/latest/configuration.html#sparkr

Running Nvim-R via PuTTy: setting up r_term_cdm

I would like to run Nvim-R on a remote machine via putty when I try to open a *.R file the remote machine returns an error message:
Please set the variable g:R_term_cmd in your vimrc. Read the plugin
documentation ...
According to the documentation, the R_term_cmd should be used in the following manner:
If |R_in_buffer| = 0 and the X Window System is running and tmux is
installed, then R will run in an external terminal emulator. The
plugin uses the first terminal emulator that it finds in the following
list:
1. gnome-terminal,
2. konsole,
3. xfce4-terminal,
4. Eterm,
5. (u)rxvt,
6. aterm,
7. roxterm,
8. terminator,
9. lxterminal
10. xterm.
If Vim does not select your favorite terminal emulator, you may define
it in your vimrc by setting the variable R_term, as shown below:
let R_term = "xterm"
If your terminal emulator is not listed above, or if you are not satisfied with the way your terminal emulator
is called by the plugin, you may define in your vimrc the variable
R_term_cmd, as in the examples below:
let R_term_cmd = "xterm -title R -e"
let R_term_cmd = "xfce4-terminal --icon=/path/to/icons/R.png --title=R -x"
However, this variable does not appear to be utilised by the sample configuration files available through the Vim-R-Tmux: An Integrated Working Environment for R. Furthermore, the settings in vimrc:
" start R with F2 key
map <F2> <Plug>RStart
imap <F2> <Plug>RStart
vmap <F2> <Plug>RStart
" send selection to R with space bar
vmap <Space> <Plug>RDSendSelection
" send line to R with space bar
nmap <Space> <Plug>RDSendLine
<LocalLeader> settings
The suggested <LocalLeader> settings not appear to work as pressing F2 does not launch connected R session.
Software versions
tmux 2.3
VIM - Vi IMproved 8.0
You have two options
Use Tmux, which you apparently are
This way, Nvim-R can actually use a tmux pane to start an R console.
Please refer to section 9.21 Integration with Tmux in the documentation.
You need to put the following in your vimrc:
let R_in_buffer = 0
let R_applescript = 0
let R_tmux_split = 1
Or just use NeoVim
NeoVim has a builtin terminal, which actually just works with the Nvim-R plugin.

Resources