<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/reactive-acronym-list-in-stratvis-a-timevis-based-shiny-app/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/DataProne-R/~3/Ip4q84Z1Gt8/acronym-list-in-timevis-shiny-app.html"> data prone - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Abstract</h2>
<p>I present a method for reactively updating a table of acronyms from a Shiny interactive timeline using renderDataTable and timevis. The method is used in the new Shiny app, <em>stratvis</em>.</p>
<h2>The <em>stratvis</em> app</h2>
<p>The <a target="_blank" rel="nofollow" href="https://github.com/padamson/stratvis"><code>stratvis</code></a> Shiny app provides a rich and <em>fully interactive</em> timeline<br>
visualization of hierarchical items (e.g. strategy, policy, guidance, goals,<br>
objectives, milestones, decisions) for a strategic view of organizational<br>
activity. The app uses the <a target="_blank" rel="nofollow" href="https://github.com/daattali/timevis"><code>timevis</code></a> R package, which is based on<br>
the <a target="_blank" rel="nofollow" href="http://visjs.org/">vis.js</a> <code>Timeline</code> module and the<br>
<a target="_blank" rel="nofollow" href="http://www.htmlwidgets.org/">htmlwidgets</a> R package. For convenience, I’ve hosted<br>
<a target="_blank" rel="nofollow" href="http://dataprone.co/shiny/stratvis/">a demo of stratvis</a> on<br>
<a target="_blank" rel="nofollow" href="http://dataprone.co/shiny/">my Shiny server</a>, so you can scroll through the interactive<br>
timeline and watch the acronym list adjust automatically.</p>
<p><a target="_blank" rel="nofollow" href="http://dataprone.co/shiny/stratvis/"><br>
<img alt="stratvis Shiny app screenshot" src="https://i0.wp.com/padamson.github.io/assets/stratvis-small.png?w=456" data-recalc-dims="1"><br>
</a></p>
<h2>Relevant background on the <em>timevis</em> API</h2>
<p>When a timeline widget is created in a Shiny app with the <code>timevis</code> method, four Shiny inputs<br>
are also created (and updated as the interactive timeline is manipulated within the app).<br>
The names of the inputs are based upon the name given to the timeline object (with <code>_data</code>,<br>
<code>_ids</code>, <code>_selected</code>, and <code>_window</code> appended). We will use the <code>_data</code> and <code>_window</code> appended<br>
objects in our<br>
method to build the reactive acronym list below.<br>
If the interactive timeline object is <code>timelineGroups</code> (as it<br>
is in the <code>stratvis</code> demo), then the following input variables are available:</p>
<ul>
<li>
<p><code>input$timelineGroups_data</code> – will return a data.frame containing the data<br>
of the items in the timeline. The input is updated every time an item is<br>
modified, added, or removed.</p>
</li>
<li>
<p><code>input$timelineGroups_window</code> – will return a 2-element vector containing the minimum and maximum dates currently visible in the timeline. The input is updated every time the viewable window of dates is updated (by zooming or moving the window).</p>
</li>
</ul>
<h2>Reactively update acronym list</h2>
<p>The block of code in <code>server.R</code> that generates the reactively updated acronym list:</p>
<figure>
<pre><code data-lang="r"><span>output</span><span>$</span><span>acronyms</span><span> </span><span>&lt;-</span><span> </span><span>DT</span><span>::</span><span>renderDataTable</span><span>({</span><span>
</span><span>firstDate</span><span> </span><span>&lt;-</span><span> </span><span>input</span><span>$</span><span>timelineGroups_window</span><span>[</span><span>1</span><span>]</span><span>
</span><span>lastDate</span><span> </span><span>&lt;-</span><span> </span><span>input</span><span>$</span><span>timelineGroups_window</span><span>[</span><span>2</span><span>]</span><span> </span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>input</span><span>$</span><span>timelineGroups_data</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>label</span><span>,</span><span>start</span><span>,</span><span>end</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>((</span><span>is.na</span><span>(</span><span>end</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>(</span><span>start</span><span> </span><span>></span><span> </span><span>firstDate</span><span> </span><span>&amp;</span><span> </span><span>start</span><span> </span><span>&lt;</span><span> </span><span>lastDate</span><span>))</span><span> </span><span>|</span><span> </span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>end</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>(</span><span>start</span><span> </span><span>&lt;</span><span> </span><span>lastDate</span><span> </span><span>&amp;</span><span> </span><span>end</span><span> </span><span>></span><span> </span><span>firstDate</span><span>)))</span><span>
</span><span>acronyms</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>grepl</span><span>(</span><span>
</span><span>paste</span><span>(</span><span>unlist</span><span>(</span><span>str_split</span><span>(</span><span>data</span><span>$</span><span>label</span><span>,</span><span>pattern</span><span>=</span><span>&quot; &quot;</span><span>)),</span><span>collapse</span><span>=</span><span>&quot;|&quot;</span><span>),</span><span> </span><span>acronym</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>acronym</span><span>,</span><span> </span><span>full</span><span>)</span><span>
</span><span>},</span><span>
</span><span>options</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>
</span><span>paging</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>order</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>list</span><span>(</span><span>1</span><span>,</span><span> </span><span>'asc'</span><span>)),</span><span>
</span><span>rownames</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>columnDefs</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span> </span><span>list</span><span>(</span><span>visible</span><span>=</span><span>FALSE</span><span>,</span><span>targets</span><span>=</span><span>0</span><span>)</span><span> </span><span>),</span><span>
</span><span>colnames</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Acronym&quot;</span><span>,</span><span>&quot;&quot;</span><span>)</span><span>
</span><span>)</span><span>
</span><span>)</span><span> </span></code></pre>
</figure>
<p>Obviously, the core functionality is provided by the <code>renderDataTable</code><br>
method, which makes a reactive version of a function that returns a data<br>
frame (or matrix) to be rendered with the DataTables library.<br>
As mentioned above, the <code>timelineGroups</code> object is the actual <code>timevis</code> interactive timeline<br>
object, and the following code ensure that the <code>acronyms</code> Shiny output is<br>
reactively updated whenever the <code>timelineGroups_window</code> Shiny<br>
input changes (i.e. when the minimum or maximum date in the visible timeline<br>
window changes):</p>
<figure>
<pre><code data-lang="r"><span>firstDate</span><span> </span><span>&lt;-</span><span> </span><span>input</span><span>$</span><span>timelineGroups_window</span><span>[</span><span>1</span><span>]</span><span>
</span><span>lastDate</span><span> </span><span>&lt;-</span><span> </span><span>input</span><span>$</span><span>timelineGroups_window</span><span>[</span><span>2</span><span>]</span><span> </span></code></pre>
</figure>
<p>The <code>label</code>, <code>start</code>, and <code>end</code> variables within the <code>timelineGroups_data</code> Shiny<br>
input are selected and then filtered for only those items visible in the timeline:</p>
<figure>
<pre><code data-lang="r"><span>data</span><span> </span><span>&lt;-</span><span> </span><span>input</span><span>$</span><span>timelineGroups_data</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>label</span><span>,</span><span>start</span><span>,</span><span>end</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>((</span><span>is.na</span><span>(</span><span>end</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>(</span><span>start</span><span> </span><span>></span><span> </span><span>firstDate</span><span> </span><span>&amp;</span><span> </span><span>start</span><span> </span><span>&lt;</span><span> </span><span>lastDate</span><span>))</span><span> </span><span>|</span><span> </span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>end</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>(</span><span>start</span><span> </span><span>&lt;</span><span> </span><span>lastDate</span><span> </span><span>&amp;</span><span> </span><span>end</span><span> </span><span>></span><span> </span><span>firstDate</span><span>)))</span></code></pre>
</figure>
<p>The call to <code>filter</code> accomodates ‘point’ and ‘box’ type objects (<code>type = 'point'</code> or<br>
<code>type = 'box'</code>) that do not have end dates (<code>end = NA</code>) as well as ‘range’ and ‘background’<br>
type objects (<code>type = 'range'</code> or <code>type = 'background'</code>). Note that this approach<br>
would also update the acronym list if an item is added interactively to the timeline.</p>
<p>Each row of the <code>acronyms</code> data frame is an acronym (<code>acronym</code> variable) and<br>
the full phrase to which it corresponds (<code>full</code> variable). The following code uses<br>
<code>grepl</code> to filter <code>acronyms</code> down to only those items present in the <code>data</code> object<br>
created above. The regular expression used in the call to <code>grepl</code> is simply<br>
all of the words (separated by white space) in <code>data$label</code> pasted together with<br>
“|” characters between them.</p>
<figure>
<pre><code data-lang="r"><span>acronyms</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>grepl</span><span>(</span><span>
</span><span>paste</span><span>(</span><span>unlist</span><span>(</span><span>str_split</span><span>(</span><span>data</span><span>$</span><span>label</span><span>,</span><span>pattern</span><span>=</span><span>&quot; &quot;</span><span>)),</span><span>collapse</span><span>=</span><span>&quot;|&quot;</span><span>),</span><span> </span><span>acronym</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>acronym</span><span>,</span><span> </span><span>full</span><span>)</span></code></pre>
</figure>
<h2>Feedback welcome</h2>
<p>If you have any feedback on the above approach or the <a target="_blank" rel="nofollow" href="https://github.com/padamson/stratvis">stratvis</a><br>
app in general, please<br>
leave a comment below or use the Tweet button.<br>
As with any of my projects, feel free to <a target="_blank" rel="nofollow" href="https://github.com/padamson/stratvis/fork">fork the stratvis repo</a><br>
and submit a pull request if you wish to contribute.</p>
<p><a data-style="mega" target="_blank" rel="nofollow" href="https://github.com/padamson/stratvis/archive/master.zip" data-icon="octicon-cloud-download">Download</a><br>
<a data-style="mega" data-count-href="http://feedproxy.google.com/padamson/stratvis/network" data-count-aria-label="# forks on GitHub" target="_blank" data-count-api="/repos/padamson/stratvis#forks_count" rel="nofollow" href="https://github.com/padamson/stratvis/fork" data-icon="octicon-repo-forked">Fork</a></p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataProne-R/~4/Ip4q84Z1Gt8"></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="http://feedproxy.google.com/~r/DataProne-R/~3/Ip4q84Z1Gt8/acronym-list-in-timevis-shiny-app.html"> data prone - R</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/H2F5Lmx_wjg"></a></div>