I'm trying to create a package of a shiny app organized with module. I can't find any help, every example which i can find doesn't use module. Someone can give me an example ?
-
2Modules are functions. You can define the module function just like you do a function when building a package. (and this is suggested in https://shiny.rstudio.com/articles/modules.html). You can look at this question to get some guidance on the mechanics of such a package https://stackoverflow.com/questions/37830819/developing-shiny-app-as-a-package-and-deploying-it-to-shiny-server – Benjamin Jul 07 '17 at 12:23
2 Answers
You will have to make sure the ui
part as well as the server
part of the module get exported in your package
#' @export
myModuleUI <- function(id){
ns = NS(id)
plotOutput(ns("plot"))
}
#' @export
myModuleServer <- function(input, output, session){
output$plot <- renderPlot({hist(rnorm(100))})
}
From the script that loads your packae you can then use
library(myPackage)
shinyApp(
fluidPage(myModuleUI("someId")),
function(input,output,session){
callModule(myModuleServer, "someId")
}
)
When it comes to documenting your modules, there is no standard way of doing this. Most packages use a minimal function-like documentation together with an example app -- see the answer of Joris Meys.

- 7,397
- 1
- 26
- 43
-
don't write that convenience wrapper, you can get into all kinds of environment trouble. – Joris Meys Jul 07 '17 at 12:36
-
I am currently using a similar pattern in my package and everything works fine so far. Can you please elaborate more how this can cause troubles? – Gregor de Cillia Jul 07 '17 at 12:40
-
2I was using a similar pattern in my package, but when using nested modules it messed up the id links sometimes. So I deleted it. I have to add that I use constructor functions that create apps with similar layout based on different modules and allows the user to specify which module they want using a character string. So my life is a bit more complex, and that's when the issues start. – Joris Meys Jul 07 '17 at 12:53
-
Thanks for sharing these insights. I haven't touched nested modules so far but if I do, this might save me a lot of time. I edited my answer to remove the convenience wrapper. – Gregor de Cillia Jul 07 '17 at 13:39
Gregor de Cillia gives you a good example of how the UI and server function can be constructed and should be exported. A few other things to take into account before the package is completely functional:
- As Gregor said, exporting these functions will make them available for the end user.
- Make sure you add the
shiny
package to the Depends list in the description file ( see also Package Dependencies in the R manual ). It makes a lot more sense to add it to the depends list compared to importing theshiny
package, as all shiny functions should also be available to the end user and not solely your module functions. The standard is now to use import rather than depends, but in this case Depends makes a lot more sense.
For making your package top-notch, add a small example that illustrates your module. Take the example of Gregor, then you add a folder inst/examples/moduleExample/
to your package, and in there you have a file app.R
as follows:
shinyApp(
fluidPage(myModuleUI("someId")),
function(input,output,session){
callModule(myModuleServer, "someId")
}
)
Now you add eg a moduleDemo
function that illustrates the use of the module.
#' @export
moduleDemo <- function(){
loc <- system.file("examples","moduleExample",
package = "myPackage")
shinyAppDir(loc)
}

- 106,551
- 31
- 221
- 263
-
2Maybe it is a better idea to use the path `examples/moduleExample/app.R` (and `shinyAppDir`) for the example. The reason for that is [this PR](https://github.com/rstudio/shiny/pull/1458) – Gregor de Cillia Jul 07 '17 at 13:20
-
@GregordeCillia true that, adapted accordingly and thanks for the tip. – Joris Meys Jul 07 '17 at 13:45