0

My idea is to create multiple elements that have radioGroupButtons and plotlyOutput in a module file. To avoid repetitions I created a function like this bellow:

tabpanel_content <- function(radio_id, plotly_id){


  div(
    id = "div_1",
    style = "",

    div(
      class = "inputs ",
      style = "width: 100%",

      radioGroupButtons(
        inputId = ns(radio_id),
        label = "",
        choices = c("Input_1","Input_2"),
        status = "success"
      )
    ),

    div(style = "",

        plotlyOutput(ns("plotly_id"), height = 200,width = "auto"))
  )
}

the changes are on radioGroupButtons and plotlyOutput ids.

Then I include this function on the module file:

mod_1_UI <- function(id) {
  ns <- NS(id)
  tagList(

    tabsetPanel(

      tabPanel(
        strong("TabPanel - Title"),

        tabpanel_content(radio_id = 'radio_input_1',plotly_id = 'plotly_chart_1'),
        tabpanel_content(radio_id = 'radio_input_2',plotly_id = 'plotly_chart_2'),
        tabpanel_content(radio_id = 'radio_input_3',plotly_id = 'plotly_chart_3'),


  )))
}

mod_1_Server <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {

    }
  )
}

And then I include the module on the app file:

library(shiny)
library(shinyWidgets)


source(file = 'module_1.R')
source(file = 'input_functions_1.R')

ui <- fluidPage(

  mod_1_UI("mod_1")

)

server <- function(input, output, session) {
}

shinyApp(ui, server)

But I have this error Message: Error in ns(radio_id) : could not find function "ns"

I think the relation between my tabpanel_content function and the module function is the problem. To create something like this, is this the best practice?

Any help?

2
  • NS, not ns. And here you have to remove the quotes: ns("plotly_id"). Commented Jul 28, 2022 at 16:38
  • I am this message error here: Error in paste0(inputId, "-label") : cannot coerce type 'closure' to vector of type 'character' Commented Jul 28, 2022 at 16:43

1 Answer 1

2

Do not use ns in the function:

tabpanel_content <- function(radio_id, plotly_id) {
  div(
    id = "div_1",
    style = "",
    div(
      class = "inputs ",
      style = "width: 100%",
      radioGroupButtons(
        inputId = radio_id,
        label = "",
        choices = c("Input_1", "Input_2"),
        status = "success"
      )
    ),
    div(
      style = "",
      plotlyOutput(plotly_id, height = 200, width = "auto")
    )
  )
}

Use it in the module:

mod_1_UI <- function(id) {
  ns <- NS(id)
  tagList(
    tabsetPanel(
      tabPanel(
        strong("TabPanel - Title"),
        tabpanel_content(
          radio_id = ns("radio_input_1"), plotly_id = ns("plotly_chart_1")
        ),
        tabpanel_content(
          radio_id = ns("radio_input_2"), plotly_id = ns("plotly_chart_2")
        ),
        tabpanel_content(
          radio_id = ns("radio_input_3"), plotly_id = ns("plotly_chart_3")
        )
      )
    )
  )
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.