Tips for Rmarkdown

by Qianyu Hang

started 2018/1/5 and updated 2018-12-26



Introduction

I think some people are transfering from Microsoft Word to R Markdown/R Bookdown to write a nice report. R Markdown is reproducible package in R Studio. People can know exactly what you did and repeat what you did! Nice, huh? So let’s get it started!

Optimize figures output

Rmarkdown is a really useful system for combining text, output and graphics generated by R/Rstudio into a single document. Figures, in particular, are a powerful means of communication in a report or your personal blog. To maximize the power of those images, Zev Ross has created a comprehensive list of list of tips and tricks for working with images in R markdown document. Also, 10 tips for making your R graphics look their best is also recommended in particular:

  • Use PNG for R graphics output, and use a high-resolution if you plan to print. Don’t ever use JPG for R graphics output.
  • Think about the aspect ratio of your graphics.
  • Remove unused whitespace around your graphics (more of an issue for base R grahics; ggplot2 handles this pretty well already).

Optimize tables output

I am also new to tables though. I previously used a Markdown Tables Generator to generate tables which are really poor. I talked with my advisor to see if there is any solution to help with it. And here is a website you can refer to Create Awesome HTML Table with knitr::kable and kableExtra.

Optimize color setting in R Studio

Another thing i wanna mention is color! You will definitely think about color when you try to make your audiences understood as quick as possible or when you are ready to submit your first manuscript to the journal. You can use hexadecimal codes to represent color. Here are two websites that i found useful to me.

Make a nice report

Still learning, but refered a website R markdown and Knitr.

For writing journal papers in Rmarkdown, i benefited a lot reading this blog. To insert references, i use the package citr alongside a .bib file that you synchronised to your library. Then click Addins button, there is an option to insert citation. Do it, and choose whichever reference you wanna cite.

For me, i am using rticles package installed in R Studio and elsarticle package installed in MiKTek to write a journal manuscript. And create a template markdown file for various formats including:

  • JSS articles
  • R journal articles
  • CTeX documents
  • ACM articles
  • ACS articles
  • AMS articles
  • PeerJ articles
  • Elsevier journal submissions
  • AEA journal submissions
  • IEEE Transaction journal submission
  • Statistics in Medicine journal submission
  • Royal Society Open Science journal submission
  • Bulletin de I’AMQ journal submission
  • MDPI journal submission
  • Springer journal submission

But, there is a but… I did not find Springer journal submission.

Updates: June 1, 2018

Currently, i found it is not perfect to use elsarticle template from rticles package. Through a few days’ work, i found an awesome website that can realize what i wanted. But there are several typos in the manuscript.Rmd file. I did several changes to write my own paper.

First, here is a manuscript repo which i folked. You need to install the package and gain access to the materials.

install.packages("devtools")
library("devtools")
install_github("jhollist/manuscriptPackage",build_vignettes=TRUE)
library("manuscriptPackage")

Now you are free to play with it.

Some tips: it is beneficial to use \clearpage syntax after abstract before submission (some journals may not require though). You could use \tableofcontents to construct a table of contents on the title page. If you wanna start page numbering from the second page in R markdown, you could add \pagenumbering{gobble} just before the second page and \pagenumbering{arabic} after the title page. In order to use Times New Roman font both for the text and the math in the manuscript, you could use usepackage: {newtxtext, newtxmath} in the preamble to specify it. The netxt package was installed in MiKTeK. If you want to reference your figure, you can use \@ref(fig:ancient-aliens).

Updates: June 4, 2018

After talking with Wenlong, i still haven’t solved “table” question in LaTeX through Rmarkdown. I am not familiar with LaTex. So i do not want to put much energy to learning coding rather than considering my research. I will put “writing an academic paper using rmarkdown” away now.

Shortcuts

Switch to another file in R studio - Control + F11/f12

Symbols

You would benefit from A table so much. Do not forget to add $...$ between LATEX codes if you wanna insert a symbol. And do not forget to add $$...$$ if you wanna insert a mathematical equation/symbol. Also, refer to the tracer.Rmd file for an example.

My experience till now

  1. Always do not use system code of rmarkdown to import images, otherwise your rmarkdown will suddenly break down one day. I encounted this problem recently. I, at first, thought it might be because i used wrong codes making it die. Then I checked all codes letter by letter which seems right though. And then i realized i imported a lot of images in Rmd files using ![image](img/path) system code which must be too heavy for a “small” package. Like i recommended before, list of tips and tricks for working with images in R markdown document solved the problem and made images nicer! I used include_graphics function in knitr package for local downloaded images. For figures produced by R, you will need to choose another way to make it.

  2. If you save your Rmd files in Google Drive, you might want to pause sync when you are working with Rmd files. Otherwise the error might frequently pop up when you try to make a change in the file. Just do not forget resuming Google Drive when you are done (I did once).

  3. Some researchers use Rmarkdown or Rbookdown to write journal articles or reports. But sometimes i asked myself why do people want to use Rmarkdown or RBookdown to write journal articles or reports. Consistant format with pieces of codes? Or easy to switch the format by just changing codes? One think to notice is that advisors/supervisors will revise/edit produced articles or reports in Microsoft Word. So why not just write directly in Word? Probably you have a better answer to this question.