<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/three-r-shiny-tricks-to-make-your-shiny-app-shines-13/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://antoineguillot.wordpress.com/2017/02/15/three-r-shiny-tricks-to-make-your-shiny-app-shines-13/"> R – Enhance Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this tutorials serie, we are going to see three tricks to do the following in a Shiny app:</p>
<ol>
<li>Add Next and Previous buttons to navigate in a tabBox</li>
<li>Build a non completely collapsible sidebar to keep the icon visible on collapse</li>
<li>Add button on a datatable output to delete/modify/ do an action on a given row.</li>
</ol>
<h3>1. Next and previous button for a TabBox</h3>
<p>Our goal is to build next and previous buttons to navigate between tabs and to follow the apps flow. Thus, we can avoid repetitive scrollings for the users of the app.</p>
<p>Below is an exemple of what we want to achieve:</p>
<p><img data-image-description="" data-orig-file="https://i0.wp.com/antoineguillot.files.wordpress.com/2017/02/exemplearrowshiny.gif?w=456&amp;ssl=1" data-orig-size="1089,285" data-image-title="exemplearrowshiny" class="wp-image-898" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="898" data-permalink="https://antoineguillot.wordpress.com/2017/02/15/three-r-shiny-tricks-to-make-your-shiny-app-shines-13/exemplearrowshiny/" alt="exemplearrowshiny" data-medium-file="https://i0.wp.com/antoineguillot.files.wordpress.com/2017/02/exemplearrowshiny.gif?w=456&amp;ssl=1?w=300" data-large-file="https://i0.wp.com/antoineguillot.files.wordpress.com/2017/02/exemplearrowshiny.gif?w=456&amp;ssl=1?w=616" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/antoineguillot.files.wordpress.com/2017/02/exemplearrowshiny.gif?w=456&ssl=1"></p>
<h4>a.What do I need to complete this ?</h4>
<p>The app is relying on Shiny and the awesome Shiny Dashboard package. You will just need these two packages to complete the app.</p>
<h4><strong>b. It all started with a Shiny Dashboard and a TabBox</strong></h4>
<p>Now, we need to create a new Shiny Apps and the skeleton of a Shiny Dashboard with its ui.R and server.R file.</p>
<p><em>ui.R: the header and sidebar are disabled because we will only need the body later on</em></p>
<pre>
library(shiny)
library(shinydashboard)
dashboardPage(
 dashboardHeader(disable = T),
 dashboardSidebar(disable = T),
 dashboardBody()
)
</pre>
<p> </p>
<p><em>server.R: just notice session was added as an input  to the server function</em></p>
<pre>
library(shiny)
library(shinydashboard)
shinyServer(function(input, output,session) {})
</pre>
<p> </p>
<h4>c. And we are adding the tabBox and the buttons:</h4>
<p>Now let’s add the next and previous button and the tabBox to the UI and the server.</p>
<p>For the tabBox, you just need to change the dashboardBody from the last part to:</p>
<pre>
dashboardBody(box(width=12,
 tabBox(width=12,id=&quot;tabBox_next_previous&quot;,
   tabPanel(&quot;Tab1&quot;,p(&quot;This is tab 1&quot;)),
   tabPanel(&quot;Tab2&quot;,p(&quot;This is tab 2&quot;)),
   tabPanel(&quot;Tab3&quot;,p(&quot;This is tab 3&quot;)),
   tabPanel(&quot;Tab4&quot;,p(&quot;This is tab 4&quot;))
   ),
   uiOutput(&quot;Next_Previous&quot;)
 )
)
</pre>
<p>The uiOutput will contain the Next and Previous button (we will want them to be ractive, you’ll see why). The id is needed to update the tabBox when clicking on a next or previous button and to get its current state.</p>
<p>The button will be created writing the following code in the server.R file:</p>
<pre>
Previous_Button=tags$div(actionButton(&quot;Prev_Tab&quot;,HTML('
&lt;div class=&quot;col-sm-4&quot;>&lt;i class=&quot;fa fa-angle-double-left fa-2x&quot;>&lt;/i>&lt;/div>
')))
Next_Button=div(actionButton(&quot;Next_Tab&quot;,HTML('
&lt;div class=&quot;col-sm-4&quot;>&lt;i class=&quot;fa fa-angle-double-right fa-2x&quot;>&lt;/i>&lt;/div>
')))
</pre>
<p> </p>
<p>We are creating two action buttons with an arrow icon and no label. They now can be incorporated in the server function:</p>
<pre>
shinyServer(function(input, output,session){
output$Next_Previous=renderUI({
 div(column(1,offset=1,Previous_Button),column(1,offset=8,Next_Button))
 })
})
</pre>
<p> </p>
<h4><strong>d. I am clicking in the buttons and nothing happened!</strong></h4>
<p>Yes, we need to link the button and the tabBox and this is going to be the toughest part!<br>
We need several things:</p>
<ul>
<li>To know which tabs we are on, to be able to move to the next and the previous tab.</li>
<li>To send a message to the tabBox when the next or previous button has been clicked for the tabBox to update to the selected tab.</li>
<li>To show the next and previous tab only when they correspond to a possible action. For instance no next tab should be displayed when we are on the last tab. That is why the buttons UI needed to be reactive.</li>
</ul>
<h4><strong>e. Getting the list of tabs in the tabBox.</strong></h4>
<p>For this we will use a small javascript script:</p>
<pre>
 $('body').mouseover(function() {
 list_tabs=[];
 $('#tabBox_next_previous li a').each(function(){
 list_tabs.push($(this).html())
 });
 Shiny.onInputChange('List_of_tab', list_tabs);})
</pre>
<p> </p>
<p>The script is saying that whenever the mouse is over the body, the shiny input<strong> List_of_tab </strong>should be assigned the list of the the tab names from the tabBox.</p>
<h5>Comments:<br>
<em>The tabBox have the following structure:</em><br>
<em>   #TabBoxID > li > a<br>
Hence at the beginning of the function we are creating empty list and appending to it the text in each tab.</em><br>
<em>The last line is used to set the value as a shiny input.</em><br>
<em>The  $(‘body’).mouseover is not optimal, but using the .load or .ready jquery function would result in an error (Shiny.onInputChange is not a function)</em></h5>
<p>Now, you just have to add the script in your tabBox:</p>
<pre>
 tabBox(width=12,id=&quot;tabBox_next_previous&quot;,
   tabPanel(&quot;Tab1&quot;,p(&quot;This is tab 1&quot;)),
   tabPanel(&quot;Tab2&quot;,p(&quot;This is tab 2&quot;)),
   tabPanel(&quot;Tab3&quot;,p(&quot;This is tab 3&quot;)),
   tabPanel(&quot;Tab4&quot;,p(&quot;This is tab 4&quot;)),
   tags$script(&quot;
     $('body').mouseover(function() {
     list_tabs=[];
     $('#tabBox_next_previous li a').each(function(){
    list_tabs.push($(this).html())
    });
    Shiny.onInputChange('List_of_tab', list_tabs);})
   &quot;)
  )
</pre>
<h4><strong>f. Hiding and showing the next and previous buttons when necessary.</strong></h4>
<p>We want to hide:</p>
<ul>
<li>The next button when the current tab is the last tab</li>
<li>The previous button when the current tab is the first one</li>
</ul>
<pre>
 output$Next_Previous=renderUI({
   tab_list=input$List_of_tab[-length(input$List_of_tab)]
   nb_tab=length(tab_list)
   if (which(tab_list==input$tabBox_next_previous)==nb_tab)
     column(1,offset=1,Previous_Button)
   else if (which(tab_list==input$tabBox_next_previous)==1)
     column(1,offset = 10,Next_Button)
   else
     div(column(1,offset=1,Previous_Button),column(1,offset=8,Next_Button))
</pre>
<p> </p>
<p>Line by line from the renderUI:<br>
– We are setting a variable to be the list of the tabs (minus the last one, because this is not a current tab but an empty one due to the HTML structure of the tabBox).<br>
– We are storing the number of tab.<br>
– If the indice of the current tab is the number of tab, this is the last tab and hence the next button should be hidden.<br>
– If the indice is one, this is the first tab, so only the previous button should be shown.<br>
-Otherwise, all the buttons should be shown.</p>
<h4><strong>g.Updating the tabBox when a button is clicked</strong></h4>
<p>For this, we will use the combination of observeEvent and updateTabsetPanel in the shinyServer function.</p>
<pre>
 observeEvent(input$Prev_Tab,
 {
  tab_list=input$List_of_tab
  current_tab=which(tab_list==input$tabBox_next_previous)
  updateTabsetPanel(session,&quot;tabBox_next_previous&quot;,selected=tab_list[current_tab-1])
 })

 observeEvent(input$Next_Tab,
 {
  tab_list=input$List_of_tab
  current_tab=which(tab_list==input$tabBox_next_previous)
  updateTabsetPanel(session,&quot;tabBox_next_previous&quot;,selected=tab_list[current_tab+1])
 })
</pre>
<p> </p>
<p>In both the observe event, we are doing similar things:</p>
<ol>
<li>Observe id to see if the Previous or Next button have been clicked</li>
<li>Saving the the list of the names of the tabs  and checking the index of the active tab in this list.</li>
<li>Updating the tabBox to the next or previous tab <em>(selected=tab_list[current_tab+-1])</em></li>
</ol>
<h4>h. That is it !</h4>
<p>Now the tabBox and the next and previous button should be functional!</p>
<p>You can find the code: <a target="_blank" rel="nofollow" href="https://gitlab.com/ant-guillot/Part1_Next_Previous_Button">HERE</a></p>
<p><a data-text-color="#111111" target="_blank" class="twitter-follow-button" data-link-color="#0093c2" rel="nofollow" href="https://twitter.com/AntGuilllot" data-show-count="false">Follow @AntGuilllot</a></p>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antoineguillot.wordpress.com/882/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/antoineguillot.wordpress.com/882/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="https://antoineguillot.wordpress.com/2017/02/15/three-r-shiny-tricks-to-make-your-shiny-app-shines-13/"> R – Enhance Data</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/brnlUDSoNV0"></a></div>