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?
CodePudding user response:
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")
)
)
)
)
}