Curtis
Curtis

Reputation: 181

Using NS Modules With shinymanager and rhino

I've created an rhino R Shiny app and would like to add shinymanager auth. Adding shinymanager means I cannot use modules because I cannot use ns <- NS(id) as id is not defined.

Based on the documentation adding shinymanager is done by wrapping the ui in shinymanager$secure_app():

# app/main.R
box::use(
  shiny,
  shinymanager,
)

# Define your `check_credentials` function.
# This is just an example. Do not hard-code the credentials in your actual application.
check_credentials <- shinymanager$check_credentials(
  data.frame(user = "admin", password = "admin")
)

#' @export
ui <- shinymanager$secure_app( # Wrap your entire UI in `secure_app()`.
  shiny$bootstrapPage(
    shiny$textInput("name", "Name"),
    shiny$textOutput("message")
  )
)

#' @export
server <- function(input, output) {
  # Call `secure_server()` at the beginning of your server function.
  shinymanager$secure_server(check_credentials)
  output$message <- shiny::renderText(paste0("Hello ", input$name, "!"))
}

The issue is that my .main file contains multiple modules such as this:

# app/main.R

box::use(
  shiny[bootstrapPage, moduleServer, NS],
)
box::use(
  app/view/chart,
)

#' @export
ui <- function(id) {
  ns <- NS(id)

  bootstrapPage(
    chart$ui(ns("chart"))
  )
}

#' @export
server <- function(id) {
  moduleServer(id, function(input, output, session) {
    chart$server("chart")
  })
}

How do I use NS for a module like chart$ui(ns("chart")) when the UI is wrapped in the shinymanager$secure_app( and ns <- NS(id) returns an error because id is not defined?

The code would be something like this:

# app/main.R
box::use(
  shiny,
  shinymanager,
)

# Define your `check_credentials` function.
# This is just an example. Do not hard-code the credentials in your actual application.
check_credentials <- shinymanager$check_credentials(
  data.frame(user = "admin", password = "admin")
)

#' @export
ui <- shinymanager$secure_app( # Wrap your entire UI in `secure_app()`.

  ns <- NS(id)

  shiny$bootstrapPage(
    shiny$textInput(ns("name"), "Name"),
    shiny$textOutput(ns("message")),
    chart$ui(ns("chart"))
  )
)

#' @export
server <- function(input, output, session) {
  # Call `secure_server()` at the beginning of your server function.
  shinymanager$secure_server(check_credentials)
  output$message <- shiny::renderText(paste0("Hello ", input$name, "!"))
  
  ns <- session$ns
  chart$server("chart")
}

Here are the rhino tutorial links:

https://appsilon.github.io/rhino/articles/how-to/use-shinymanager.html

https://appsilon.github.io/rhino/articles/tutorial/create-your-first-rhino-app.html

Maybe there is another way to use a module in the .main file without using NS? I feel like there should be an easy solution that I'm just not thinking of.

Upvotes: 0

Views: 80

Answers (0)

Related Questions