<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/interchanging-rmarkdown-and-spinnable-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://webbedfeet.netlify.com/post/interchanging-rmarkdown-and-spinnable-r/"> R on Abhijit Dasgupta</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Dean Attali wrote this nice <a rel="nofollow" href="https://deanattali.com/2015/03/24/knitrs-best-hidden-gem-spin/" target="_blank">post</a> a few years ago describing <code>knitr</code>’s <code>spin</code> function. This function allows a regular R file, with comments written with the <code>roxygen2</code>-style comment tag <code>#'</code> to<br>
be rendered as an HTML document with the comments rendered as text and the results of<br>
the R code rendered in place, much as a RMarkdown document would. The basic rules for this are (from Dean’s post):</p>
<ul>
<li>Any line beginning with <code>#'</code> is treated as a markdown directive (<code>#' # title</code> will be a header, <code>#' some **bold** text</code> results in some <strong>bold</strong> text)</li>
<li>Any line beginning with <code>#+</code> is parsed as code chunk options</li>
<li>Run <code>knitr::spin</code> on the file</li>
</ul>
<p>In effect, this “spinnable” R script is the complement of a RMarkdown document with<br>
respect to format, since the RMarkdown document is primarily a text (Markdown) document with code chunks, and the R script is primarily a code document with text chunks.</p>
<p>RMarkdown is nice when you want to create a final document to report your analysis, but can be burdensome when you’re developing code. The R script is much nicer for developing code, but it would be nice to get a nice document at the end of development without too much hassle. It turns out we can flip back and forth between the two complementary formats through functions in <code>knitr</code>, where <code>fname</code> denotes the respecive source file:</p>
<ul>
<li>RMarkdown to R : <code>knitr::purl(fname, documentation = 2)</code></li>
<li>R to RMarkdown : <code>knitr::spin(fname, knit = FALSE, format = &quot;Rmd&quot;)</code></li>
</ul>
<p>Why would you want to convert R to RMarkdown when it renders to HTML or Word or PDF automatically (within RStudio), you ask? Because <code>spin</code> uses <code>knit2html</code>/<code>knit2pdf</code>/<code>knit</code> for rendering the documents, rather than <code>rmarkdown::render</code>, which uses the power of <a rel="nofollow" href="https://pandoc.org/" target="_blank">Pandoc</a> and hence has richer options in terms of formatting. You could create a pipeline that starts with your R script, creates the intermediate Rmd file using spin, and then uses <code>rmarkdown::render</code> to create the final product.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://webbedfeet.netlify.com/post/interchanging-rmarkdown-and-spinnable-r/"> R on Abhijit Dasgupta</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/IZQk_dHGwQs" height="1"></a></div>