<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/shiny-application-layouts-exercises-part-5/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/05/07/shiny-application-layouts-exercises-part-5/?utm_source=rss&utm_medium=rss&utm_campaign=shiny-application-layouts-exercises-part-5"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/unnamed-chunk-2-5-2.png"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-5258" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/unnamed-chunk-2-5-2-150x150.png?resize=150%2C150" data-recalc-dims="1"></a></p>
<p><strong>Shiny Application Layouts-Vertical Layout</strong></p>
<p>In the fifth part of our series we will apply the <code>kmeans()</code> function to the <code>iris</code> dataset to create a shiny application. The difference is that now we will display its result vertically.</p>
<p>This part can be useful for you in two ways. </p>
<p>First of all, you can see different ways to enhance the appearance and the utility of your shiny app. </p>
<p>Secondly you can make a revision on what you learnt in <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/2016/12/12/building-shiny-app-exercises-part-1/">“Building Shiny App”</a> series as we will build basic shiny staff in order to present it in the proper way.</p>
<p>Read the examples below to understand the logic of what we are going to do and then test yous skills with the exercise set we prepared for you. Lets begin!</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/05/07/shiny-application-layouts-solutions-part-5/">here</a>.</p>
<p><strong>Vertical Layout</strong> </p>
<p>Every element passed to <code>verticalLayout()</code> will occupy a full row. The difference with <code>fluidRow()</code> is that <code>verticalLayout()</code> does not expect to be another column inside. Look at the example below:<br>
#ui.R<br>
<code>fluidPage(<br>
  verticalLayout(<br>
    titlePanel(&quot;example&quot;),<br>
    plotOutput(&quot;plot&quot;),<br>
    wellPanel(<br>
      sliderInput(&quot;p&quot;, &quot;Points&quot;, 10, 200,<br>
                  value = 50, step = 10)<br>
    )<br>
  )<br>
)</code></p>
<p><strong>Exercise 1</strong></p>
<p>Create the initial UI. <strong>HINT</strong>: Use <code>fluidpage()</code>.</p>
<p><strong>Exercise 2</strong></p>
<p>Apply the vertical layout to your UI. <strong>HINT</strong>: Use <code>verticalLayout()</code>.</p>
<p><strong>Exercise 3</strong></p>
<p>Give the title “Vertical Iris” to your app. <strong>HINT</strong>: Use <code>titlePanel()</code>.</p>
<p><strong>Exercise 4</strong></p>
<p>Create the ui side of your plot. <strong>HINT</strong>: Use <code>plotOutput</code>.</p>
<p>You can use the <code>wellPanel()</code> function which will help you keep your elements separated. Now you should create a <code>selectInput()</code> with the X variables of the <code>iris</code> dataset.<br>
#ui.R<br>
<code>wellPanel(selectInput('x', 'X Variable', names(iris)))</code></p>
<div>
<aside><img src="http://www.r-exercises.com/2017/05/07/shiny-application-layouts-exercises-part-5/?utm_source=rss&utm_medium=rss&utm_campaign=shiny-application-layouts-exercises-part-5"></aside>
<div><strong>Learn more</strong> about Shiny in the online course <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/product/r-shiny-interactive-web-apps/">R Shiny Interactive Web Apps – Next Level Data Visualization</a>. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).</div>
</div>
<p><strong>Exercise 5</strong></p>
<p>Put a <code>selectInput()</code> to choose the X variable under your plot. <strong>HINT</strong>: Use <code>selectInput()</code> and <code>wellPanel()</code>.</p>
<p>In the next two rows we will put with the same way a <code>selectInput()</code> and a <code>numericInput()</code> for the Y Variable and the number of clusters respectively, like the example below.<br>
#ui.R<br>
<code>selectInput('y', 'Y Variable', names(iris),<br>
                selected=names(iris)[[2]]),<br>
    numericInput('clusters', 'ClusterS', 3,<br>
                 min = 1, max = 9)</code></p>
<p><strong>Exercise 6</strong></p>
<p>Create a <code>selectInput()</code> for the Y Variable and a <code>numericInput()</code> to decide the number of clusters and separate them. The features of both are up to you.</p>
<p>Now we have to combine the selected variables for x and y into a new data frame like the example below:<br>
#ui.R<br>
<code>comb &lt;- reactive({<br>
    data[, c(input$x, input$y)]<br>
  })</code></p>
<p><strong>Exercise 7</strong></p>
<p>Combine the selected variables into a new data frame. <strong>HINT</strong>: Use <code>reactive({})</code>.</p>
<p>In order to apply the <code>kmeans()</code> function and decide the number of clusters, to the new data frame we have to use reactivity again. Here is an example:<br>
#ui.R<br>
<code>clusters &lt;- reactive({<br>
    kmeans(comb(), input$clusters)<br>
  })</code></p>
<p><strong>Exercise 8</strong></p>
<p>Apply the <code>kmeans()</code> function to <code>Data()</code> and use as input the number of clusters. <strong>HINT</strong>: Use <code>reactive({})</code>.</p>
<p><strong>Exercise 9</strong></p>
<p>Plot <code>Data()</code> to activate your plot on the server side. <strong>HINT</strong>: Use <code>renderPlot()</code>.</p>
<p>To create a completed kmeans plot you have to add cluster centers and separate each team by color. You can do this with this part of code.<br>
<code>col = clusters()$cluster,<br>
         pch = 20, cex = 3)<br>
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)</code></p>
<p><strong>Exercise 10</strong></p>
<p>Add the code above in the correct place of your server side to complete your kmeans plot.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Building Shiny App exercises part 7" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/02/19/building-shiny-app-exercises-part-7/">Building Shiny App exercises part 7 </a></li>
<li><a title="Building Shiny App exercises part 4" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/01/01/building-shiny-app-exercises-part-4/">Building Shiny App exercises part 4 </a></li>
<li><a title="Building Shiny App exercises part 6" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/02/07/building-shiny-app-exercises-part-6/">Building Shiny App exercises part 6 </a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/r-courses/">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="http://www.r-exercises.com/2017/05/07/shiny-application-layouts-exercises-part-5/?utm_source=rss&utm_medium=rss&utm_campaign=shiny-application-layouts-exercises-part-5"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/aqDn3cy_YJE"></a></div>