introduction.Rmd
There currently are two packages that allow useRs to build web apps with R: Shiny and OpenCPU. They both have some drawbacks. While Shiny makes it simple to create inputs and outputs, it requires the useR to write Shiny specific code. On the other hand, OpenCPU does not provide any boilerplate functions to create input and output elements. So, useRs need to write JavaScript and html to create a functional app. However, OpenCPU is an independent platform and can be scaled without using the proprietary Shiny Server Pro.
This is where appifyr comes in. It tries to make the OpenCPU engine more accessible for useRs with little to none knowledge of JavaScript and html.
Appifyr makes it easy to build apps. Appifyr takes package functions or functions in your rmarkdown document (experimental) and turns them into an app. It does this by using JavaScript and html templates. The function arguments will be linked to the html-form input. Appifyr creates html-id-tags to identify the form-input. When you click submit the values are read from the form and a request is sent to the OpenCPU server. The OpenCPU server then runs the R-code. The R-function’s output is then handled by the Javascript callback and displayed to the user.
This video shows a timelapse workflow (from R-markdown)
First, you need to install devtools
from CRAN.
install.packages("devtools")
Second, install appifyr
. It has a fair amount of package dependencies in its own right, these will be installed automatically.
devtools::install_github("retowyss/appifyr")
Third, install OpenCPU. It too has a bunch of dependencies. If you are using Ubuntu/Debian you might have to install some additional system libraries.
install.packages("opencpu")
If you want to create an app with appifyr you will be creating an R-package. Appifyr just writes Javascript and html for you. The app runs on the OpenCPU server. OpenCPU can only make package functions available. There are excellent resources out there to learn about R-package, my favorite one is Hadley Wickhams R-Package book. It’s a good idea to familiarize yourself with the basics of writing R-package.
Currently appifyr only supports plots for a return value. But it has flexibel input definitions.
The support for Rmarkdown to R-package conversion is experimental. There are some cases where it will not work, e.g. you can’t read a file like you normally would nor can you access variables in the global environment. The function needs to be self-contained.
For more reliable behavior you might want to write your function directly as R-package functions (R/
) and save your data as .Rda
files in the package’s data/
directory. You should use the template created by appifyr and delete the code in the Rmarkdown index file.
When writing your functions as package functions in your (R/
) directory use build_app()
to build the app and use roxygen2 commenting style.
To learn more about how to use appifyr head over to Sepals and Petals. It’s an app created with appifyr and runs on the OpenCPU cloud server. There are a lot of examples. It’s also a good idea to have a look at Sepals and Petals code on Github. Its functions are written in R-package style. Feel free to clone it and use it as a template for your own app.
Publishing your own app to the OpenCPU cloud server is simple. You only need to set a webhook in your Github repo: Instructions.