Skip to content

Version of add_module for {tidymodules} #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DivadNojnarg opened this issue Feb 20, 2020 · 1 comment · Fixed by #505
Closed

Version of add_module for {tidymodules} #365

DivadNojnarg opened this issue Feb 20, 2020 · 1 comment · Fixed by #505

Comments

@DivadNojnarg
Copy link
Contributor

Hi All,

Once {tidymodules} is on CRAN (end of year?), we would like to know if you are interested to add a parameter to the add_module function so that it creates a R6 class template instead of the default.
We can develop the feature for you.

An example of what it could produce:

# module R6Class ---------------------------------

#' R6 Class representing the module
#'
#' @description
#' UI and server code necessary for the module
#' @details
#' More details
#'
#' @export
module <- R6::R6Class(
  "module",
  inherit = tidymodules::TidyModule,
  public = list(
    #' @field nested_mod nested modules. This is updated by the initialize function.
    nested_mod = list(),
    
    #' @description
    #' Module initialization
    #' @param ... NULL by default.
    #' @return A new `module` object.
    initialize = function(...) {
      super$initialize(...)
      
      # initialize nested modules
      # self$nested_mod[[1]] <- Nested$new(inherit = TRUE)
      
      # define ports
      self$definePort({
        self$addInputPort(
          name = "iport1",
          description = "My beautiful input",
          sample = list()
        )
        
        self$addOutputPort(
          name = "oport1",
          description = "My uggly output",
          sample = tibble()
        )
        
      })
      
    },
    
    #' @description
    #' Create the module UI
    #' @return ...
    ui = function() {
      tagList(
        # UI elements go here
      )
    },
    
    #' @description
    #' Server function description
    #' @param input Shiny input object.
    #' @param output Shiny output object.
    #' @param session Shiny session object.
    #' @return The server function.
    server = function(input, output, session) {
      
      super$server(input, output, session)
      
      # call all nasted modules below
      # self$nested_mod[[1]]$callModule()
      
      # update output ports
      self$assignPort({
        # self$updateOutputPort(...)
      })
      
    }
  )
)
@ColinFay
Copy link
Member

I think the safe way to do that would be through the idea of golem templating.

In order to implement that feature, one thing I can imagine would be to have a "module template" function that returns the content of the module you want to create, in the spirit of add_module(name = "bla", template = tidymodules::module_template).

In my opinion that would be the best way to go for the following reasons:

  • you can keep your "freedom" for the format, meaning that if at some point the tidy module templates changes, you can change it inside the package instead of having to PR-ing golem, which would result in a potential version mismatch if a new version of golem is released long after a new version of tidymodules

  • that template format can be bootstrapped to other module formats, as long as the function returns what's needed, golem stays agnostic

  • in a sense, a tidymodules template function would work even outside golem

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants