Supplementing your R package with a Shiny app
来源:互联网 发布:淘宝网沙发套布料 编辑:程序博客网 时间:2024/05/12 19:14
The R community is generally very fond of open-source-ness and the idea of releasing all code to the public. Writing packages has become such an easy experience now that Hadley'sdevtools
is so powerful, and as a result there are new packages being released by useRs every single day.
A good package needs to have two things: useful functionality, and clear usage instructions. The former is a no-brainer, while the latter is what developers usually dread the most - the D-word (Documentation. Yikes.). Proper documentation is essential so that others will know what your package can do and how to do it. And with the use of Shiny, we now have another great tool we can use to showcase a package's capabilities.
Incorporating Shiny
In a nutshell, Shiny is a package that lets you run your R code as an interactive webpage. What this means for package developers is that you can have an interactive webpage that lets users experiment with your package and see what it can do before having to read through the potentially lengthy function documentations/vignette.
As an example, I recently released a package for adding marginal plots to ggplot2. You be the judge: after that one-sentence description of some functionality, would you rather go straight to theREADME, or see it in action firstin a Shiny app online? I might be wrong, but I think it's useful to interactively see what the package can do.
Making a Shiny app doesn't necessarily always make sense for every package, but there are certainly many times when it can be a great addition to a package's "documentation". I think that if a new package has some functions thatcan be easily illustrated in a simple Shiny app, it's worth it to take the extra 1-2 hours to develop it. This way, a user who finds your package and isn't quite sure what to do with it can try the Shiny app to see whether or not this is the functionality they were looking for. You can have several Shiny apps, each showing the usage of a particular function, or one app that is representative of a whole package. Whatever makes the most sense. Of course, having a Shiny app is in no way a replacement to documentation, it's just a useful add-on.
There are two ways to complement a package with a Shiny app that shows its main usage. These two methods are NOT mutually exclusive; I personally do both of them together:
1. Host the Shiny app online
You can host your Shiny app somewhre that is publicly available, such as shinyapps.io or on your own Shiny Server. Then you can include a link in the package's README or vignette or function documentation that points to the Shiny app.
As an example, I host my own Shiny Server where I can host my Shiny apps, and whenever I release a new package, I include a link in the README to a demo app.
The advantage of doing this is that people can play aroud with your package before even downloading it.
2. Include the app in the package and add a function to launch it
I recommend including the source code of the Shiny app in your package, and having a function such asrunExample()
that will launch the app. Here are the steps to do this (I've learned a lot from looking atshiny::runExample
source code - thanks RStudio):
First, add Shiny as a dependency in your DESCRIPTION
file (preferably under theSuggests:
field).
Then place your Shiny app folder under inst/shiny-examples/
and add an R file calledrunExample.R
. The package's tree structure should look like this
- mypacakge |- inst |- shiny-examples |- myapp |- ui.R |- server.R |- R |- runExample.R |- ... |- DESCRIPTION |- ...
Your runExample.R
will be simple - it will just look for the Shiny app and launch it
#' @exportrunExample <- function() { appDir <- system.file("shiny-examples", "myapp", package = "mypackage") if (appDir == "") { stop("Could not find example directory. Try re-installing `mypackage`.", call. = FALSE) } shiny::runApp(appDir, display.mode = "normal")}
Of course, don't forget to document this function! Now users can try out an app showcasing your package by runningmypackage::runExample()
.
This method can easily support more than one Shiny app as well, simply place each app underinst/shiny-examples/
and change the runExample code to something like this
runExample <- function(example) { # locate all the shiny app examples that exist validExamples <- list.files(system.file("shiny-examples", package = "mypackage")) validExamplesMsg <- paste0( "Valid examples are: '", paste(validExamples, collapse = "', '"), "'") # if an invalid example is given, throw an error if (missing(example) || !nzchar(example) || !example %in% validExamples) { stop( 'Please run `runExample()` with a valid example app as an argument.n', validExamplesMsg, call. = FALSE) } # find and launch the app appDir <- system.file("shiny-examples", example, package = "mypackage") shiny::runApp(appDir, display.mode = "normal")}
Now running runExample("myapp")
will launch the "myapp" app, and runningrunExample()
will generate a message telling the user what examples are allowed.
- Supplementing your R package with a Shiny app
- Supplementing your R package with a Shiny app
- #R#R presentation and Shiny package
- [iOS]Your package contains a file 'XXX.app/ XXX' with a name that contains leading or
- 【Shiny】基于R-Shiny制作地图App(百度地图)
- Dashboards in R with Shiny and GoogleVis
- Packaging your Shiny App as an Windows desktop app
- matlabr: a Package to Calling MATLAB from R with system
- matlabr: a Package to Calling MATLAB from R with system
- Integrating libtool with your package
- "Your package contains a file with a name that contains leading or trailing whitespace"解决方法
- A Simple Shiny App for Monitoring Trading Strategies
- R、ggplot2、shiny 汇总
- How to get your very own RStudio Server and Shiny Server with DigitalOcean
- App系列------Your content must have a ListView whose id attribute is 'android.R.id.list'
- Imputing missing data with R; MICE package
- make your driver as a deb package
- In-App Purch - Your first In-App Purchase(s) must be submitted with a new app version...问题解决方案
- Android应用名称多行显示
- [BZOJ1597]Usaco2008Mar 土地购买|斜率优化dp
- Intent传递对象
- 如何在三亚婚纱摄影中更好展示自己?三亚卡布奇诺时光婚纱摄影为你解答
- 在Python写中文
- Supplementing your R package with a Shiny app
- CentOS 各版本说明
- mysql 5.5版本安装
- OC三大特性
- 三亚婚纱摄影,燃起的晨曦---三亚卡布奇诺时光婚纱摄影
- django批量插入数据
- css 常用属性、布局
- MD5加密使用
- VS2012下基于Glut OpenGL显示一些立体图形示例程序