cat_r_file <- function(file){ cat("```R", readLines(file), "```", sep = "\n") }
Shiny modules make it much easier to design and build complex shiny apps.
Imagine that you are designing an app that requires the user to upload a csv file to be parsed into a data frame. You will wish to use the data frame elsewhere in the app. Instead of writing the parser from scratch, you can use a set of shinypod functions in your app.
Shinypod is simply an implementaton of a set of design guidelines to allow you to use, remix, and even build your own shiny modules. An advantage of using such guidelines is that we can all use each others' shinypods more quickly and reliably.
Before getting started, you are referred to RStudio's article on shiny modules.
This package, shinypod, offers functions that you can use to put a CSV parser into your app.
Let's look at a ui.R
file:
cat_r_file(system.file("shiny", "read_delim", "ui.R", package = "shinypod"))
And a server.R
file:
cat_r_file(system.file("shiny", "read_delim", "server.R", package = "shinypod"))
For this implementation we call upon three functions.
read_delim_sidebar_side()
read_delim_sidebar_main()
read_delim_server()
The first two functions each return a named shiny::tagList
of UI elements that can be used in a sidebar layout. The side panel contains the inputs, and the main panel contains the outputs.
Other UI arrangements are possible; you are referred to the "remixing-shinypods" vignette.
The third function is used in the server function to return a reactive element. This reactive returns the parsed dataframe.
To use all three, all you have to do is call each of the three functions using the same id
, in this case "csv"
- this is to keep the shiny namespace tidy.
If you like, try out the deployed app.
In this example, we will look at what you can do by combining shinypods. Here, we will combine the pod used to parse a csv with a pod used to build a dygraph.
Dygraphs are especially handy for visualizing time series. Often, time-series data may be available in a data frame (parsed from a csv), and we wish to visualize it. This is where we would use a set of dygraphs functions from shinypod.
We can build onto our previous example by adding a dyraph shinypod.
Let's look at a ui.R
file:
cat_r_file(system.file("shiny", "read_delim_dygraph", "ui.R", package = "shinypod"))
And a server.R
file:
cat_r_file(system.file("shiny", "read_delim_dygraph", "server.R", package = "shinypod"))
A few things to note:
"csv"
) as for the dygraph pod ("dyg"
).read_delim_server()
function as an argument to the dygraph_server()
function. This is how the data is moved around.dygraph_server()
function returns a reactive that returns a dygraph. We use this to build the output for the app. Note also that the dygraph shinypod returns a basic dygraph; we can use functions from the dygraphs package to customize it.If you like, try out the deployed app.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.