Cat at a laptop: Scientific writing in R Markdown

How I felt when first trying to work in R Markdown.

Writing can be scary. Writing can be scary for everyone, not just us scientists. But whether or not we enjoy it, or think we’re good at it, it’s probably the best tool for communicating our findings. So removing as much pain from the process is key.

That’s why I’ve started using R Markdown for writing.

If you’re like me, the worst part about writing scientific papers is formatting. I hate it. I hate getting bogged down in font size, citation style, line numbers–all that stuff. Not only does it take me forever to get just right, but it gives me so much room to mess up stuff that isn’t based in content. If I’m spending time fighting with format, that’s time away from thinking about stuff that really matters. And the idea of switching between different journals’ format style makes me want to cry. R Markdown made worrying about that a thing of the past.

But perhaps even better than the formatting convenience R Markdown provides, it makes collaboration so much easier. This is especially true when you pair R Studio with your Github account. All changes and additional files referenced are all neatly connected, and any code printout included in your paper is already sitting in your paper.

So, I’ve switched to writing in R Markdown. I’ve always worked in either Word or Google Docs, and I still will if I’m writing something that isn’t going to require a lot of coordinating; but for big projects, I’m moving on up. I’m ready to get productive.

When I first tried this new step in my workflow, I felt less than skilled. I have experience in R Studio and Markdown, but when learning anything new I feel like a cat trying to type. So here’s some important tips I’ve collected from my first time through the process to hopefully make it easier.

  1. Define and fill the space R will reference when filling in format details. Three dashes (—) start and end the referential space, so write any parameters you want to fill followed by a colon and the content you want associated with it (title: Scientific Writing in R Markdown). When you create a new .rmd file, this is already started for you. Some parameters require a little extra characters, like abstracts or authors.  You’ll also need to include which output you want (a specific journal, word doc, html, pdf, etc.). If you want to format in a specific journal style, you can look up different csl (citation style and language) codes to reference journals here. You’ll also need to install and run rticles package. The rticles package allows you to reference different journal format styles so your .rmd can knit to that format style. After you finish the referential section, begin writing your paper outside the ending three dashes.
  2. Know and use your syntax. Writing in R Markdown means you’re writing in plain text as opposed to rich text. Rich text is when you’re writing but you have all these different formatting options–italics, font, colors–all the formatting options you can see in the the GUI interface. This is what you’re working with when you’re in Word. Plain text, which is just the text characters, is what you’ll use whenever you’re working in R. In order to get things like italics, or numbered lists, or bold, you need to use certain syntax. The rich text formatting will appear after you knit. Once you get used to this, it’s snap (here’s a handy guide to syntax). Plus, it’s one less thing to distract you when you’re trying to focus on content and ideas.
  3. Understand citations. Probably my single favorite thing about R Markdown is the ease with which I can include citations. It took me a minute to figure out the steps, but once I did, I never want to type out a citation or use a Word plugin again. All you have to do is export whichever papers you could possibly want to cite from your reference manager (I use Mendeley) into a .bib file. Notice what your citation key is. For Mendeley, it automatically formats your key to be author and year (@Lemon2018). After you create this, make sure your bibliography reference in your .rmd is your new .bib file. If you know your citation key, all you need to add a parenthetical citation is include [@author]. For example, you might type: “A cat like to be scratched behind its ears [@Lemon2018]”. This will automatically populate the entire citation at the end of the document. If you want to include multiple citations in one parenthetical, simply separate the keys with a semi-colon [@Lemon2017;@Lemon2014].
  4. Code! Don’t forget you’re writing in R Studio, so being able to directly code is a huge advantage of working in R Markdown. You can include any figures or tables you would in R Studio, just insert a new chunk. For tables, I recommend the kable function in the knitr package which creates an attractive table from a dataframe you already have. Just be sure to include “include=FALSE” at the beginning of your chunk so you only see the outputs of your code. Here’s a video that shows side-by-side screens of coding/writing in Markdown and how the code will look after knitting.

For me, it was a steep learning curve to make the transition from rich text programs to R markdown. In this post, I included some introductory tips for switching to R Markdown. There are lots of more advanced options with R Markdown, but for this post I wanted to focus on the challenges that I  struggled with while writing my first paper in an .rmd file. This doesn’t include steps that I found intuitive, or questions that are associated with learning to code in R, or tricks that are so advanced that I didn’t run into them. But I found the answers to most of my questions by scouring the web, so even if I didn’t answer something here, the answer is probably out there. Hopefully, the tips I devised can help an intermediate R coder get the most out of their work with R Markdown.

 

Congratulations to Dr. Filazzola

Completing facilitation research and being a facilitator through collaborative mentoring and supporting team science, Alex defended his PhD yesterday. Congratulations!

 

The subes and all of us will miss your open spirit!
So tempting to post a different pic :)!

 

 

 

Posted in fun