<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/building-shiny-app-exercises-part-7/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/02/19/building-shiny-app-exercises-part-7/?utm_source=rss&utm_medium=rss&utm_campaign=building-shiny-app-exercises-part-7"> 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="http://r-exercises.com/wp-content/uploads/2017/01/shiny-1.png?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-4236" src="https://i1.wp.com/r-exercises.com/wp-content/uploads/2017/01/shiny-1-150x150.png?resize=150%2C150" data-recalc-dims="1"></a></p>
<p><strong>Connect widgets &amp; plots</strong></p>
<p>In the seventh part of our journey we are ready to connect more of the widgets we created before with our k-means plot in order to totally control its output. Of cousre we will also reform the plot itself properly in order to make it a real k-means plot.<br>
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/02/19/building-shiny-app-solutions-part-7/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p>First of all let’s move the widgets we are going to use from the <code>sidebarPanel</code> into the <code>mainPanel</code> and specifically under our <code>plot</code>.</p>
<p><strong>Exercise 1</strong></p>
<p>Remove the <code>textInput</code> from your server.R file. Then place the <code>checkboxGroupInput</code> and the <code>selectInput</code> in the same row with the <code>sliderInput</code>. Name them “Variable X” and “Variable Y” respectively. HINT: Use <code>fluidrow</code> and <code>column</code>.</p>
<p><strong>Create a reactive expression</strong></p>
<p>Reactive expressions are expressions that can read reactive values and call other reactive expressions. Whenever a reactive value changes, any reactive expressions that depended on it are marked as “invalidated” and will automatically re-execute if necessary. If a reactive expression is marked as invalidated, any other reactive expressions that recently called it are also marked as invalidated. In this way, invalidations ripple through the expressions that depend on each other.<br>
The reactive expression is activated like this: <code>example &lt;- reactive({ })</code></p>
<p><strong>Exercise 2</strong></p>
<p>Place a reactive expression in server.R, at any spot except inside <code>output$All</code> and name it “Data”. HINT: Use <code>reactive</code></p>
<p><strong>Connect your dataset’s variables with your widgets.</strong></p>
<p>Now let’s connect your <code>selectInput</code> with the variables of your dataset as in the example below.</p>
<p>#ui.R<br>
<code>library(shiny)<br>
shinyUI(fluidPage(<br>
  titlePanel(&quot;Shiny App&quot;),
<p> sidebarLayout(<br>
sidebarPanel(h2(&quot;Menu&quot;),<br>
selectInput('ycol', 'Y Variable', names(iris)) ),<br>
mainPanel(h1(&quot;Main&quot;)<br>
)<br>
)<br>
))<br>
#server.R<br>
<code>shinyServer(function(input, output) {<br>
  example &lt;- reactive({<br>
    iris[, c(input$ycol)]<br>
  })<br>
})  </code></p>
<p><strong>Exercise 3</strong></p>
<p>Put the variables of the <code>iris</code> dataset as inputs in your <code>selectInput</code> as “Variable Y” . HINT: Use<code> names</code>.</p>
<p><strong>Exercise 4</strong></p>
<p>Do the same for <code>checkboxGroupInput</code> and “Variable X”. HINT: Use<code> names</code>.</p>
<p>Select the fourth variabale as default like the example below.</p>
<p>#ui.R<br>
<code>library(shiny)<br>
shinyUI(fluidPage(<br>
  titlePanel(&quot;Shiny App&quot;),
<p> sidebarLayout(<br>
sidebarPanel(h2(&quot;Menu&quot;),<br>
checkboxGroupInput(&quot;xcol&quot;, &quot;Variable X&quot;,names(iris),<br>
selected=names(iris)[[4]]),<br>
selectInput(&quot;ycol&quot;, &quot;Y Variable&quot;, names(iris),<br>
selected=names(iris)[[4]])<br>
),<br>
mainPanel(h1(&quot;Main&quot;)<br>
)<br>
)<br>
))<br>
#server.R<br>
<code>shinyServer(function(input, output) {<br>
  example &lt;- reactive({<br>
    iris[, c(input$xcol,input$ycol)<br>
         ]<br>
  })<br>
})  </code></p>
<p><strong>Exercise 5</strong></p>
<p>Make the second variable the default choise for both widgets. HINT: Use <code>selected</code>.</p>
<p>Now follow the example below to create a new function and place there the automated function for k means calculation.</p>
<p>#ui.R<br>
<code>library(shiny)<br>
shinyUI(fluidPage(<br>
  titlePanel(&quot;Shiny App&quot;),
<p> sidebarLayout(<br>
sidebarPanel(h2(&quot;Menu&quot;),<br>
checkboxGroupInput(&quot;xcol&quot;, &quot;Variable X&quot;,names(iris),<br>
selected=names(iris)[[4]]),<br>
selectInput(&quot;ycol&quot;, &quot;Y Variable&quot;, names(iris),<br>
selected=names(iris)[[4]])<br>
),<br>
mainPanel(h1(&quot;Main&quot;)<br>
)<br>
)<br>
))<br>
#server.R<br>
<code>shinyServer(function(input, output) {<br>
  example &lt;- reactive({<br>
    iris[, c(input$xcol,input$ycol)<br>
         ]<br>
  })<br>
  example2 &lt;- reactive({<br>
    kmeans(example())<br>
  })<br>
})  </code></p>
<p><strong>Exercise 6</strong></p>
<p>Create the reactive function <code>Clusters</code> and put in there the function <code> kmeans</code> which will be applied on the function <code>Data</code>. HINT: Use <code>reactive</code>.</p>
<p><strong>Connect your plot with the widgets.</strong></p>
<p>It is time to connect your plot with the widgets.</p>
<p><strong>Exercise 7</strong></p>
<p>Put <code>Data </code>inside <code>renderPlot</code> as first argument replacing the data that you have chosen to be plotted until now. Moreover delete <code>xlab</code> and <code>ylab</code>.</p>
<p><strong>Improve your k-means visualiztion.</strong></p>
<p>You gan change automatically the colours of your clusters by copying and pasting this part of code as first argument of <code>renderPlot </code>before the <code>plot</code> function:</p>
<p><code>palette(c(&quot;#E41A1C&quot;, &quot;#377EB8&quot;, &quot;#4DAF4A&quot;, &quot;#984EA3&quot;,<br>
      &quot;#FF7F00&quot;, &quot;#FFFF33&quot;, &quot;#A65628&quot;, &quot;#F781BF&quot;, &quot;#999999&quot;))</code></p>
<p>We will choose to have up to nine clusters so we choose nine colours.</p>
<p><strong>Exercise 8</strong></p>
<p>Set <code>min</code> of your <code>sliderInput</code> to 1, <code>max</code> to 9 and <code>value</code> to 4 and use the <code>palette</code> function to give colours.</p>
<p>This is how you can give different colors to your clusters. To activate these colors put this part of code into your <code>plot</code> function.</p>
<p><code>col = Clusters()$cluster,</code></p>
<p><strong>Exercise 9</strong></p>
<p>Activate the <code> palette</code> function.</p>
<p>To make your clusters easily foundable you can fully color them by adding into <code>plot</code> function this:<br>
<code>pch</code> = 20,<code> cex</code> = 3</p>
<p><strong>Exercise 10</strong></p>
<p>Fully color the <code>points</code> of your <code>plot</code>.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Building Shiny App exercises part 6" target="_blank" rel="nofollow" href="http://r-exercises.com/2017/02/07/building-shiny-app-exercises-part-6/">Building Shiny App exercises part 6 </a></li>
<li><a title="Building Shiny App exercises part 1" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/12/building-shiny-app-exercises-part-1/">Building Shiny App exercises part 1 </a></li>
<li><a title="Building Shiny App exercises part 2" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/18/building-shiny-app-exercises-part-2/">Building Shiny App exercises part 2 </a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://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://r-exercises.com/2017/02/19/building-shiny-app-exercises-part-7/?utm_source=rss&utm_medium=rss&utm_campaign=building-shiny-app-exercises-part-7"> 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/tneNCID_FWw"></a></div></code></code></code>