For the past 6-7 years I have been using TextMate 2 as my text editor which I’ve found useful for R code, bash, Markdown, etc. You could also look into Sublime Text or use RStudio (post about this setup coming soon).
Sometimes students are interested in this setup, which is what I’ll document here. Though I want to highlight that you can get a very similar setup using other tools. Note that this setup only works for Mac computers.
First, install TextMate 2 for free. TextMate allows users to contribute bundles which are a set of tools that enhance the editor. For example, if you want to quickly insert an R code chunk in a
.Rmd file you can add a command for it inside a bundle. You can also use a bundle to get the editor to recognize R code inside an R markdown code chunk. Probably the easiest way to get jump-started is to copy my exact setup.
Change some preferences
So next, go to the preferences menu
and under bundle, choose the R bundle as shown below.
As you can see, it hasn’t been updated in a while. I have made a few edits myself here and there which I’ll describe in the next section.
You should also enable running TextMate from the terminal.
Finally, here are my main file preferences: I want my files to be
.R files by default and to use linux line endings to avoid issues later on.
Adding bundles from git
TextMate allows you to install bundles by adding the bundle files in a specific folder. The bundle files are most likely in a GitHub repository, so you just need to clone (download) them to where TextMate expect them to be.
- https://github.com/lcolladotor/r.tmbundle for R and sending code to be evaluated in an iTerm2 terminal (setup explained later)
- https://github.com/noniq/Merge-Markers.tmbundle for git merging
- https://github.com/elia/base16-themes.tmbundle for theme colors
- https://github.com/lcolladotor/criticmarkup.tmbundle for CriticMarkup in Markdown files
- https://github.com/lcolladotor/knitr.tmbundle for
- https://github.com/lcolladotor/markdown-redcarpet.tmbundle for basically running
rmarkdown::render()on the document at hand and previewing it live (if it’s an html doc). It also makes it so that R code inside code chunks will be recognized as such, enabling all the R code shortcuts.
## Go to main bundle directory cd ~/Library/Application\ Support/TextMate/ ## Download Leonardo's bundles (he uses the leo branch) ## For R, sendind code to iTerm2 git clone https://github.com/lcolladotor/r.tmbundle.git ## For merging git clone https://github.com/noniq/Merge-Markers.tmbundle.git ## For more color themes git clone https://github.com/elia/base16-themes.tmbundle.git ## For commenting Markdown files git clone https://github.com/lcolladotor/criticmarkup.tmbundle.git ## For knitr::knit() git clone https://github.com/lcolladotor/knitr.tmbundle.git ## For rmarkdown::render() git clone https://github.com/lcolladotor/markdown-redcarpet.tmbundle.git
As you can see, these bundles help adapt TextMate2 for working with R files of different flavors. But it’s not beginner friendly, hence the upcoming blog post about using RStudio.
Some feature highlights
One of the features that I really like from TextMate is searching/replacing (you can use regex) across all the files and sub-directories of a given directory. It’s very useful when trying to fix a common typo across different files or finding all the places where a function/argument was used. The latter one is handy when you are looking at someone else’s code. It’s basically like searching inside a GitHub repository: example, search
baseurl in all of blogdown for finding the code that reads it from a config file, which I did for this PR.
You can also comment all the lines of code you have selected fairly easily using the
I’ve also used the
Gist bundles, though not as frequently. Also, TextMate automatically spell checks for you and knows to ignore R markdown code chunks.
Evaluating code in R console or iTerm2
If you download and install the iTerm2 terminal, you can configure TextMate to evaluate the code in that terminal. The particular code I have for doing this is in the
leo branch of the following repo https://github.com/lcolladotor/r.tmbundle/commits/leo. In total I use 3 different keyboard shortcuts depending on whether I want to evaluate the code:
- in an R console window;
- in an R console window after automatically running
setwd()to the directory that contains the files I’m looking at;
- to the iTerm2 terminal, which is useful when working with a computing cluster.
getwd() ## run in terminal with cmd + enter shortcut getwd() ## run in R console using backtip (`) shortcut getwd() ## run in R console using cmd + R, runs setwd() first
If I’m working with an R Markdown file (
.Rmd extension), I frequently use the
alt (option) + t shortcut for running
rmarkdown::render() and viewing the output file.
For example, if I’m working with the
recount-brain/index.Rmd file (you can get it here), my setup allows me to run all the R shortcuts. That’s because it recognizes the R code chunk syntax and uses the
Anyway, after using
alt (option) + t TextMate shows me the final html.
A lot of the bundles in TextMate are from the days when we run
Sweave(). So they work well with
.Rnw files and all the like. I did modify one of them to use
knitr::knit() instead of
If you enable the terminal preferences you can now use the
mate command in any directory in your laptop. TextMate will open and show you all the tabs of files you had last opened in that same directory. This behavior is also a part of the
.Rproj files with RStudio.
The command I really like is
rmate because it enables me to remotely open a file from the cluster in TextMate, which combined with the evaluate in iTerm2 command makes it easy to work. Basically, I power up an iTerm2 terminal, log into the cluster, navigate to the directory that contains the files I’m working with, and then open them remotely with
rmate takes a bit of work but it’s definitely worth it.
- In the cluster, install
rmatefollowing the instructions at https://github.com/textmate/rmate
- Find a port that works for doing the forwarding. The default one will likely be taken already by another user. Find more about this here. There they use
ssh -R 8080:localhost:80 public.example.comfor testing. Sadly, I don’t know of a quick and easy way to find a port for you to use :/
- Edit your cluster’s
~/.bashrcfile with the port information. Mine includes these lines where
someSecretPortNumberis replaced by my port number.
## rmate port # https://github.com/textmate/rmate export RMATE_PORT="someSecretPortNumber"
- Edit your laptop’s
~/.ssh/configfile so you don’t have to specify the port every time you
## Will work later as (aka, less typing): ## ssh j ## ssh cluster Host j cluster User yourUsernameGoesHere Hostname jhpce01.jhsph.edu RemoteForward someSecretPortNumber localhost:someSecretPortNumber ForwardX11 yes ForwardX11Trusted yes
- Edit your cluster’s
~/.ssh/configfile so the port gets forwarded also when you access a compute node with
qrsh. All of JHPCE’s compute nodes are named
computesomething, so we can take advantage of that in the config file.
# For rmate Host compute* RemoteForward someSecretPortNumber localhost:someSecretPortNumber
Once you do these steps,
rmate should work on a new terminal window.
I don’t remember right now if I manually edited the TextMate variables, but just in case, here’s the info.
This blog post was made possible thanks to:
## Session info ----------------------------------------------------------------------------------------------------------
## setting value ## version R version 3.4.3 (2017-11-30) ## system x86_64, darwin15.6.0 ## ui X11 ## language (EN) ## collate en_US.UTF-8 ## tz America/New_York ## date 2018-03-11
## Packages --------------------------------------------------------------------------------------------------------------
## package * version date source ## backports 1.1.2 2017-12-13 CRAN (R 3.4.3) ## base * 3.4.3 2017-12-07 local ## bibtex 0.4.2 2017-06-30 CRAN (R 3.4.1) ## BiocStyle * 2.6.1 2017-11-30 Bioconductor ## blogdown 0.5.10 2018-03-10 Github (lcolladotor/[email protected]) ## bookdown 0.7 2018-02-18 cran (@0.7) ## colorout * 1.2-0 2018-02-19 Github (jalvesaq/[email protected]) ## compiler 3.4.3 2017-12-07 local ## datasets * 3.4.3 2017-12-07 local ## devtools * 1.13.5 2018-02-18 CRAN (R 3.4.3) ## digest 0.6.15 2018-01-28 CRAN (R 3.4.3) ## evaluate 0.10.1 2017-06-24 CRAN (R 3.4.1) ## graphics * 3.4.3 2017-12-07 local ## grDevices * 3.4.3 2017-12-07 local ## htmltools 0.3.6 2017-04-28 CRAN (R 3.4.0) ## httr 1.3.1 2017-08-20 CRAN (R 3.4.1) ## jsonlite 1.5 2017-06-01 CRAN (R 3.4.0) ## knitcitations * 1.0.8 2017-07-04 CRAN (R 3.4.1) ## knitr 1.20 2018-02-20 cran (@1.20) ## lubridate 1.7.3 2018-02-27 CRAN (R 3.4.3) ## magrittr 1.5 2014-11-22 CRAN (R 3.4.0) ## memoise 1.1.0 2017-04-21 CRAN (R 3.4.0) ## methods * 3.4.3 2017-12-07 local ## plyr 1.8.4 2016-06-08 CRAN (R 3.4.0) ## R6 2.2.2 2017-06-17 CRAN (R 3.4.0) ## Rcpp 0.12.15 2018-01-20 CRAN (R 3.4.3) ## RefManageR 0.14.20 2017-08-17 CRAN (R 3.4.1) ## rmarkdown 1.9 2018-03-01 cran (@1.9) ## rprojroot 1.3-2 2018-01-03 CRAN (R 3.4.3) ## stats * 3.4.3 2017-12-07 local ## stringi 1.1.6 2017-11-17 CRAN (R 3.4.2) ## stringr 1.3.0 2018-02-19 cran (@1.3.0) ## tools 3.4.3 2017-12-07 local ## utils * 3.4.3 2017-12-07 local ## withr 2.1.1 2017-12-19 CRAN (R 3.4.3) ## xfun 0.1 2018-01-22 CRAN (R 3.4.3) ## xml2 1.2.0 2018-01-24 CRAN (R 3.4.3) ## yaml 2.1.18 2018-03-08 cran (@2.1.18)