Day36: Interfaces

Posted by csiu on April 1, 2017 | with: 100daysofcode, Setup

For the beginning of the new month, I thought I would share how I’m doing my work in writing these blog posts. At the end of the post, I also describe how to use Python and R together in a Jupyter Notebook.

Jupyter Notebooks

I started out coding in Python with Jupyter Notebooks and find them excellent for exploratory analysis. You can run local chunks of code and view the corresponding output, making debugging code and viewing figures easy. In addition, you are also able to describe what it is you are doing with formatted text and links in Markdown.

Jupyter Notebooks are not exclusive to Python. You can install different kernels for programming in other languages. For instance, “Jupyter and Conda for R” (Christine Doig, 2015) describes how you can use Jupyter Notebooks to run R.

Spyder

For producing whole Python scripts, I find Spyder (the Scientific PYthon Development EnviRonment) to be very nice. The thing that I like best is the available autocompletion of code feature.

In comparison to Jupyter Notebooks, producing Python scripts is better for programs you intend to rerun, possible with different parameters or different inputs.

RStudio

For writing R, I tend to use RStudio. Again, I like the available code autocompletion and being able to run local chunks of code for inspecting variables and refining figures.

Much like Jupyter Notebooks, I like to create R Markdown documents “to weave together narrative text and code to produce elegantly formatted output.”

GitUp

From one of my past mentors, I remember them saying that good commits address one objective. But oftentimes, I make a bunch of edits (for different objectives) without committing the changes in the code. I could git add --interactive to interactively stage the lines that address a single objective, but this is tedious. With GitUp, this adding of specific lines is trivial.

Jekyll

Jekyll is used to create this blog. This means the posts are all written in markdown. Therefore, when I finish writing a Jupyter Notebook, I would jupyter-nbconvert --to markdown and use that as a basis for a blog post. Or, if I was writing a R Markdown document, I would “knit” to produce a markdown file as a basis for a blog post.


Integrating R and Python in Jupyter Notebooks

An example of a Jupyter Notebook using both Python and R is found here.

1. Install rpy2

conda install -c r rpy2

2. Load R extension by adding the following to a cell in the Jupyter Notebook

%load_ext rpy2.ipython

3. Use R by adding %%R to the top of a cell in the Jupyter Notebook