{"id":4209,"date":"2018-07-12T10:04:10","date_gmt":"2018-07-12T09:04:10","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=4209"},"modified":"2018-08-02T13:17:56","modified_gmt":"2018-08-02T12:17:56","slug":"introduction-to-r-markdown","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2018\/07\/introduction-to-r-markdown\/","title":{"rendered":"Introduction to R Markdown"},"content":{"rendered":"<p>Two of our esteemed OPIGlets presented a workshop on collaborative research using Jupyter Notebook this week at <a href=\"https:\/\/www.iscb.org\/ismb2018\">ISMB<\/a> in Chicago. Their workshop highlights the importance of finding ways to share your work conveniently and reproducibly. So on a related note, I thought I would share a brief introduction to another useful tool,\u00a0<a href=\"http:\/\/rmarkdown.rstudio.com\" target=\"_blank\" rel=\"noopener\">R Markdown<\/a> with RStudio, which I use to present updates to various supervisors and to remember what I did three months (or three days) ago. This method of sharing work is highly readable, reproducible, and narrative-driven.<\/p>\n<p>I use R for much of my data analysis and all of my visualisation, and I count the tidyverse among my most beloved friends. If you&#8217;re so inclined, it&#8217;s easy to execute python, bash, and more from within R Markdown. You also don&#8217;t need to use RStudio to use R Markdown, but that&#8217;s a whole other story.<\/p>\n<p>Starting a new markdown file in RStudio will generate a template script explaining most of what you need to know. If I showed you that then I&#8217;d be out of a blog post, but I will at least link to the <a href=\"https:\/\/www.rstudio.com\/wp-content\/uploads\/2015\/03\/rmarkdown-reference.pdf\" target=\"_blank\" rel=\"noopener\">R Markdown Reference Guide<\/a>.<\/p>\n<p>R Markdown files consist of text written in markdown, and code chunks that can be individually executed and displayed inline within RStudio. To &#8220;knit&#8221; the whole thing together, the <a href=\"https:\/\/yihui.name\/knitr\/\" target=\"_blank\" rel=\"noopener\">knitr<\/a> package is used to execute and combine code chunks, then <a href=\"http:\/\/pandoc.org\/\" target=\"_blank\" rel=\"noopener\">pandoc<\/a> converts the whole thing into an attractive document.<\/p>\n<p>Here&#8217;s an example. The metadata at the top sets up the document. I&#8217;ll be generating an html document here, but notice some other tempting examples commented out. Yes, you can use it for Latex (swoon). You can even make a Word document, but really, why would you?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-highlight=\"11\">---\r\ntitle: \"Informative Title\"\r\nauthor: \"Clare E. West\"\r\ndate: \"10\/07\/2018\"\r\noutput: html_document\r\n#output: beamer_presentation\r\n#output: pdf_document\r\n---\r\n\r\n```{r setup, include=FALSE}\r\nknitr::opts_chunk$set(echo = TRUE)\r\nlibrary(knitr)\r\nlibrary(ggplot2)\r\nlibrary(tidyr)\r\nlibrary(dplyr)\r\n```\r\n\r\n## Big Title\r\n###\u00a0Smaller title\r\n\r\nR Markdown scripts have the extension .Rmd\r\n\r\nR Markdown is __so__ *fun*. You can read all about it [here](https:\/\/www.rstudio.com\/wp-content\/uploads\/2015\/03\/rmarkdown-reference.pdf).\r\n\r\n```{r}\r\nprint(\"Hello world\")\r\n```<\/pre>\n<p>Notice that chunks are enclosed within three backticks, with the language and options in braces. Single commands can be executed inline using single backticks.<\/p>\n<p>As highlighted in the example above, global options are set like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">knitr::opts_chunk$set(echo = TRUE)<\/pre>\n<p>&#8220;echo=TRUE&#8221; means that the code in each chunk is displayed in the final product; this is useful to show collaborators (or your future self) exactly how you did something. Change this option (&#8220;echo = FALSE&#8221;) globally or in individual blocks to prevent code from printing. This is useful to hide uninteresting commands, or when presenting to people who don&#8217;t have the time or inclination to read your code (hard to imagine). Notice I&#8217;ve also used &#8220;include = FALSE&#8221;\u00a0 for the library-loading code chunk, which means evaluate but don&#8217;t include in the output. Another useful option is &#8220;eval = FALSE&#8221;, which means don&#8217;t even run this chunk.<\/p>\n<p>So let&#8217;s see what that looks like when we render it:<\/p>\n<div id=\"attachment_4213\" style=\"width: 566px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.29.53.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" aria-describedby=\"caption-attachment-4213\" loading=\"lazy\" class=\"wp-image-4213 size-full\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.29.53.png?resize=556%2C384&#038;ssl=1\" alt=\"\" width=\"556\" height=\"384\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.29.53.png?w=556&amp;ssl=1 556w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.29.53.png?resize=300%2C207&amp;ssl=1 300w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/a><p id=\"caption-attachment-4213\" class=\"wp-caption-text\">The above example output as HTML<\/p><\/div>\n<div id=\"attachment_4214\" style=\"width: 702px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.28.57.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" aria-describedby=\"caption-attachment-4214\" loading=\"lazy\" class=\"wp-image-4214 size-full\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.28.57.png?resize=625%2C282&#038;ssl=1\" alt=\"\" width=\"625\" height=\"282\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.28.57.png?w=692&amp;ssl=1 692w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.28.57.png?resize=300%2C135&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.28.57.png?resize=624%2C281&amp;ssl=1 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><p id=\"caption-attachment-4214\" class=\"wp-caption-text\">The above example output as Latex<\/p><\/div>\n<p>Plots generated in code chunks or images from other sources can be embedded. Set the width in the options. &#8220;fig.width&#8221; sets the width (in inches) of the figure generated, while &#8220;out.width&#8221; scales the image in the final documents, for which the units will depend on the document type.\u00a0Within RStudio, these are previewed inline below the code chunk.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">## Including plots\/images\r\n```{r fig.width = 4, fig.height = 3, out.width = \"400px\", echo=FALSE}\r\nt  %&gt;% group_by(Tour, Winner, N, Tournament) %&gt;% filter(WRank &lt;= 20) %&gt;% summarise(WPts = max(WPts))  %&gt;% ggplot(aes(x=N, y=WPts, group=Winner, colour=(Winner==\"Murray A.\"))) + geom_point() + geom_line() + labs(x=\"Tournament Number\",y=\"Ranking Points\") + scale_colour_discrete(\"\",labels=c(\"Not Andy Murray\", \"Andy Murray\")) + theme_bw() + theme(legend.position = \"bottom\", legend.margin = margin(0, 0, 0, 0))\r\nknitr::include_graphics(\"https:\/\/s.yimg.com\/ny\/api\/res\/1.2\/69ZUzNSMYb09GKd8CNJeew--~A\/YXBwaWQ9aGlnaGxhbmRlcjtzbT0xO3c9ODAwO2g9NjAw\/http:\/\/media.zenfs.com\/en_us\/News\/afp.com\/0102e1f7d0d3c35303c8a62d56a5eb79c2c8b4d8.jpg\")\r\n```<\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.39.05.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-4219 size-full\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.39.05.png?resize=625%2C276&#038;ssl=1\" alt=\"\" width=\"625\" height=\"276\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.39.05.png?w=826&amp;ssl=1 826w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.39.05.png?resize=300%2C133&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.39.05.png?resize=768%2C339&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.39.05.png?resize=624%2C276&amp;ssl=1 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>Rather than just printing data R-style, you can nicely format it into a table using kable (part of knitr). I also style mine using <a href=\"https:\/\/cran.r-project.org\/web\/packages\/kableExtra\/vignettes\/awesome_table_in_pdf.pdf\">kableExtra<\/a>, which makes it look nice and gives you extra options. By default tables fill the full width, you can override this using e.g. kable_styling(full.width = FALSE, position = &#8220;left&#8221;). When making a latex document, use kable(table, booktabs = T, &#8220;latex&#8221;) to get a (reproducible) latex-style table.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.41.43.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-4220 size-full\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.41.43.png?resize=625%2C532&#038;ssl=1\" alt=\"\" width=\"625\" height=\"532\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.41.43.png?w=939&amp;ssl=1 939w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.41.43.png?resize=300%2C255&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.41.43.png?resize=768%2C653&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.41.43.png?resize=624%2C531&amp;ssl=1 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>Here&#8217;s how to use python and bash. Thanks to the package <a href=\"https:\/\/rstudio.github.io\/reticulate\/articles\/r_markdown.html\">reticulate<\/a>, you can even share objects between your R and Python chunks. Exclude reticulate (knitr::opts_chunk$set(python.reticulate=FALSE) if you prefer to keep your languages separate.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-highlight=\"2,11\">### Mix it up with python\r\n```{python}\r\na='Wow python'\r\nprint(a.split()[0])\r\n```\r\n\r\nWhat a wild ride. \r\n\r\n### or bash\r\n\r\n```{bash, echo=TRUE}\r\nls | head \r\n```\r\n\r\nOh look, there's our output, ready to share.<\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.50.06.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-4225 size-full\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.50.06.png?resize=560%2C466&#038;ssl=1\" alt=\"\" width=\"560\" height=\"466\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.50.06.png?w=560&amp;ssl=1 560w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-12-at-09.50.06.png?resize=300%2C250&amp;ssl=1 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><\/p>\n<p>Finally, if you hate GUIs &#8211; and you know I do &#8211; you can ditch the interactive notebook part and just generate documents from R Markdown files like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">rmarkdown::render(\"BlogExample.Rmd\")<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two of our esteemed OPIGlets presented a workshop on collaborative research using Jupyter Notebook this week at ISMB in Chicago. Their workshop highlights the importance of finding ways to share your work conveniently and reproducibly. So on a related note, I thought I would share a brief introduction to another useful tool,\u00a0R Markdown with RStudio, [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,14],"tags":[],"ppma_author":[527],"class_list":["post-4209","post","type-post","status-publish","format-standard","hentry","category-code","category-howto"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":527,"user_id":42,"is_guest":0,"slug":"clare","display_name":"Clare West","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/9a762724a4c7d60c6441502fe11052c3785c325c1c1e844dacfbe2bee8b33347?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=4209"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4209\/revisions"}],"predecessor-version":[{"id":4263,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4209\/revisions\/4263"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=4209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=4209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=4209"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=4209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}