<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/programmatically-creating-text-output-in-r-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-exercises.com/2018/05/25/programmatically-creating-text-output-in-r-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2018/05/33824145905_d6dc3515f8_z.jpg?ssl=1" target="_blank"><img sizes="(max-width: 295px) 100vw, 295px" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2018/05/33824145905_d6dc3515f8_z-295x300.jpg?resize=295%2C300&ssl=1" alt="" width="295" class="wp-image-11580" data-recalc-dims="1" height="300"></a></p>
<p>In the age of Rmarkdown and Shiny, or when making any custom output from your data you want your output to look consistent and neat. Also, when writing your output you often want it to obtain a specific (decorative) format defined by the html or LaTeX engine. These exercises are an opportunity to refresh our memory on functions such as paste, sprintf, formatC and others that are convenient tools to achieve these ends. All of the solutions rely partly on the ultra flexible sprintf() but there are no-doubt many ways to solve the exercises with other functions, feel free to share your solutions in the comment section.</p>
<p>Example solutions are available <a rel="nofollow" href="https://www.r-exercises.com/2018/05/25/programmatically-creating-text-output-in-r-solutions" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong></p>
<p>Print out the following vector as prices in dollars (to the nearest cent):<br>
<code>c(14.3409087337707, 13.0648270623048, 3.58504267621646, 18.5077076398145,<br>
16.8279241011882)</code>. Example: <code>$14.34</code></p>
<p><strong>Exercise 2</strong></p>
<p>Using these numbers <code>c(25, 7, 90, 16)</code> make a vector of filenames in the following format: <code>file_025.txt</code>. That is, left pad the numbers so they are all three digits.</p>
<p><strong>Exercise 3</strong></p>
<p>Actually, if we are only dealing numbers less than hundred <code>file_25.txt</code> would have been enough. Change the code from last exercise so that the padding is progammatically decided by the biggest number in the vector.</p>
<p><strong>Exercise 4</strong></p>
<p>Print out the following haiku on three lines, right aligned, with the help of cat. <code>c(&quot;Stay the patient course.&quot;, &quot;Of little worth is your ire.&quot;, &quot;The network is down.&quot;)</code>.</p>
<p><strong>Exercise 5</strong></p>
<p>Write a function that converts a number to its hexadecimal representation. This is a useful skill when converting bmp colors from one representation to another. Example output:<code></code></p>
<pre>      tohex(12)
      [1] &quot;12 is c in hexadecimal&quot;
</pre>
<p><strong>Exercise 6</strong></p>
<p>Take a string and programmatically surround it with the html header tag <code>h1</code></p>
<p><strong>Exercise 7</strong></p>
<p>Back to the poem from exercise 4, let R convert to html <a rel="nofollow" href="https://www.w3schools.com/tags/tag_ul.asp" target="_blank">unordered list</a>. So that it would appear like the following in a browser:</p>
<ul>
<li>Stay the patient course.</li>
<li>Of little worth is your ire.</li>
<li>The network is down.</li>
</ul>
<p><strong>Exercise 8</strong></p>
<p>Here is a list of the currently top 5 movies on imdb.com in terms of rating <code>c(&quot;The Shawshank Redemption&quot;, &quot;The Godfather&quot;, &quot;The Godfather: Part II&quot;, &quot;The Dark Knight&quot;, &quot;12 Angry Men&quot;, &quot;Schindler's List&quot;)</code> convert them into a list compatible with written text.</p>
<p>Example output:</p>
<p><code>[1] &quot;The top ranked films on imdb.com are The Shawshank Redemption, The Godfather, The Godfather: Part II, The Dark Knight, 12 Angry Men and Schindler's List&quot;</code></p>
<p><strong>Exercise 9</strong></p>
<p>Now you should be able to solve this quickly: write a function that converts a proportion to a percentage that takes as input the number of decimal places. Input of 0.921313 and 2 decimal places should return <code>&quot;92.13%&quot;</code></p>
<p><strong>Exercise 10</strong></p>
<p>Improve the function from last exercise so the percentage take consistently 10 characters by doing some left padding. Raise an error if percentage already happens to be longer than 10.</p>
<p>(Image by <a rel="nofollow" href="https://www.flickr.com/photos/daniel_friedman/" target="_blank">Daniel Friedman</a>).</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/07/13/parallel-computing-exercises-foreach-and-doparallel-part-2/" title="Parallel Computing Exercises: Foreach and DoParallel (Part-2)" target="_blank">Parallel Computing Exercises: Foreach and DoParallel (Part-2) </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/08/03/parallel-computing-exercises-snow-and-rmpi-part-3/" title="Parallel Computing Exercises: Snow and Rmpi (Part-3)" target="_blank">Parallel Computing Exercises: Snow and Rmpi (Part-3) </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/01/01/building-shiny-app-exercises-part-4/" title="Building Shiny App exercises part 4" target="_blank">Building Shiny App exercises part 4 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<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://www.r-exercises.com/2018/05/25/programmatically-creating-text-output-in-r-exercises/"> R-exercises</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/Tb_CfHleSng" height="1"></a></div>