Sublime Text is a closed source expansible text editor, which I use for several tasks. I have shown how to use it as a processor for technical texts, a Read Eval Print Loop (REPL) for the Swift Programming Language. Below I will explain how to do the latter with R as well.
Launch the command palette, with Ctrl + Shift + P
and type Install Packages
. Now search for SublimeREPL
and install it.
In Preferences > Keybindings
add the following to the user's option:
[
// Modified Sublime-REPL keybindings for an "R-friendly" set of shortcuts.
// Copy and paste this text into the Key Bindings - User (under Preferences menu).
// For more information, see http://tomschenkjr.net/2012/05/17/using-sublime-text-2-for-r/
// Executes a selection of text in REPL, latter only displays code and does not execute
{ "keys": ["ctrl+shift+r"], "command": "repl_transfer_current", "args": {"scope": "selection"}},
// Executes the entire file (build) in REPL, latter only displays code and does not execute
{ "keys": ["ctrl + f7"], "command": "repl_transfer_current", "args": {"scope": "file"}},
// Executes line(s) of text in REPL terminal, latter only displays code and does not execute
{ "keys": ["ctrl+alt+r"], "command": "repl_transfer_current", "args": {"scope": "lines"}},
// Executes a block (e.g., a custom function) of text in REPL terminal, latter only displays code and does not execute
{ "keys": ["ctrl+shift+alt+r"], "command": "repl_transfer_current", "args": {"scope": "block"}},
]
One of the main features from Rstudio is the object list, this can be achieved in the REPL by creating a function lsos()
, which will list the objects loaded in the memory. In order for it to work create a snippet file on .config/sublime-text-3/Packages/User
directory with the following content:
<snippet>
<content><![CDATA[
# improved list of objects
.ls.objects <- function (pos = 1, pattern, order.by,
decreasing=FALSE, head=FALSE, n=5) {
napply <- function(names, fn) sapply(names, function(x)
fn(get(x, pos = pos)))
names <- ls(pos = pos, pattern = pattern)
obj.class <- napply(names, function(x) as.character(class(x))[1])
obj.mode <- napply(names, mode)
obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
obj.prettysize <- napply(names, function(x) {
format(utils::object.size(x), units = "auto") })
obj.size <- napply(names, object.size)
obj.dim <- t(napply(names, function(x)
as.numeric(dim(x))[1:2]))
vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
obj.dim[vec, 1] <- napply(names, length)[vec]
out <- data.frame(obj.type, obj.size, obj.prettysize, obj.dim)
names(out) <- c("Type", "Size", "PrettySize", "Length/Rows", "Columns")
if (!missing(order.by))
out <- out[order(out[[order.by]], decreasing=decreasing), ]
if (head)
out <- head(out, n)
out
}
# shorthand
lsos <- function(..., n=10) {
gc()
.ls.objects(..., order.by="Size", decreasing=TRUE, head=TRUE, n=n)
}
lsos()
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>lsos</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
</snippet>
Now, whenever you need to check which objects are loaded into the memory, run lsos()
inside the REPL.
In View > Groups
add a new Group:

The window should now look like:

Open the command palette again and look for Sublime REPL R
, press enter and a R environment will be opened in the empty group.

Now you can pass an entire line from the left group to the R environment with Ctrl + Alt + r
and a selection with code with Shift + Ctrl + r
, the code will be passed on to the right group an will be immediately evaluated.
In order to get a knitr building system inside Sublime text, install the knitr
package and edit the knitr-Markdown-sublime.build
so it reads:
{
"selector": "text.html.markdown.knitr",
"env": { "LANG": "en_US.UTF-8" },
"cmd": [ "Rscript -e \"out= rmarkdown::render(input = '$file', output_dir='/tmp/');system(paste('xdg-open ',out))\"" ],
"shell": true,
"variants":
[
{
"name": "Run",
"working_dir": "$file_path",
"shell_cmd": "Rscript -e \"rmarkdown::render(input = '$file')\""
},
{
"name": "Bookdown",
"working_dir": "$file_path",
"shell_cmd": "cd $project_path; Rscript -e \"bookdown::render_book('index.Rmd', output_dir='_book')\""
},
{
"name": "Blogdown",
"working_dir": "$project_path",
"shell_cmd": "Rscript -e \"rmarkdown::render_site('$project_path',encoding = 'UTF-8')\""
}
]
}