R Shiny - nested Module not triggering events in server - r

I am trying to create an R Shiny app which requires nested modules. My code is functional when calling a single nested module but fails when I go one level deeper. The ui component is being created without issue, but the buttons etc. do not work within this module.
I have attached a simple example. Here, when you press the first button, it calls a module and creates a second button within a wellPanel. This is then repeated if this second button is pressed. However, I would expect that when I press the third button, I would get the string
Third button Pressed
printed to screen.
Any suggestions?
library(shiny)
# Second Level Mod --------------------------------------------------------
second_mod_ui <- function(id) {
ns <- shiny::NS(id)
fluidPage(
wellPanel(
actionButton(ns('addButton3'), '', icon = icon('plus'))
)
)
}
second_mod_server <- function(input, output, session) {
observeEvent(input$addButton3, {
print("Third button Pressed")
})
}
# First Level Mod ---------------------------------------------------------------
first_mod_ui <- function(id) {
ns <- shiny::NS(id)
shiny::uiOutput(ns("showButton"))
}
first_mod_server <- function(input, output, session) {
ns <- session$ns
output$showButton <- renderUI({
fluidPage(
wellPanel(
actionButton(ns('addButton2'), '', icon = icon('plus'))
)
)
})
observeEvent(input$addButton2, {
i <- sprintf('%04d', input$addButton2)
id <- sprintf('Button2%s', i)
insertUI(
selector = paste0('#', ns("addButton2")),
where = "beforeBegin",
ui = second_mod_ui(id)
)
callModule(second_mod_server, id)
})
}
# Main App ----------------------------------------------------------------
ui <- fluidPage(
br(),
actionButton('addButton1', '', icon = icon('plus'))
)
server <- function(input, output) {
# If the Plus button has been pressed
observeEvent(input$addButton1, {
# Create a new id
i <- sprintf('%04d', input$addButton1)
id <- sprintf('Button1%s', i)
# Insert the new UI
insertUI(
selector = '#addButton1',
where = "beforeBegin",
ui = first_mod_ui(id)
)
# Call the server
callModule(first_mod_server, id)
})
}
shinyApp(ui = ui, server = server)

Related

Modularing Shiny - Pop-up box

In my shiny-app I use pop-up boxes to allow the user to quickly look up data in a table. That works well using showModal, however currently I'm creating some module servers and there I run into issues. What I understood, is that the id in a module ui needs to be defined using ns (as is done below for the action button).
However, my data table inside the pop-up box is defined inside the moduleServer. If I use ns("my_database") there, I get the message that ns is not a recognized function.
How can I get that table to show inside my pop-up box, embedded in a moduleServer?
library(shiny)
dataPage <- function(id, label = "Counter") {
ns <- NS(id)
tagList(
column(12,
h1("Testing my database pop up"),
actionButton(ns("show_database"), "Show my database")
)
)
}
dataServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
observeEvent(input$show_database, { # Pop-up box, currently for testing purposes
showModal(modalDialog(
title = "My database",
"My database",
dataTableOutput("my_database"),
easyClose = TRUE,
footer = NULL
))
})
output$my_database <- renderDataTable({
temp <- data.frame(a = seq(1,10), b = letters[1:10])
temp
})
}
)
}
ui <- fluidPage(
dataPage("data1", "Counter #1")
)
server <- function(input, output, session) {
dataServer("data1")
}
shinyApp(ui, server)

How to access reactive value in parent module?

What I want to achieve is to get access to the reactive value passed to a parent module from a child module. The reproducible example below shows the idea. When i click the button in mod_server_btn then its value should be printed out in the console (from within parent module):
library(shiny)
mod_ui_btn <- function(id, label = "ui1UI") {
ns <- NS(id)
shinyUI(fluidPage(
actionButton(ns("confirm"), "Submit", class='btn-primary')
))
}
mod_server_btn <- function(input, output, session) {
cond <- reactive({ input$confirm})
return(cond)
}
ui =fluidPage(
mod_ui_btn("test"),
uiOutput("example")
)
server=shinyServer(function(input, output, session) {
value <- callModule(mod_server_btn,"test")
print(value)
#print(value$cond) # these 3 don't work either
#print(value()$cond)
#print(value())
})
shinyApp(ui=ui,server=server)
However, it doesn't work. When I click the button then I got a text: reactive({input$confirm}) in the console and it's not what I want, I need to access button value. General question is - is it possible at all to get access to reactive value in a parent module?
EDIT: #rbasa, #YBS thanks for your answers. In fact in my real app I need to return more than one reactive value to parent module. Below is slightly changed code - I added second button in mod_ui_btn - now I need to return values from both buttons to the server module. I made a list of reactives but can't get access to them using observe or output$example <-:
library(shiny)
mod_ui_btn <- function(id, label = "ui1UI") {
ns <- NS(id)
shinyUI(fluidPage(
actionButton(ns("confirm"), "Submit", class='btn-primary'),
actionButton(ns("confirm2"), "Submit2", class='btn-primary')
))
}
mod_server_btn <- function(input, output, session) {
return(
list(
cond = reactive({ input$confirm}),
cond2 = reactive({ input$confirm2})
)
)
}
ui =fluidPage(
mod_ui_btn("test"),
verbatimTextOutput("example"),
verbatimTextOutput("example2")
)
server=shinyServer(function(input, output, session) {
value <- callModule(mod_server_btn,"test")
output$example <- renderPrint(value$cond)
output$example2 <- renderPrint(value$cond2)
observe({
print(value$cond) #this is how I usually catch reactives - by their name
print(value$cond2)
})
})
shinyApp(ui=ui,server=server)
I usually use return(list(..some reactive values)) to return more than one ractive value to other module and catch then using their names in parent module. Here it doesn't work even if I use observe. No value is returned.
You can access with value(). I would recommend to change your mod_server_btn to the one shown below, and notice the call in server. EDIT: updated for multiple variables. Try this
library(shiny)
mod_ui_btn <- function(id, label = "ui1UI") {
ns <- NS(id)
shinyUI(fluidPage(
actionButton(ns("confirm"), "Submit", class='btn-primary'),
actionButton(ns("confirm2"), "Submit2", class='btn-primary')
))
}
mod_server_btn <- function(id) {
moduleServer(id, function(input, output, session) {
return(
list(
cond = reactive(input$confirm),
cond2 = reactive(input$confirm2)
)
)
})
}
ui =fluidPage(
mod_ui_btn("test"),
verbatimTextOutput("example"),
verbatimTextOutput("example2")
)
server=shinyServer(function(input, output, session) {
# value <- callModule(mod_server_btn,"test")
value <- mod_server_btn("test")
output$example <- renderPrint(value$cond())
output$example2 <- renderPrint(value$cond2())
observe({
print(value$cond()) #this is how I usually catch reactives - by their name
print(value$cond2())
})
})
shinyApp(ui=ui,server=server)

Shiny namespace issue with toggle and tabsetPanel

I’m looking for some help with a simple Shiny app with a modularised design please. I think the problem is a name space issue so the example below is set out as a simplified version of my actual project.
The aim is for ‘tab_3’ on the tabsetPanel to only show when the ‘View Tab_3’ is checked, which works fine. I would like to update the tabsetPanel however to also select ‘tab_3’ when ‘View Tab_3’ is checked and this is not firing as desired.
I can get the tabsetPanel to also select ‘tab_3’ when ‘View Tab_3’ is checked if I wrap the tabsetPanel’s id in a namespace function, id = ns("tab_a_tha"), however then I lose the show/hide functionality of ‘tab_3’.
My hunch is that the solution lies within providing a namespace to the toggle function but I haven’t found any clues on how to approach it.
library(shiny)
library(shinyjs)
inner_moduleUI <- function(id){
ns <- NS(id)
tagList(
fluidRow(checkboxInput(ns("chckbx"), "View Tab_3", value = F)),
tabsetPanel(
id = "tab_a_tha",
# id = ns("tab_a_tha"),
tabPanel('tab_1'),
tabPanel('tab_2'),
tabPanel('tab_3')
)
)
}
inner_module <- function(input, output, session){
observeEvent(input$chckbx, {
toggle(condition = input$chckbx, selector = "#tab_a_tha li a[data-value=tab_3]")
if(input$chckbx == T){
updateTabsetPanel(session, 'tab_a_tha', selected = 'tab_3')
}
})
}
ui <- fluidPage(
useShinyjs(),
uiOutput('main_ui')
)
server <- function(input, output, session) {
output$main_ui <- renderUI({inner_moduleUI('inner_ns') })
callModule(inner_module, 'inner_ns')
}
shinyApp(ui = ui, server = server)
you are right the problem is with the namespace. The trick is that you can access the namespace function also in the server part of a module with session$ns.
Using this and wrapping the tap id in the ns function. We can use paste0 to generate the new selector of the toggle function. We get something like this:
library(shiny)
library(shinyjs)
inner_moduleUI <- function(id){
ns <- NS(id)
tagList(
fluidRow(checkboxInput(ns("chckbx"), "View Tab_3", value = F)),
tabsetPanel(
id = ns("tab_a_tha"),
# id = ns("tab_a_tha"),
tabPanel('tab_1'),
tabPanel('tab_2'),
tabPanel('tab_3')
)
)
}
inner_module <- function(input, output, session){
observeEvent(input$chckbx, {
toggle(condition = input$chckbx, selector = paste0("#",session$ns("tab_a_tha")," li a[data-value=tab_3]"))
if(input$chckbx == T){
updateTabsetPanel(session, 'tab_a_tha', selected = 'tab_3')
}
})
}
ui <- fluidPage(
useShinyjs(),
uiOutput('main_ui')
)
server <- function(input, output, session) {
output$main_ui <- renderUI({inner_moduleUI('inner_ns') })
callModule(inner_module, 'inner_ns')
}
shinyApp(ui = ui, server = server)

Accessing parent namespace inside a Shiny Module

I'm trying to updateSelectInput on a selectInput from the parent namespace inside a sub-module. In the module function, I'm inside the namespace as far as I understand, and therefore I can't access and update the selectInput from the parent namespace. How can I solve this?
library(shiny)
library(shinydashboard)
moduleUI <- function(id) {
ns <- NS(id)
box(
title=actionLink(ns("link"),"This is a link"),
plotOutput(ns("plot"))
)
}
module <- function(input, output,session,number) {
output$plot <- renderPlot({
plot(number)
})
observeEvent(input$link,{
print(paste0("Number is: ",number))
updateSelectInput(session,"selectInput",selected=number) #Doesn't work
})
}
ui <-
dashboardPage(
dashboardHeader(title="Title"),
dashboardSidebar(
selectInput("selectInput","Choose one option",choices=seq(1,10),selected=1)
),
dashboardBody(
moduleUI("5"),
moduleUI("10")
)
)
server <- function(session,input, output) {
callModule(module=module,id="5",5)
callModule(module=module,id="10",10)
}
shinyApp(ui = ui, server = server)
Took me a while, but I found a way to get the sub-module to update the super-module.
Shiny is designed so that access to other modules must be done via module arguments or returned values. We can not pass the widget ID between modules, but we can pass the session information of the parent.
library(shiny)
moduleUI <- function(id) {
ns <- NS(id)
uiOutput(ns("my_link"))
}
module <- function(input, output, session, number, parent) {
output$my_link <- renderUI({
actionLink(session$ns("link"), paste0("This is a link to ", number))
})
observeEvent(input$link,{
updateSelectInput(session = parent,"selectInput",selected = number) ### use parent session
})
}
ui <- fluidPage(
selectInput("selectInput","Choose one option",choices=seq(1,10),selected=1),
moduleUI("5"),
moduleUI("10")
)
server <- function(session,input, output) {
callModule(module = module, id = "5", 5, parent = session) ### pass session information
callModule(module = module, id = "10", 10, parent = session) ### pass session information
}
shinyApp(ui = ui, server = server)
In particular note that:
we pass the current session information when the sub-module is called
we use the parent session when updating the input selector
I think the ideal would be to get the sub-module to observe and update the super-module. However, I can only offer a solution in line with my comment above: having one observer per sub-module in the super-module. This will rapidly get cumbersome if you have many sub-modules.
library(shiny)
library(shinydashboard)
moduleUI <- function(id) {
ns <- NS(id)
box(
title=actionLink(ns("link"),"This is a link"),
plotOutput(ns("plot"))
)
}
module <- function(input, output,session,number) {
current = reactiveValues()
current$return_value = 0
returnvalue <- reactive(current$return_value)
output$plot <- renderPlot({
plot(number)
})
observeEvent(input$link,{
print(paste0("Number is: ",number))
current$return_value = current$return_value + 1
})
return(list(rv = returnvalue, num = number))
}
ui <-
dashboardPage(
dashboardHeader(title="Title"),
dashboardSidebar(
selectInput("inputID","Choose one option",choices=seq(1,10),selected=1),
actionButton("button","Knap")
),
dashboardBody(
moduleUI("5"),
moduleUI("10")
)
)
server <- function(session,input, output) {
val1 <- callModule(module=module,id="5",5)
val2 <- callModule(module=module,id="10",10)
observeEvent(val1$rv(),{
updateSelectInput(session,inputId="inputID",selected=val1$num)
})
observeEvent(val2$rv(),{
updateSelectInput(session,inputId="inputID",selected=val2$num)
})
}
shinyApp(ui = ui, server = server)
Key changes from Tobias's question:
sub-modules have different names
separate observer for each sub-module
sub-module contains a return_value that gets updated every time the link is clicked. This ensures the observer in the super-module has a change to observe.
sub-module returns a list with two values: return_value as described above, and the value to update the UI with.

Update output in dynamic module - R Shiny

I have a code that allows to dynamically add modules in a Shiny app. This module is composed of a selectInput and can be added by clicking on the "Add filter" Button.
What I try to do is to put text at the right of each selectInput widget which value update when the user click on the perform Button and is equal to the selection on the selectInput
I don't know how to do. Many tries were unsuccessfull...
The code is the following :
library(shiny)
moduleFilterUI <- function(id) {
ns <- NS(id)
uiOutput(ns("SymbolicFilter"))
}
moduleSymbolicFilter <- function(input, output, session) {
output$SymbolicFilter <- renderUI({
fluidRow(
column(width = 4, selectInput(session$ns("cname"), "Column name", choices = c(1:5)))
)
})
}
ui <- fluidPage(
fluidRow(
actionButton("addSymbolicFilterModule", "Add filter"),
actionButton("Filter", "Perform"),
uiOutput("symbolicFilters"))
)
)
server <- function(input, output, session) {
symbolicFilterModules <- list()
makeReactiveBinding("symbolicFilterModules")
observeEvent(input$addSymbolicFilterModule, {
duplicateSymbolicFilterid <- paste0("duplicateSymbolicFilter", input$addSymbolicFilterModule)
symbolicFilterModules <<- c(symbolicFilterModules, list(moduleSymbolicFilterUI(duplicateSymbolicFilterid)))
callModule(moduleSymbolicFilter, duplicateSymbolicFilterid)
shinyjs::disable("addSymbolicFilterModule")
iLast <- length(symbolicFilterModules)
for (i in 1:(iLast-1)){
duplicateSymbolicFilterid <- paste0("duplicateSymbolicFilter", i)
updateSelectInput(session, paste0(duplicateSymbolicFilterid,"-cname"),
selected=input[[paste0(duplicateSymbolicFilterid,"-cname")]])
}
})
observeEvent(input$Filter,{
shinyjs::enable("addSymbolicFilterModule")
iLast <- length(symbolicFilterModules)
duplicateSymbolicFilterid <- paste0("duplicateSymbolicFilter", iLast)
cname <- input[[paste0(duplicateSymbolicFilterid,"-cname")]]
for (i in 1:(iLast)){
duplicateSymbolicFilterid <- paste0("duplicateSymbolicFilter", i)
updateSelectInput(session, paste0(duplicateSymbolicFilterid,"-cname"),
selected=input[[paste0(duplicateSymbolicFilterid,"-cname")]])
}
})
output$symbolicFilters <- renderUI({
symbolicFilterModules
})
}
shinyApp(ui = ui, server = server)
maybe you had already solved the problem, but...
you named the module moduleFilterUI, but you call moduleSymbolicFilterUI...

Resources