<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/modularize-your-shiny-apps-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/10/15/modularize-your-shiny-apps-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://i1.wp.com/www.r-exercises.com/wp-content/uploads/2018/08/building-blocks-615239_1280.jpg?ssl=1" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2018/08/building-blocks-615239_1280-300x225.jpg?resize=300%2C225&ssl=1" alt="" width="300" class="wp-image-11932" data-recalc-dims="1" height="225"></a>Shiny modules are short (well, usually short) server and UI functions, that can be connected to each other by a common namespace, and be embedded within a regular Shiny app. You can’t run a Shiny module without a parent Shiny app. The modules can contain both inputs and outputs, and are usually centered around a single operation or theme.</p>
<p>The biggest advantage of modules is the ability to efficiently reuse Shiny code, which can save a great deal of time. In addition, modules can help you standardize and scale your Shiny operations. Lastly, even if not reused, Shiny modules can help with organizing the code and break it into smaller pieces – which is very much needed in many complex Shiny apps. Some more information on Shiny modules can be found <a rel="nofollow" href="https://shiny.rstudio.com/articles/modules.html" target="_blank">here</a>.</p>
<p>In the following exercise set, you will practice the not-so-straightforward use of Shiny modules. The first four exercises are a warm-up, and will help you “refresh” on how to build each part of a Shiny module. In each of the last six exercises you will build a complete end-to-end module and run a minimal Shiny app to test it. Answers to the exercises are available <a rel="nofollow" href="https://r-exercises.com/2018/10/15/modularize-your-shiny-apps-solutions/" target="_blank">here</a>.</p>
<p>Two reminders before we begin:<br>
* A typical UI function would take the argument <code>id</code> and start with the line <code>ns &lt;- NS(id)</code>.<br>
* All input and output IDs within a UI function should be wrapped with the function like <code>ns()</code>.</p>
<p><strong>Exercise 1</strong><br>
Build a module-UI-function that provides a <code>selectInput</code> control, where the choices are <code>LETTERS</code>.</p>
<p><strong>Exercise 2</strong><br>
Build the corresponding module-server-function, that prints the selected letter to the console.</p>
<p><strong>Exercise 3</strong><br>
Build a regular UI object that contains the module-UI-function.</p>
<p><strong>Exercise 4</strong><br>
Build a regular server function that calls the module you built in exercises 1 and 2.<br>
Put together a minimal Shiny app that runs everything (e.g, <code>shinyApp(ui = ui, server = server)</code>).</p>
<p><strong>Exercise 5</strong><br>
Adjust the module you built to show the selecter letter as a UI <code>textOutput</code> instead of printing it to the console.</p>
<div>
<aside><img src="https://www.r-exercises.com/2018/10/15/modularize-your-shiny-apps-exercises/"></aside>
<div><strong>Learn more</strong> about Shiny apps in the online courses <a rel="nofollow" href="https://www.r-exercises.com/product/r-shiny-apps/" target="_blank">Create Interactive Web Applications with the R Shiny Package</a> and <a rel="nofollow" href="https://www.r-exercises.com/product/r-shiny-interactive-web-apps/" target="_blank">R Shiny Interactive Web Apps</a>.</div>
</div>
<p><strong>Exercise 6</strong><br>
In some cases you’ll need the same module twice in a single Shiny app.<br>
Build a minimal Shiny app that uses the module from exercise 5 twice.</p>
<p><strong>Exercise 7</strong><br>
Still with the same “letters” module, add an option for the app developer to select the label of the <code>selectInput</code>, with the default value being “Select a letter”. Call the module with a different value for the label than the default.</p>
<p><strong>Exercise 8</strong><br>
Build a “contact form” module that contains a name (<code>textInput</code>), a subject (<code>selectInput</code> with dynamic choices, namely the person who uses the module can choose which choices to display) a message (<code>textAreaInput</code>) and a button (<code>actionButton</code> with a dynamic label). Upon clicking the button, all of the form information should be saved in a text file.</p>
<p><strong>Exercise 9</strong><br>
Build a module that takes a number <i>n</i> using <code>numericInput</code>, and generates <i>n</i> elements of type <code>textInput</code>.</p>
<p><strong>Exercise 10</strong><br>
Modules can also be nested within each other. Namely, one module can call anther module.<br>
To practice that, create two modules, an “inner” one and an “outer” one.<br>
Your minimal Shiny app should call the “outer” module, that in turn will call the “inner” module.<br>
The module-server-functions should take an argument <code>text</code>, and render it as a <code>textOutput</code>.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2016/12/25/building-shiny-app-exercises-part-3/" title="Building Shiny App exercises part 3" target="_blank">Building Shiny App exercises part 3 </a></li>
<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/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="https://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/10/15/modularize-your-shiny-apps-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/prLYdO8ICK0" height="1"></a></div>