Site icon R-bloggers

Announcing ShinyTester – a package that helps you build Shiny apps

[This article was first published on R – AmitKohli.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Shiny is awesome, but can be a bit daunting and easy to make mistakes in. I recently came back to Shiny after a hiatus of a few years and it was much more challenging than I feel comfortable admitting. I was making bonehead mistakes like writing something instead of output$something, confusing where to put Output commands vs Render commands, etc. I would eventually find my mistake, curse myself and move on with a crumpled ego. Then I had the realization that maybe if I was a beginner, I wouldn’t even know what I was doing wrong. Thusly did I conclude that I was in a unique position to help out the R community: Dumb enough to make mistakes, but experienced enough to eventually remember how to resolve them. So naturally, I wrote an R package that tests the code of the Shiny app itself.

 

To install: install.packages("ShinyTester") cause yes, it’s on CRAN (my first!!! Shoutout to @jbkunst for invaluable help!)

 

Quick caveat: Since this package parses code and everyone writes code differently, it will necessarily be super buggy. If this package doesn’t work for your app (after reading the full list of caveats at the bottom of this post), please help by opening an Issue on the github repo.

 

The package consists of two functions that analyze the code itself:

It is my hope that both of these combined minimize the intrinsic boneheadedness in us all. This is really quite beta though… please do check the Caveats! In the meantime, some examples:

Examples for ShinyDummyCheck:

ShinyTester::ShinyDummyCheck("https://raw.githubusercontent.com/mexindian/ShinyServer/master/LineSelector")

Provides this table:

 

Which shows that there are no errors in the Shiny app, oh except for the fact that I defined an object twice… whoops (Yeah, see that’s exactly the boneheadedness I’m talkin bout). The structure of the table is as follows:

 

Examples for ShinyHierarchy:

A simple example:

library(ShinyTester)
ShinyHierarchy("https://raw.githubusercontent.com/rstudio/shiny-examples/master/003-reactivity")

Will yield:

image

 

Which shows one of the weaknesses of the function… it assumes all Item names are unique… and will act strangely if this assumption doesn’t hold (ie – caption).

 

A more complex example:

ShinyTester::ShinyHierarchy("https://raw.githubusercontent.com/mexindian/ShinyServer/master/LineSelector")

Yields:

And here we can start to see the structure that I’m attempting to show… there are basically three ROWS of nodes. The first one is the UI Inputs, the second row are the reactives (kinda…), and the third row are the outputs being visualized. I said the reactives are “kinda” the second row because I have introduced a small shift to each node in the middle row in order to see reactive flows into each other (if they are all in the same row, you can’t really see them). The structure is made evident in a more complex case below (forgive the redacted names):

If you want to suppress the shift in reactive nodes, use offsetReactives = F

Caveats:

This is a very naive app, and in early stages at that… it works best with my style of programming and will probably take significant work to universalize (since we’re talking about code… maybe it’s impossible to fully universalize).  Some other caveats:

Other tips for working in Shiny:

 

Enjoy!

(Thanks to my rusers community, especially to Joshua Kunst and Colin Phillips for discussion, help and encouragement required to push this through to CRAN).

To leave a comment for the author, please follow the link and comment on their blog: R – AmitKohli.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.