daattali / shinyjs

💡 Easily improve the user experience of your Shiny apps in seconds
https://deanattali.com/shinyjs
Other
734 stars 119 forks source link

Making runcode work inside Shiny modules #185

Closed mjhelf closed 5 years ago

mjhelf commented 5 years ago

Hi @daattali , these are the changes as discussed in #184 ...

The id argument is used in an unusual way here (because the enclosing module UI needs to be passed, instead of creating an identifier for a UI element), so maybe the argument name should be changed to something else to make that clearer?

Let me know if this needs more testing or adjustments,

Best, Max

Here is a minimal example, I wasn't sure whether it should be included in the documentation:

library(shiny)
library(shinyjs)

  ModuleEx <- function(input,output, session){
    runcodeServer()
  }

  ModuleExUI <- function(id){
    runcodeUI(id = id)
  }

  shinyApp(
    ui = fluidPage(
      useShinyjs(),  # Set up shinyjs
      ModuleExUI("Mod1")
    ),
    server = function(input, output) {
      callModule(ModuleEx, "Mod1")
    }
  )

And here is a more involved example that tests out the new possibilities:

library(shiny)
library(shinyjs)

  ModuleX <- function(input,output, session, 
                      aValue = reactive({})){
    runcodeServer()

  }

  ModuleXUI <- function(id, rcType = "text"){

    runcodeUI(code = "shinyjs::alert(aValue())",
              type = rcType,
              id = id)

    }

  shinyApp(
    ui = fluidPage(
      useShinyjs(),  # Set up shinyjs
      runcodeUI(code = "shinyjs::alert('Hello!')"),
      ModuleXUI("Mod1"),
      ModuleXUI("Mod2", rcType = "textarea"),
      ModuleXUI("Mod3", rcType = "ace")

    ),
    server = function(input, output) {
      runcodeServer()
      callModule(ModuleX, "Mod1", aValue = reactive({"This is Module#1"}))
      callModule(ModuleX, "Mod2", aValue = reactive({"This is Module#2"}))
      callModule(ModuleX, "Mod3", aValue = reactive({"This is Module#3"}))

    }
  )
daattali commented 5 years ago

Looks good.

The span tag with id "runcode_errorMsg" also needs to be namespaced. The documentation should be capitalized and a bit more concise: "When used inside a shiny module, the module's id needs to be provided to \code{runcodeUI}. This argument should remain \code{NULL} when not used inside a module."

Also please bump the version in the DESCRIPTION , and add an item to the NEWS file

mjhelf commented 5 years ago

Hi @daattali, thanks for the feedback and sorry for the late response! I made changes according to your comments. Let me know if this is ok, thank you!

daattali commented 5 years ago

Looks great, thanks!