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 ?
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:
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 the shiny
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)
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With