<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/useful-tools-for-shiny-developers-in-the-new-version-of-shinyjs/"></div></div><div><a data-url="https://www.r-bloggers.com/useful-tools-for-shiny-developers-in-the-new-version-of-shinyjs/" data-counturl="https://www.r-bloggers.com/useful-tools-for-shiny-developers-in-the-new-version-of-shinyjs/" data-count="horizontal" data-text="Useful tools for shiny developers in the new version of shinyjs" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://deanattali.com/blog/shinyjs-v08/"> Dean Attali's R Blog</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://github.com/daattali/shinyjs">shinyjs</a> is a package that helps you easily improve the user interaction and user experience in your shiny apps. It has come a long way in the 1.5 years since it was first released, and today I’m happy to announce that version 0.8 is now on CRAN.</p>
<p>This version brings two new features that are used primarily to make the process of developing a shiny app easier, along with a few more improvements to existing functions.</p>
<h2>New functions to help with shiny app development</h2>
<h3>1. Run arbitrary R code live in a Shiny app</h3>
<p>When I develop Shiny apps or packages for Shiny, I often find myself wanting to be able to run some R code on-demand while the app is running. Outside of Shiny, in regular R programming, we have the R console where we can run any command at any point in time, but in Shiny we don’t really have that.</p>
<p>What I usually end up doing is create several action buttons, and make each button run a different piece of code. This works, but it’s slow and tedious. A better solution that I sometimes use when I’m not too lazy is add a text input where I can type any code and run it. This works better, but requires a lot of code to set up.</p>
<p>Wouldn’t it be awesome if there was an easy way to run arbitrary R code on-demand while you’re developing your app? Now there is! All you need to do is call <code>runcodeUI()</code> in your UI and <code>runcodeServer()</code> in your server, and your app will have an input that can be used to run any R command.</p>
<p>Don’t believe how easy it is? The code below is all you need.</p>
<div>
<pre><code><span>shinyApp</span><span>(</span><span>
</span><span>ui</span><span> </span><span>=</span><span> </span><span>fluidPage</span><span>(</span><span>
</span><span>useShinyjs</span><span>(),</span><span>
</span><span>strong</span><span>(</span><span>&quot;Enter an R expression&quot;</span><span>),</span><span>
</span><span>runcodeUI</span><span>()</span><span>
</span><span>),</span><span>
</span><span>server</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>input</span><span>,</span><span> </span><span>output</span><span>)</span><span> </span><span>{</span><span>
</span><span>runcodeServer</span><span>()</span><span>
</span><span>}</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/deanattali.com/img/blog/shinyjs-v08/run-arbitrary-code.gif"><img alt="Run arbitrary code GIF" src="https://i0.wp.com/deanattali.com/img/blog/shinyjs-v08/run-arbitrary-code.gif?w=456" data-recalc-dims="1"></a></p>
<p>Note that this can be useful for testing and while developing an app locally, but it should not be included in an app that is accessible to other people, as letting others run arbitrary R code can open you up to security attacks.</p>
<h3>2. Redirect any console.log messages from the JavaScript console to the R console</h3>
<p>When developing and debugging a Shiny app that uses custom JavaScript code, it can be helpful to use <code>console.log()</code> messages in JavaScript. However, it can be time-consuming to have to keep opening the browser’s JavaScript console in order to see the messages. Instead, you can use the new <code>showLog()</code> function to redirect JavaScript log messages to the R console.</p>
<p>Again, using this feature is fairly simple: you need to use the <code>showLog = TRUE</code> parameter when including shinyjs, and you need to call <code>showLog()</code> somewhere in the server, as the example code below shows. (The example below makes use of a function called <code>logjs()</code>, which is a shinyjs function that does what you’d think – it sends a message to be logged to the JavaScript console.)</p>
<div>
<pre><code><span>shinyApp</span><span>(</span><span>
</span><span>ui</span><span> </span><span>=</span><span> </span><span>fluidPage</span><span>(</span><span>
</span><span>useShinyjs</span><span>(</span><span>showLog</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span># Set up shinyjs with showLog
</span><span> </span><span>textInput</span><span>(</span><span>&quot;text&quot;</span><span>,</span><span> </span><span>&quot;Type something&quot;</span><span>)</span><span>
</span><span>),</span><span>
</span><span>server</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>input</span><span>,</span><span> </span><span>output</span><span>)</span><span> </span><span>{</span><span>
</span><span>showLog</span><span>()</span><span>
</span><span>observe</span><span>({</span><span>
</span><span>logjs</span><span>(</span><span>paste</span><span>(</span><span>&quot;Length of text:&quot;</span><span>,</span><span> </span><span>nchar</span><span>(</span><span>input</span><span>$</span><span>text</span><span>)))</span><span>
</span><span>})</span><span>
</span><span>}</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<h2>Improvements to existing functions</h2>
<p>There are two main feature improvements in this version of shinyjs:</p>
<ol>
<li>
<p>The <code>onclick()</code> and <code>onevent()</code> functions previously could only run an R expression when the event (mouse click, key press, mouse hover, etc) happened. This version adds support for a callback function, where the JavaScript Event object is passed to the R callback. This means that you can now get exact information about user events straight into Shiny without having to code in JavaScript at all – things like the mouse coordinates of a mouse click or what key the user pressed on the keyboard. This was requested several times in the past, and I finally got around to implementing it.</p>
</li>
<li>
<p>The <code>reset()</code> function now works on file inputs. How to reset file inputs is a frequently asked question not only in Shiny, but in web development in general. I’ve seen this question come up many times in different Shiny forums, and hopefully this will work well for most users.</p>
</li>
</ol>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/useful-tools-for-shiny-developers-in-the-new-version-of-shinyjs/"></div></div><div><a data-url="https://www.r-bloggers.com/useful-tools-for-shiny-developers-in-the-new-version-of-shinyjs/" data-counturl="https://www.r-bloggers.com/useful-tools-for-shiny-developers-in-the-new-version-of-shinyjs/" data-count="vertical" data-text="Useful tools for shiny developers in the new version of shinyjs" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://deanattali.com/blog/shinyjs-v08/"> Dean Attali's R Blog</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/RsqxkD2ZOow"></a></div>