<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/adding-logging-to-a-shiny-app-with-loggit/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rmhogervorst.nl/cleancode/blog/2018/02/28/adding-json-logging-to-shiny-app.html"> Clean Code</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>This is a very short post with example code</em><br>
Over time when you move your shiny app from your computer to a server, you<br>
want to add some logging. Generally logging is defined in levels : INFO (everything you want to print),<br>
WARNING (it does not stop the application, but it could be a problem), and<br>
ERROR (fatal things).</p>
<p>Shiny server does already log all it’s actions to<br>
a file on the server, but that file can be hard to access.<br>
It would be nice if every app has its own logging, close to the app location.</p>
<p>The package <a rel="nofollow" href="https://github.com/ryapric/loggit" target="_blank">loggit</a> by Ryan J. Price,<br>
overwrites the normal message, warning and stop functions<br>
in R and replaces them with identically functioning functions, but the package<br>
ALSO writes to a file. Thus the loggit packages writes to a json file of your<br>
choosing and has nice timestamped data, which is extremely easy to parse.</p>
<h4>Some prerequisites:</h4>
<ul>
<li>you need to have the loggit package installed on your shiny-server</li>
<li>you need to create the logfile first (empty)</li>
</ul>
<h4>example code</h4>
<p>This is a non functioning app.r example.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>shiny</span><span>)</span><span>
</span><span>library</span><span>(</span><span>loggit</span><span>)</span><span>
</span><span>setLogFile</span><span>(</span><span>&quot;loggit.json&quot;</span><span>)</span><span>
</span><span>loggit</span><span>(</span><span>&quot;INFO&quot;</span><span>,</span><span> </span><span>&quot;app has started&quot;</span><span>,</span><span> </span><span>app</span><span> </span><span>=</span><span> </span><span>&quot;start&quot;</span><span>)</span><span>
</span><span>ui</span><span> </span><span>&lt;-</span><span> </span><span>fluidpage</span><span>(</span><span>
</span><span>#your ui here</span><span>
</span><span>)</span><span>
</span><span>server</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>ui</span><span>,</span><span> </span><span>server</span><span>,</span><span> </span><span>session</span><span>){</span><span>
</span><span>message</span><span>(</span><span>&quot;this is an message&quot;</span><span>)</span><span>
</span><span># it would be nice if you had some server logic here</span><span>
</span><span>session</span><span>$</span><span>onSessionEnded</span><span>(</span><span>function</span><span>(){</span><span>loggit</span><span>(</span><span>&quot;INFO&quot;</span><span>,</span><span> </span><span>&quot;app has stopped&quot;</span><span>,</span><span> </span><span>app</span><span> </span><span>=</span><span> </span><span>&quot;stop&quot;</span><span>)})</span><span>
</span><span>}</span><span>
</span><span>shinyApp</span><span>(</span><span>ui</span><span>,</span><span> </span><span>server</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>This example has the ui and server defined and a function shinyApp() that<br>
starts the server.</p>
<p>There are 3 things I’d like to point your attention to:</p>
<ul>
<li>you first set your logfile location (you need to create it first)</li>
<li>I’ve used both loggit() and the general function message() They both write to loglevel INFO</li>
<li>I also used the session$onSessionEnded thingy, this is activated when you<br>
close the app</li>
</ul>
<p>Are all these things necessary? No absolutely not, you generally don’t want to<br>
know if an app has started, and stopped. You might only want to write the<br>
warnings and errors to the log.</p>
<p>I think the package is extremely user friendly and easy to implement, would you use this in production?</p>
<p>Did I miss anything? Did I make a mistake? open an issue on <a rel="nofollow" href="https://github.com/rmhogervorst/cleancode/issues" target="_blank">github</a>,<br>
or send me a message on <a rel="nofollow" href="https://twitter.com/RMHoge" target="_blank">twitter</a>.</p>
<p><a rel="nofollow" href="http://rmhogervorst.nl/cleancode/blog/2018/02/28/adding-json-logging-to-shiny-app.html" target="_blank">Adding logging to a shiny app with loggit</a> was originally published by at <a rel="nofollow" href="http://rmhogervorst.nl/cleancode" target="_blank">Clean Code</a> on February 28, 2018.</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://rmhogervorst.nl/cleancode/blog/2018/02/28/adding-json-logging-to-shiny-app.html"> Clean Code</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/SRwPrLceY2w" height="1"></a></div>