<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>rmarkdown on plantarum.ca</title>
    <link>https://plantarum.ca/tags/rmarkdown/</link>
    <description>Recent content in rmarkdown on plantarum.ca</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Sun, 03 Oct 2021 00:00:00 +0000</lastBuildDate>
    
        <atom:link href="https://plantarum.ca/tags/rmarkdown/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Emacs for Bioinformatics #4: RMarkdown</title>
      <link>https://plantarum.ca/2021/10/03/emacs-tutorial-rmarkdown/</link>
      <pubDate>Sun, 03 Oct 2021 00:00:00 +0000</pubDate>
      
      <guid>https://plantarum.ca/2021/10/03/emacs-tutorial-rmarkdown/</guid>
      <description>



&lt;p&gt;This is part four in my series of Emacs tutorials aimed at bioinformatics
(and other scientific analysis) workflows. See the rest on my
&lt;a href=&#34;https://plantarum.ca/tutorials/&#34;&gt;tutorials&lt;/a&gt; page.&lt;/p&gt;
&lt;p&gt;Emacs provides full support for editing
&lt;a href=&#34;https://rmarkdown.rstudio.com/&#34;&gt;RMarkdown&lt;/a&gt; documents. RMarkdown has
extensive documentation, both at the previous RStudio link, and several
free online books by Xie et al. (notably &lt;a href=&#34;https://bookdown.org/yihui/rmarkdown/&#34;&gt;R Markdown: The Definitive
Guide&lt;/a&gt;, but also several others
listed on &lt;a href=&#34;https://bookdown.org/yihui/rmarkdown/&#34;&gt;Yihui Xie’s Bookdown
page&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Most of these references assume you are using the
&lt;a href=&#34;https://rstudio.com/&#34;&gt;RStudio&lt;/a&gt; development environment. The purpose of
this tutorial is to get you started editing RMarkdown documents in Emacs.&lt;/p&gt;
&lt;div id=&#34;installation&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Installation&lt;/h1&gt;
&lt;div id=&#34;prerequisites&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;You need to have &lt;a href=&#34;https://www.r-project.org/&#34;&gt;R&lt;/a&gt; installed, of course. You
will also need &lt;a href=&#34;https://pandoc.org/&#34;&gt;Pandoc&lt;/a&gt; in order to take full
advantage of all the output options available. If you want to create PDF
documents, you’ll need &lt;a href=&#34;https://www.latex-project.org/&#34;&gt;LaTeX&lt;/a&gt; as well.&lt;/p&gt;
&lt;p&gt;All three of these programs are provided in the package repositories for
most major Linux distributions. See the links above for instructions for
installing on Windows or Apple computers.&lt;/p&gt;
&lt;p&gt;You will also need to install the &lt;code&gt;rmarkdown&lt;/code&gt; R package. You can do this
from within R via:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;install.packages(&amp;quot;rmarkdown&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will also install the other R requirements, notably the
&lt;a href=&#34;https://yihui.org/knitr/&#34;&gt;knitr&lt;/a&gt; package.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&#34;https://bookdown.org/yihui/rmarkdown/&#34;&gt;bookdown&lt;/a&gt; package provides some
more advanced citation features. I won’t discuss them in this short
tutorial, but in order to use them you need to install that package too:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;install.packages(&amp;quot;bookdown&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;emacs-packages&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Emacs Packages&lt;/h2&gt;
&lt;p&gt;We need a few additional Emacs packages to comfortably edit RMarkdown
documents. These are:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;a href=&#34;https://github.com/jrblevin/markdown-mode&#34;&gt;Markdown Mode&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;The major mode for editing files in markdown format. &lt;strong&gt;This tutorial uses
features added after 6 January 2021.&lt;/strong&gt;&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;a href=&#34;https://ess.r-project.org/&#34; title=&#34;ESS&#34;&gt;ESS&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;The collection of modes for editing R code and interacting with the R
program.&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;a href=&#34;https://polymode.github.io/&#34;&gt;poly-R (Polymode)&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;&lt;code&gt;polymode&lt;/code&gt; is a ‘glue’ mode. The &lt;code&gt;poly-R&lt;/code&gt; variant extends markdown mode
to allow us to edit embedded code snippets in R (and other languages too)&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;(&lt;code&gt;poly-R&lt;/code&gt; also supports files in &lt;code&gt;.Rnw&lt;/code&gt; format, which mix LaTeX and R
code. We won’t cover that here)&lt;/p&gt;
&lt;p&gt;&lt;code&gt;polymode&lt;/code&gt; started out as a collection of modes to support files with
different combinations of languages. As it has grown, many of those
different modes have been split out into separate packages. When we
install &lt;code&gt;poly-R&lt;/code&gt;, it will automatically install the core of the
&lt;code&gt;polymode&lt;/code&gt; system for us.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This tutorial uses features added after 29 September 2021.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As in previous tutorials, (see &lt;a href=&#34;https://plantarum.ca/2020/12/30/emacs-tutorial-03/&#34;&gt;my
blog&lt;/a&gt; or the &lt;a href=&#34;https://www.youtube.com/watch?v=So1LYzSk9o0&#34;&gt;demo on
Youtube&lt;/a&gt;), we can install all
three of these packages from &lt;a href=&#34;https://melpa.org/#/&#34;&gt;MELPA&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Once we have the required packages installed, no further configuration
should be necessary. When we next open a file with a &lt;code&gt;.Rmd&lt;/code&gt; extension,
Emacs will know to use the &lt;code&gt;poly-markdown+R-mode&lt;/code&gt; for these files. If
everything is working properly, you’ll see &lt;code&gt;Markdown PM-Rmd&lt;/code&gt; in the
modeline at the bottom of the window for these files, and &lt;code&gt;Markdown&lt;/code&gt;,
&lt;code&gt;RMarkdown&lt;/code&gt;, and &lt;code&gt;Polymode&lt;/code&gt; menus at the top of Emacs frame.&lt;/p&gt;
&lt;div id=&#34;configuration-note&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Configuration Note&lt;/h3&gt;
&lt;p&gt;Depending on how you have installed &lt;code&gt;poly-R&lt;/code&gt;, it may be loaded
automatically, or you might need to load it yourself in your config. If it
isn’t loaded automatically, you might see errors like &lt;code&gt;(void-function poly-gfm+r-mode)&lt;/code&gt; when you try to open an Rmarkdown file.&lt;/p&gt;
&lt;p&gt;You can fix this with by adding the following line to your Emacs config.
The location isn’t critical, but it’s probably most convenient to put it at
the beginning of any configuration you use for ESS/R/Markdown.&lt;/p&gt;
&lt;pre class=&#34;lisp&#34;&gt;&lt;code&gt;(require &amp;#39;poly-R)&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;github-flavoured-markdown-and-code-blocks&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Github Flavoured Markdown and Code Blocks&lt;/h3&gt;
&lt;p&gt;Markdown mode supports several different options for code blocks. To take
full advantage of the RMarkdown support provided by the &lt;code&gt;rmarkdown&lt;/code&gt; R
package, we need to use fenced code blocks, along with language strings
wrapped in braces&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. I’ll explain this in more detail below.&lt;/p&gt;
&lt;p&gt;This variant of markdown is referred to as “Github Flavoured Markdown”, and
the &lt;code&gt;markdown-mode&lt;/code&gt; package provides &lt;code&gt;gfm-mode&lt;/code&gt; with a few extra features
particular to it. Turning on &lt;code&gt;gfm-mode&lt;/code&gt; for Rmd files requires the
following line in your Emacs configuration to turn it on&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&#34;lisp&#34;&gt;&lt;code&gt;;; associate the new polymode to Rmd files:
(add-to-list &amp;#39;auto-mode-alist
             &amp;#39;(&amp;quot;\\.[rR]md\\&amp;#39;&amp;quot; . poly-gfm+r-mode))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You will also need the following line, if you want &lt;code&gt;gfm-mode&lt;/code&gt; to
automatically insert braces for code blocks (described below):&lt;/p&gt;
&lt;pre class=&#34;lisp&#34;&gt;&lt;code&gt;;; uses braces around code block language strings:
(setq markdown-code-block-braces t)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will switch you from using &lt;code&gt;poly-markdown+R-mode&lt;/code&gt; to
&lt;code&gt;poly-gfm+r-mode&lt;/code&gt;, which shows up in your mode bar as “PM-Rmd(gfm)”. It’s
nearly similar, the main differences being the support for fenced code
blocks.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;rmarkdown&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;RMarkdown&lt;/h1&gt;
&lt;div id=&#34;editing-markdown&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Editing Markdown&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://www.markdownguide.org/basic-syntax/&#34;&gt;Markdown syntax&lt;/a&gt; is designed
to be easily entered by hand, which means if you’re already familiar with
the format you can just get going. Markdown mode will provide you with
syntax highlighing automatically:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;markdown-mode.jpg&#34; alt=&#34;A Markdown Mode buffer showing syntax highlighting&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;A Markdown Mode buffer showing syntax highlighting&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Of course, there are lots of shortcuts available. You can explore the most
frequently used in the &lt;code&gt;Markdown&lt;/code&gt; menu. I’ll summarize some of the main
ones here to get you started.&lt;/p&gt;
&lt;div id=&#34;headings&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Headings&lt;/h3&gt;
&lt;p&gt;Markdown has two different kinds of headings. The “Atx” style uses &lt;code&gt;#&lt;/code&gt;
symbols at the beginning of the heading, and, optionally, also at the end:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
# Heading Level 1

## Heading Level 2 ##
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can insert these headings with the following commands:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;C-c C-s h&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;insert a heading at the same level as the previous heading.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;If the &lt;code&gt;region&lt;/code&gt; is active, the contents of the region will be used as the
header text. If &lt;code&gt;point&lt;/code&gt; is on a line with text, the line will be
converted into a header. Otherwise, an empty header will be created.&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;C-c C-s {1-9}&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;insert a heading at the specified level. i.e., &lt;code&gt;C-c C-s 3&lt;/code&gt; inserts a
third-level heading. &lt;code&gt;region&lt;/code&gt; and &lt;code&gt;point&lt;/code&gt; can be used to set the heading
text as for the previous.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;You can manipulate headings with the following commands:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;C-c &amp;lt;up&amp;gt;&lt;/code&gt; and &lt;code&gt;C-c &amp;lt;down&amp;gt;&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;move a heading and all of its content up or down in the document.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;ie., turn this:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;markdown-move1.jpg&#34; alt=&#34;Markdown headings in original order&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Markdown headings in original order&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;into this:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;markdown-move2.jpg&#34; alt=&#34;Markdown headings with subheading 2 ahead of subheading 1&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Markdown headings with subheading 2 ahead of subheading 1&lt;/p&gt;
&lt;/div&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;C-c &amp;lt;left&amp;gt;&lt;/code&gt; and &lt;code&gt;C-c &amp;lt;right&amp;gt;&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;promote or demote a heading.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;i.e., turn this:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;markdown-move1.jpg&#34; alt=&#34;Markdown headings in hierarchy&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Markdown headings in hierarchy&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;into this (and vice versa):&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;demotion.jpg&#34; alt=&#34;Markdown with subheading 2 demoted&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Markdown with subheading 2 demoted&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;If you prefer asymmetric headings (i.e., with &lt;code&gt;#&lt;/code&gt; symbols only at the
beginning of the line), you can configue this by setting the variable
&lt;code&gt;markdown-asymmetric-header&lt;/code&gt; to &lt;code&gt;t&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;lisp&#34;&gt;&lt;code&gt;;; set in your ~/.emacs or ~/.emacs.d/init.el
(setq markdown-asymmetric-header t)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alternatively, you can do this via &lt;code&gt;M-x customize-variable markdown-asymmetric-header&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Markdown mode also supports the &lt;code&gt;setext&lt;/code&gt; style headings:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
Heading Level 1
===============

Heading Level 2
---------------
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Only two levels are supported, which you can insert automatically with the
commands &lt;code&gt;C-c C-s !&lt;/code&gt; (level 1) and &lt;code&gt;C-c C-s @&lt;/code&gt; (level 2).&lt;/p&gt;
&lt;div id=&#34;heading-navigation&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;Heading Navigation&lt;/h4&gt;
&lt;p&gt;You can move from heading to heading with the following commands:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;C-c C-n&lt;/code&gt; and &lt;code&gt;C-c C-p&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;move to next and previous headings&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;C-c C-f&lt;/code&gt; and &lt;code&gt;C-c C-b&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;move forward and backward to headings at the same level&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;C-c C-u&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;move up to parent heading&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;/div&gt;
&lt;div id=&#34;heading-visibility&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;Heading Visibility&lt;/h4&gt;
&lt;p&gt;You can hide and show different sections in documents by pressing the
&lt;code&gt;&amp;lt;TAB&amp;gt;&lt;/code&gt; key with point on a heading. For example, with point on the &lt;code&gt;# Installation&lt;/code&gt; heading, when I press &lt;code&gt;&amp;lt;TAB&amp;gt;&lt;/code&gt; I move from this:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;unhidden.jpg&#34; alt=&#34;Markdown buffer with all sections visible&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Markdown buffer with all sections visible&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;to this:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;hidden.jpg&#34; alt=&#34;Markdown buffer with the Installation section hidden&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Markdown buffer with the Installation section hidden&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;This doesn’t change any of the text in your file, it only hides the parts
you don’t want to see. Repeatedly pressing the &lt;code&gt;&amp;lt;TAB&amp;gt;&lt;/code&gt; key will toggle
through the various levels of hiding and showing.&lt;/p&gt;
&lt;p&gt;If you want to toggle all the headings at once, &lt;code&gt;Shift-&amp;lt;TAB&amp;gt;&lt;/code&gt; will toggle
visibility for all headings at once. You can use this to collapse your
entire document to a table of contents:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;toc.jpg&#34; alt=&#34;A Markdown buffer with only headings visible&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;A Markdown buffer with only headings visible&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;links-and-images&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Links and Images&lt;/h3&gt;
&lt;p&gt;Inserting links is done with &lt;code&gt;C-c C-l&lt;/code&gt;. Emacs will first prompt you for the
link URL, followed by the link text, and finally the tooltip text. Only the
URL is required. To open a link from Emacs, use &lt;code&gt;C-c C-o&lt;/code&gt;, which will take
you to the webpage in your browser.&lt;/p&gt;
&lt;p&gt;Images are handled similarly, and are inserted with &lt;code&gt;C-c &amp;lt;TAB&amp;gt;&lt;/code&gt; or &lt;code&gt;C-c C-i&lt;/code&gt;. The URL can be a web resource (e.g.,
&lt;code&gt;https://my-images.ca/image1.jpg&lt;/code&gt;), or a local file (e.g.,
&lt;code&gt;./images/image1.jpg&lt;/code&gt;). The image will appear as:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;![Image Caption](image URL)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can toggle displaying the actual image in the buffer with &lt;code&gt;C-c C-x C-i&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;tables&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Tables&lt;/h3&gt;
&lt;p&gt;To insert a new table, use the command &lt;code&gt;C-c C-s t&lt;/code&gt;. You will be prompted
for the number of rows and columns, and the alignment you want. When you’re
done, you’ll have a proper markdown table ready to edit:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;|   |   |   |   |
|---|---|---|---|
|   |   |   |   |
|   |   |   |   |
|   |   |   |   |
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With point in any of the cells, you can &lt;code&gt;&amp;lt;TAB&amp;gt;&lt;/code&gt; into the next cell, or
&lt;code&gt;Shift-&amp;lt;TAB&amp;gt;&lt;/code&gt; into the previous cell. Each time you hit tab the cells will
resize automatically to accomodate your text.&lt;/p&gt;
&lt;p&gt;Additional commands are available for moving, adding and deleting rows and
columns; see the &lt;code&gt;Markdown -&amp;gt; Tables&lt;/code&gt; menu the options.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;other-markup&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Other Markup&lt;/h3&gt;
&lt;p&gt;Markdown mode also provides shortcuts for other markup elements. See the
&lt;code&gt;Markdown&lt;/code&gt; menu for some of the options. I find most of the basics (bold,
emphasis, unordered lists) are just as fast to type by hand as they are to
insert using shortcuts.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;working-with-r-code&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Working with R Code&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;rmarkdown&lt;/code&gt; uses fenced code blocks with braces around the language string. i.e.,:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    ```{R code-block-example}
    ## R code goes here!
    1 + 1
    ```&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you’ve set up &lt;code&gt;gfm-mode&lt;/code&gt; as described above, you can create one of these
code blocks with the command &lt;code&gt;markdown-insert-gfm-code-block&lt;/code&gt;, bound to
&lt;code&gt;C-c C-s C&lt;/code&gt; by default. Alternatively, simply entering three “`” characters
at the beginning of a line will call the function for you&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;. Either way,
you’ll be prompted for the language of the code block (which will be R most
of the time, but you can use others!). You can also add a label for the
code block at the prompt, and any additional options you want to use for
the chunk. You can also add options later if you change your mind.&lt;a href=&#34;#fn4&#34; class=&#34;footnote-ref&#34; id=&#34;fnref4&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once you have created a code block, &lt;code&gt;polymode&lt;/code&gt; will work its magic. You can
continue to edit the markdown portions of your document, with all the
features of &lt;code&gt;gfm-mode&lt;/code&gt;. But when point is in an R code block, you’ll be
editing it in &lt;code&gt;ESS[R]&lt;/code&gt; mode. That allows you to use all the features of
that package (see
&lt;a href=&#34;https://plantarum.ca/tutorials/emacs-tutorial-03/&#34;&gt;plantarum.ca&lt;/a&gt; for a
quick tutorial/refresher).&lt;/p&gt;
&lt;p&gt;Polymode provides some additional conveniences:&lt;/p&gt;
&lt;div id=&#34;navigation&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Navigation&lt;/h3&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;polymode-next-chunk&lt;/code&gt;/&lt;code&gt;polymode-previous-chunk&lt;/code&gt;, bound to &lt;code&gt;M-n C-n&lt;/code&gt; and &lt;code&gt;M-n C-p&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;move to the next/previous chunk. i.e., move from an RMarkdown chunk to
the next R code chunk.&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;polymode-next-chunk-same-type&lt;/code&gt;/&lt;code&gt;polymode-previous-chunk-same-type&lt;/code&gt;, bound to &lt;code&gt;M-n M-C-n&lt;/code&gt; and &lt;code&gt;M-n M-C-p&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;move to the next/previous chunk of the same type. i.e., move from one R
code chunk the next R code chunk.&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;polymode-kill-chunk&lt;/code&gt;, bound to &lt;code&gt;M-n M-k&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;kill the current chunk&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;polymode-toggle-chunk-narrowing&lt;/code&gt;, bound to &lt;code&gt;M-n C-t&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;toggle narrowing the buffer to display only the current chunk, or to
display the entire document&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;/div&gt;
&lt;div id=&#34;evaluation&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Evaluation&lt;/h3&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;polymode-eval-region-or-chunk&lt;/code&gt;, bound to &lt;code&gt;M-n v&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;evaluate all code chunks in the active region, or the chunk at point if there
is no active region&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;polymode-eval-buffer&lt;/code&gt;, bound to &lt;code&gt;M-n b&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;evaluate all code chunks in the buffer&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;polymode-eval-buffer-from-beg-to-point&lt;/code&gt;/&lt;code&gt;polymode-eval-buffer-from-point-to-end&lt;/code&gt;, bound to &lt;code&gt;M-n u&lt;/code&gt; or &lt;code&gt;M-n ↑&lt;/code&gt;, and &lt;code&gt;M-n d&lt;/code&gt; or &lt;code&gt;M-n ↓&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;evaluate all code chunks from the beginning of the buffer to point (&lt;code&gt;u&lt;/code&gt; and &lt;code&gt;↑&lt;/code&gt;), or from point to the end of the buffer (&lt;code&gt;d&lt;/code&gt; and &lt;code&gt;↓&lt;/code&gt;)&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;exporting-rmarkdown&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Exporting RMarkdown&lt;/h2&gt;
&lt;p&gt;The most important ‘convenience’ of &lt;code&gt;polymode&lt;/code&gt; is that it connects Emacs to
the programs used to export RMarkdown files to presentation formats (i.e.,
pdf, html, slides). The main function you need for this is
&lt;code&gt;polymode-export&lt;/code&gt;, bound to &lt;code&gt;M-n e&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The first time you run this, you’ll be asked which exporter you would like
to use. There are two choices, &lt;code&gt;markdown&lt;/code&gt; and &lt;code&gt;markdown-ess&lt;/code&gt;. &lt;code&gt;markdown&lt;/code&gt;
means &lt;code&gt;polymode&lt;/code&gt; will start a new, self-contained R process and compile
your file there. When compilation is finished, the process will be closed.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;markdown-ess&lt;/code&gt; will use an existing R process, or start a new one if there
isn’t an active process available. When compilation is complete, the R
process remains active. This allows you to check the values of various
objects interactively. This can be useful as you develop a new script.&lt;/p&gt;
&lt;p&gt;RMarkdown files can be compiled to produce a variety of output formats. You
will be prompted to select which one you want the first time you run the
exporter. &lt;code&gt;polymode&lt;/code&gt; remembers this setting, so you don’t get prompted a
second time. If you want to switch, say from pdf output to html, you can
reset the target via &lt;code&gt;C-u M-n e&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://github.com/jrblevin/markdown-mode/pull/581&#34;&gt;Feature added 6 January
2021&lt;/a&gt;.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://github.com/polymode/poly-R/pull/27&#34;&gt;Feature added 29 September
2021&lt;/a&gt;&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;By default; you can turn this feature off by setting the variable
&lt;code&gt;markdown-gfm-use-electric-backquote&lt;/code&gt; to nil.&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn4&#34;&gt;&lt;p&gt;I’m working on tab-completion for R chunk options, but haven’t
decided how best to set it up yet. Watch this space!&lt;a href=&#34;#fnref4&#34; class=&#34;footnote-back&#34;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Blogdown Citations: Using Zotero with RMarkdown</title>
      <link>https://plantarum.ca/2020/07/20/blogdown-citations/</link>
      <pubDate>Mon, 20 Jul 2020 00:00:00 +0000</pubDate>
      
      <guid>https://plantarum.ca/2020/07/20/blogdown-citations/</guid>
      <description>
&lt;script src=&#34;https://plantarum.ca/rmarkdown-libs/header-attrs/header-attrs.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;So far, &lt;a href=&#34;https://bookdown.org/yihui/blogdown/&#34;&gt;blogdown&lt;/a&gt; has been really nice to work with. The documentation is really good, and it covered just about everything I needed to get up and running.&lt;/p&gt;
&lt;p&gt;The only thing that isn’t in the official manual is how to include citations. I’ll run down my system here before I forget what I’ve done.&lt;/p&gt;
&lt;p&gt;First off, you need a reference database. I am a long-time user of &lt;a href=&#34;http://www.bibtex.org/&#34;&gt;BibTeX&lt;/a&gt;, and have accumulated a large database of references. However, no-one I work with uses this system, and I don’t have a compelling reason to convert anyone.&lt;/p&gt;
&lt;p&gt;So in the interests of collaboration, I’m moving to &lt;a href=&#34;https://www.zotero.org/&#34;&gt;Zotero&lt;/a&gt;. Zotero is a more conventional graphical program, with a desktop app and a web interface. With the addition of the &lt;a href=&#34;https://retorque.re/zotero-better-bibtex/&#34;&gt;Better BibTeX&lt;/a&gt; plugin, it can also be configured to automatically sync your online Zotero database with a BibTeX file on your local computer. This means I can continue to use Emacs’ BibTeX support for “cite-while-you-write” composing, while managing the actual data in a format I can easily share with colleagues.&lt;/p&gt;
&lt;div id=&#34;setup&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Setup&lt;/h1&gt;
&lt;p&gt;Here is how my current setup works:&lt;/p&gt;
&lt;p&gt;First, head over to &lt;a href=&#34;https://www.zotero.org/&#34;&gt;Zotero&lt;/a&gt;, sign up for a free account, and install the desktop app for your operating system (the big three are all supported). Next, you’ll need to install &lt;a href=&#34;https://retorque.re/zotero-better-bibtex/&#34;&gt;Better BibTeX&lt;/a&gt; plugin. You might need to restart Zotero to get everything working together.&lt;/p&gt;
&lt;p&gt;Now you can get Better BibTeX configured. Open up the Zotero desktop app, and follow the menus: &lt;code&gt;Edit -&amp;gt; Preferences -&amp;gt; Better Bibtex&lt;/code&gt;. Here you can set the citation key format. I use &lt;code&gt;[authEtAl]_[year]&lt;/code&gt;, which creates keys that look like &lt;code&gt;Smith_2002&lt;/code&gt;, &lt;code&gt;SouleMills_1998&lt;/code&gt;, and &lt;code&gt;BrookEtAl_2002&lt;/code&gt;. There are a lot of options, described in the &lt;a href=&#34;https://retorque.re/zotero-better-bibtex/citing/#configurable-citekey-generator&#34;&gt;Better BibTeX manual&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now you’re ready to start adding references to your database, if you haven’t already. Zotero has &lt;a href=&#34;https://www.zotero.org/download/connectors&#34;&gt;&lt;code&gt;Connectors&lt;/code&gt;&lt;/a&gt; that add a button to your browser, so you can add a reference to your library with a single click. You can also import from other formats.&lt;/p&gt;
&lt;p&gt;Once you’ve got some references in your library, you need to export it to a file, so it will be accessible when you compile your RMarkdown file. To do this, left-click on the library you want to export in the left pane of the Zotero window. Select &lt;code&gt;Export Library&lt;/code&gt;, and be sure to check the &lt;code&gt;Keep Updated&lt;/code&gt; box. Then select the format &lt;code&gt;Better CSL JSON&lt;/code&gt;. Use the file browser to pick a convenient location, and you’re done.&lt;/p&gt;
&lt;p&gt;By ticking the &lt;code&gt;Keep Updated&lt;/code&gt; box, Zotero will automatically update this file when you make any changes to your library. That means you don’t ever need to export it again, or even open up the &lt;code&gt;json&lt;/code&gt; file.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;use&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Use&lt;/h1&gt;
&lt;p&gt;After all that, you’re ready to add citations to your posts. You need to add one line to your header, indicating where the &lt;code&gt;json&lt;/code&gt; file is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;title: &amp;quot;Blogdown Citations: Using Zotero with RMarkdown&amp;quot;
bibliography: /path/to/my/bibliography.json&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Basic citations are entered as:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th align=&#34;left&#34;&gt;Input&lt;/th&gt;
&lt;th align=&#34;left&#34;&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;&lt;code&gt;[@Smith_2002]&lt;/code&gt;&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;(Smith 2002)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;&lt;code&gt;[e.g., @Smith_2002, page 37]&lt;/code&gt;&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;(e.g., Smith 2002, page 37)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;&lt;code&gt;as claimed by @Smith_2002&lt;/code&gt;&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;as claimed by Smith (2002)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;&lt;code&gt;as Smith claimed -@Smith_2002&lt;/code&gt;&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;as Smith claimed (2002)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;At this point, when you rebuild your site with &lt;code&gt;blogdown::build_site()&lt;/code&gt; (or &lt;code&gt;site_serve&lt;/code&gt;), the processor will recognize the citations, look them up in your bibliography file, and replace them with properly formatted citations. The reference list is appended at the end of the document, as you would expect.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;emacs-specifics-cite-as-you-write&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Emacs Specifics: Cite As You Write&lt;/h1&gt;
&lt;p&gt;So far, so good. But entering citations by hand is still tedious. It would be much better to have a quick-lookup function, that would allow you to quickly pick a reference and have the citation inserted for you.&lt;/p&gt;
&lt;p&gt;There are plugins for different editors that will handle this for you. I assume &lt;a href=&#34;https://rstudio.com/&#34;&gt;RStudio&lt;/a&gt; provides something convenient, but I don’t use it so I don’t know for sure. The rest of this post will describe how I get “Cite As You Write” in Emacs.&lt;/p&gt;
&lt;p&gt;Emacs has some limited support for picking citations directly from Zotero via &lt;a href=&#34;https://github.com/egh/zotxt&#34;&gt;zotxt&lt;/a&gt; and the &lt;a href=&#34;https://github.com/egh/zotxt-emacs&#34;&gt;zotxt-emacs&lt;/a&gt; package. However, these are relatively rudimentary compared to what’s available for BibTeX. I’ve hit on a solution that lets me use my favourite BibTeX features to insert citations, while Zotero manages my database behind the scenes.&lt;/p&gt;
&lt;p&gt;To accomplish this, you need to export a second database file. The steps are identical to what we did above for the &lt;code&gt;.json&lt;/code&gt; file, but this time we’ll pick the &lt;code&gt;Better BibTeX&lt;/code&gt; format. Be sure to check the &lt;code&gt;Keep Updated&lt;/code&gt; option, as we did before. It’s convenient to save the file in the same directory as the first one, but you don’t have to.&lt;/p&gt;
&lt;p&gt;Now Zotero will keep both of these files in sync automatically. We never need to look at them directly, we can continue to use the Zotero program as our primary tool for managing our references. When we build our site, &lt;code&gt;Blogdown&lt;/code&gt; will look up our references in the &lt;code&gt;.json&lt;/code&gt; file, and while we’re writing we can use Emacs’ BibTeX support to lookup citations keys in the &lt;code&gt;.bibtex&lt;/code&gt; file. The same keys are used in both files.&lt;/p&gt;
&lt;p&gt;As I mentioned, Emacs has several options for looking up and inserting BibTeX citation keys. I’m currently using &lt;a href=&#34;https://github.com/tmalsburg/helm-bibtex&#34;&gt;ivy bibtex&lt;/a&gt;, which is provided as part of the &lt;code&gt;helm-bibtex&lt;/code&gt; package. It’s well-documented on its home page.&lt;/p&gt;
&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>
