Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shiny app with module as a package

Tags:

r

shiny

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 ?

like image 481
Fitzzz Avatar asked Jul 07 '17 12:07

Fitzzz


2 Answers

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 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)
}
like image 175
Joris Meys Avatar answered Oct 10 '22 10:10

Joris Meys


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.

like image 7
Gregor de Cillia Avatar answered Oct 10 '22 09:10

Gregor de Cillia