This note explains how to use an application launcher along with text expansion and shell commands to accomplish a few specific tasks that can be useful to R users.
This note assumes that you are equipped with an application launcher that supports text expansion and can process shell commands. On Mac OS X, I recommend Alfred, because the little money that you will need to pay for its Powerpack is very well compensated by the tons of useful features (including shell support) that it brings to the application.
This note also assumes that you are equipped with a text expander, which is a small utility that will transform keyboard inputs into longer ones, thereby saving you the hassle of memorising and typing shell commands. On Mac OS X, I recommend aText, because it is cheap, fast and stable.
If you are going to use the Alfred + aText combination that I recommend, remember to set the “Preferences → Appearance → Options → Focusing” setting in Alfred to “Compatibility mode” in order to enable aText in the Alfred prompt, and to write your aText shortcuts as plain text content.
Below are three example cases of using Alfred and aText to perform some specific tasks that can be useful to the R user.
Find open network ports
There are a variety of situations in which listing open network ports is useful to the R user, such as using Shiny or any other software that sends its results from R to a specific local network port.
Since the definition of files in UNIX includes network connections, listing only open network sockets is as simple as passing a few options to
lsof and then subsetting the result to connections that are currently being “listened to” (i.e. awaiting connections):
> lsof -i -P | grep -i "listen"
At that stage, the command has become too long to memorize if you are not a frequent shell user. This is where text expansion becomes handy, as the entire command above (including the trailing
> character) can be assigned to an abbrevation like
lsof. Typing those four letters in Alfred will now return results that might include something like
rsession 21309 fr 5u IPv4 0xbb252b45e6a7134d 0t0 TCP localhost:42136 (LISTEN) rsession 21309 fr 19u IPv4 0xbb252b45d9f58b35 0t0 TCP localhost:18323 (LISTEN)
… which shows the specific ports that are being used by my R session.
If you are planning to use
lsof for other tasks than the one described above, you will naturally want to use another abbreviation than
lsof in your text expander.
Launch a local Web server
If your R code produces results that require PHP to be viewed, you will need to run a PHP server on your local computer. An easy way to do this is to install a “solution stack package” such as XAMPP, which will run an Apache HTTP server with Perl, PHP and a few other things on it.
On Mac OS X, one way to launch or stop XAMPP is to use the following commands:
> sudo /Applications/XAMPP/xamppfiles/xampp start > sudo /Applications/XAMPP/xamppfiles/xampp stop
The first thing you will want to do is to create a symlink for the XAMPP executable into a directory that is part of your
PATH, such as the
/usr/local/bin directory, where Homebrew, for instance, also creates symlinks for the packages that it installs. Use the following command to create that symlink:
ln -s /Applications/XAMPP/xamppfiles/xampp /usr/local/bin
The two commands above are now executable through less text:
> sudo xampp start > sudo xampp stop
An easy way to execute any of these commands is then to assign their common part (
> sudo xampp) to the
xampp abbreviation in your text expander, so that in your application launcher, typing
xampp start or
xampp stop will either launch or stop your local server.
Extension: Quicker Git commands
The logic followed in this note can be applied to many other use cases, such as shortening Git commands.
Git and GitHub allow to track revisions on any content, especially code or any other form of plain text, but also many other forms of media like images or maps. There are excellent tutorials to learn them, such as this short guide and its summary of most useful Git commands, both by Karl Broman.
Even the excellent GitHub desktop client cannot cover the thousands of different functionalities that Git offers; as a consequence, it is necessary to use the command line to use some parts of Git, such as Git submodules or Git Large File Storage, which is supported by GitHub but only minimally configurable through its desktop client.
Any part of your Git workflow can be shortened for quicker use. For instance, if you are tracking a repository that comes with release tags, it might be helpful to review the history of the commits to the repository with these tags apparent. The Git command to do this (i.e. “visualize the version tree”) is
> git log --pretty=oneline --graph --decorate --all
… or any text shortcut that expands to it, such as