<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/automatic-output-format-in-rmarkdown/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://hagutierrezro.blogspot.com/2017/11/i-am-writing-rmarkdown-document-with.html"> Data Literacy - The blog of Andrés Gutiérrez</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am writing a <span>Rmarkdown</span> document with plenty of tables, and I want them in a decent format, e.g. <span>kable</span>. However I don’t want to format them one by one. For example, I have created the following data frame in <span>dplyr</span></p>
<pre><span>data2</span> %<span>></span>% group_by(<span>uf</span>) %<span>></span>%<br>  summarise(<span>n</span> <span>=</span> n(), ) %<span>></span>%<br>  arrange(desc(<span>n</span>))</pre>
<p> One solution to the output format of this data frame would be to name it as an object in R, and then give it a format by using the <span>kable</span> function.</p>
<pre><span>t1</span> <span>&lt;-</span> <span>data2</span> %<span>></span>%<br>  group_by(<span>uf</span>) %<span>></span>%<br>  summarise(<span>n</span> <span>=</span> n(), ) %<span>></span>% arrange(desc(<span>n</span>))<br><br><span>knitr</span><span>:</span><span>:</span>kable(<span>t1</span>)</pre>
<p>However, if your document has hundreds of these queries and you need a faster way to compile the document, while keeping the <span>kable</span> style automatically, avoiding giving a name to the data frame and even avoiding to call the kable function over that name, you can use the <a rel="nofollow" href="https://yihui.name/printr/" target="_blank"><span>printr</span> package</a>. Just add the following piece of code inside a chunk at the beginning of your document and <em>voilá.</em></p>
<pre>library(<span>printr</span>)</pre>
<p>Now, all of your data frames will have a decent style, and you do not need to worry about this issue. For example, I have knitted a presentation by using <span><em>printr </em></span>and the first code in this post, and this is the result:</p>
<p> <a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-SWS9fNhK2gA/WhHLm6Xmd2I/AAAAAAAAAhQ/JG8xQncFg9YsBOoXu1KCr2inA7M2XZ4YwCK4BGAYYCw/s1600/Screen%2BShot%2B2017-11-19%2Bat%2B3.10.33%2BPM.png" target="_blank"><img border="0" alt="" src="https://i0.wp.com/1.bp.blogspot.com/-SWS9fNhK2gA/WhHLm6Xmd2I/AAAAAAAAAhQ/JG8xQncFg9YsBOoXu1KCr2inA7M2XZ4YwCK4BGAYYCw/s320/Screen%2BShot%2B2017-11-19%2Bat%2B3.10.33%2BPM.png?resize=320%2C145&ssl=1" data-recalc-dims="1"></a></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="http://hagutierrezro.blogspot.com/2017/11/i-am-writing-rmarkdown-document-with.html"> Data Literacy - The blog of Andrés Gutiérrez</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/3wtZ7GvBltU" height="1"></a></div>