How to update input in shiny modlue from another shiny module? - r

I've got two shiny modules, with updateTextInput() in the first one. I want to update textInput() in the second module, when button from the first is clicked. I know it's because those modules are in different namespaces but I can't figure out how to communicate modules.
Reprex below :)
library(shiny)
firstUI <- function(id) {
ns <- NS(id)
tagList(
actionButton(ns("update"), "Update 1st and 2nd module"),
textInput(ns("first"), "Update me pls1", value = "Clear me!")
)
}
firstServer <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$update, {
updateTextInput(session = session, "first", value = "")
updateTextInput(session = session,"second", value = "")
})
})
}
secondUI <- function(id) {
ns <- NS(id)
tagList(
textInput(ns("second"), "Update me pls", value = "Clear me!")
)
}
secondServer <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$update, {
updateTextInput(session = session, "first", value = "")
updateTextInput(session = session,"second", value = "")
})
})
}
ui <- fluidPage(
firstUI("module_one"),
secondUI("module_two")
)
server <- function(input, output, session) {
firstServer("module_one")
secondServer("module_two")
}
shinyApp(ui, server)

You can do it by making the first input$update reactive, then returning that value and making it reactive to the second server module. This way the second server module is "listening" to the change in the first one.
library(shiny)
firstUI <- function(id) {
ns <- NS(id)
tagList(
actionButton(ns("update"), "Update 1st and 2nd module"),
textInput(ns("first"), "Update me pls1", value = "Clear me!")
)
}
firstServer <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$update, {
updateTextInput(session = session, "first", value = "")
updateTextInput(session = session,"second", value = "")
})
reactive(input$update)
})
}
secondUI <- function(id) {
ns <- NS(id)
tagList(
textInput(ns("second"), "Update me pls", value = "Clear me!")
)
}
secondServer <- function(id, clear) {
moduleServer(id, function(input, output, session) {
observeEvent(clear(), {
updateTextInput(session = session, "first", value = "")
updateTextInput(session = session,"second", value = "")
})
})
}
ui <- fluidPage(
firstUI("module_one"),
secondUI("module_two")
)
server <- function(input, output, session) {
clear <- reactive(firstServer("module_one"))
secondServer("module_two", clear())
}
shinyApp(ui, server)

A roundabout way would be to use shinyjs to trigger the updating manually.
library(shiny)
library(shinyjs)
firstUI <- function(id) {
ns <- NS(id)
tagList(
actionButton(ns("update"), "Update 1st and 2nd module"),
textInput(ns("first"), "Update me pls1", value = "Clear me!")
)
}
firstServer <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$update, {
updateTextInput(session = session, "first", value = "")
runjs('document.getElementById("module_two-second").value = ""')
})
})
}
secondUI <- function(id) {
ns <- NS(id)
tagList(
textInput(ns("second"), "Update me pls", value = "Clear me!")
)
}
secondServer <- function(id) {
moduleServer(id, function(input, output, session) {
# Code not needed in here for now
})
}
ui <- fluidPage(
useShinyjs(),
firstUI("module_one"),
secondUI("module_two")
)
server <- function(input, output, session) {
firstServer("module_one")
secondServer("module_two")
}
shinyApp(ui, server)
Shiny modules work by giving each element a unique id by pasting [module_name]-[element_id] together in the html frontend, so each module server can correctly identify which it should be talking to. The first server can find and talk to module_two-second when passed that id directly. Ideally there might be a way of doing this within the Shiny code itself though.
Edit: fix within Shiny by passing parent_session (without shinyjs)
The updateTextInput call can indeed find module_two-second itself if it can look outside of its own session environment. To achieve this, you can pass the parent_session as the argument to updateTextInput (defined in firstServer function definition and passed as parent_session = session in the server body):
library(shiny)
firstUI <- function(id) {
ns <- NS(id)
tagList(
actionButton(ns("update"), "Update 1st and 2nd module"),
textInput(ns("first"), "Update me pls1", value = "Clear me!")
)
}
firstServer <- function(id, parent_session) {
moduleServer(id, function(input, output, session) {
observeEvent(input$update, {
updateTextInput(session = session, "first", value = "")
updateTextInput(session = parent_session, "module_two-second", value = "")
})
})
}
secondUI <- function(id) {
ns <- NS(id)
tagList(
textInput(ns("second"), "Update me pls", value = "Clear me!")
)
}
secondServer <- function(id) {
moduleServer(id, function(input, output, session) {
# Code not needed in here for now
})
}
ui <- fluidPage(
firstUI("module_one"),
secondUI("module_two")
)
server <- function(input, output, session) {
firstServer("module_one", parent_session = session)
secondServer("module_two")
}
shinyApp(ui, server)

Related

ObserveEvent not triggered in nested module inserted with insertUI

I can create a module that inserts a button and triggers a browser() call inside an observeEvent() when the button is clicked:
library(shiny)
mod_ui <- function(id){
ns <- NS(id)
actionButton(ns("test"), "Test")
}
mod_server <- function(id) {
moduleServer(id, function(input, output, session){
ns <- session$ns
observeEvent(input$test, {
browser()
})
})
}
ui <- fluidPage(
mod_ui("mod_top")
)
server <- function(input, output, session){
mod_server("mod_top")
}
shinyApp(ui = ui, server = server)
But if I insert this module inside another module using insertUI, the browser call is no longer triggered:
library(shiny)
mod_ui <- function(id) {
ns <- NS(id)
div(
id = ns("place_here"),
actionButton(ns("add"), "Add")
)
}
mod_server <- function(id) {
moduleServer(id, function(input, output, session){
ns <- session$ns
observeEvent(input$add, {
insertUI(
immediate = TRUE,
selector = paste0("#", ns("place_here")),
where = "beforeEnd",
ui = mod_ui2(ns("mod_inner"))
)
mod_server2(ns("mod_inner"))
})
})
}
mod_ui2 <- function(id){
ns <- NS(id)
actionButton(ns("test"), "Test")
}
mod_server2 <- function(id) {
moduleServer(id, function(input, output, session){
ns <- session$ns
observeEvent(input$test, {
browser()
})
})
}
ui <- fluidPage(
mod_ui("mod_top")
)
server <- function(input, output, session){
mod_server("mod_top")
}
shinyApp(ui = ui, server = server)
How do I trigger the browser call?
Just needed to remove the namespacing of the nested server module:
mod_server2("mod_inner")
Here is the full working app:
library(shiny)
mod_ui <- function(id) {
ns <- NS(id)
div(
id = ns("place_here"),
actionButton(ns("add"), "Add")
)
}
mod_server <- function(id) {
moduleServer(id, function(input, output, session){
ns <- session$ns
observeEvent(input$add, {
insertUI(
immediate = TRUE,
selector = paste0("#", ns("place_here")),
where = "beforeEnd",
ui = mod_ui2(ns("mod_inner"))
)
mod_server2("mod_inner")
})
})
}
mod_ui2 <- function(id){
ns <- NS(id)
actionButton(ns("test"), "Test")
}
mod_server2 <- function(id) {
moduleServer(id, function(input, output, session){
ns <- session$ns
observeEvent(input$test, {
browser()
})
})
}
ui <- fluidPage(
mod_ui("mod_top")
)
server <- function(input, output, session){
mod_server("mod_top")
}
shinyApp(ui = ui, server = server)
Now I understand what you were trying to do. Needed to remove the ns() from mod_server2. Also, as pointed out in previous response, needed to correct for input$test.
library(shiny)
mod_ui <- function(id) {
ns <- NS(id)
div(
id = ns("place_here"),
actionButton(ns("add"), "Add")
)
}
mod_server <- function(id) {
moduleServer(id, function(input, output, session){
observeEvent(input$add, {
ns <- session$ns
insertUI(
immediate = TRUE,
selector = paste0("#", ns("place_here")),
where = "beforeEnd",
ui = mod_ui2(ns("mod_inner"))
)
mod_server2("mod_inner")
})
})
}
mod_ui2 <- function(id){
ns <- NS(id)
actionButton(ns("test"), "Test")
}
mod_server2 <- function(id) {
moduleServer(id, function(input, output, session){
observeEvent(input$test, {
browser()
})
})
}
ui <- fluidPage(
mod_ui("mod_top")
)
server <- function(input, output, session){
mod_server("mod_top")
}
shinyApp(ui = ui, server = server)

Shiny server function finds module when using fixed IDs but not when using ns()

I have a shiny app, each module is it's own file. Each module get's an ns <- NS(id). When I adress an Element, say a button from one of those modules with observeEvent it works if I just hardcode an ID in the module, but not if I use ns(). What am I doing wrong?
Module:
mod_add_element_ui <- function(id){
ns <- NS(id)
tagList(
shiny::actionButton(ns("add_element"), "add new element", icon = icon("plus-square"))
)
}
mod_add_element_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
})
}
app_ui:
app_ui <- function(request) {
tagList(
fluidPage(
mod_add_element_ui("add_element_ui_1"),
div(id="add_here")
)
)
}
app_server:
app_server <- function( input, output, session ) {
mod_add_element_server("add_element_ui_1")
observeEvent(input$add_element,
{
mod_add_element_server(id="mod")
insertUI(selector = "#add_here", ui = mod_add_element_ui("mod"))
}
)
}
Try this
mod_add_element_ui <- function(id){
ns <- NS(id)
tagList(
shiny::actionButton(ns("add_element"), "add new element", icon = icon("plus-square"))
)
}
mod_add_element_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
return(reactive(input$add_element))
})
}
app_ui <- function(request) {
tagList(
fluidPage(
mod_add_element_ui("add_element_ui_1"),
div(id="add_here")
)
)
}
app_server <- function( input, output, session ) {
added_element <- mod_add_element_server("add_element_ui_1")
observeEvent(added_element(),
{
mod_add_element_server(id="mod")
insertUI(selector = "#add_here", ui = mod_add_element_ui("mod"))
}
)
}
shinyApp(app_ui, app_server)

How do you have different server execution based on selected tabItem() in shiny?

Background
I am using {brochure} and {golem} to build a shiny app. I have one outer module grid that consists of inner modules subGrid2 which displays the same module UI on two tabs.
GOAL
have a module subGrid2 that can be used for repeating graph
visualizations on multiple tabs.
in the REPREX --> fake graph generated from {shinipsum} to
be displayed on the "Home" tab + "Portfolio" tab
use observeEvent to look at the slected tab and generate server response respectivley
Problem
The observeEvent reactive expr. fails to recognize when the corresponding tab is selected to generate the correct server response.
-using the reprex below replicates my issue-
TL/DR
Why wont the observeEvent reactive generate the correct server response per the selected tab?
REPREX
uncomment observeEvent to see error
#22.2.22
library(brochure)
library(shiny)
library(shinipsum)
library(shinydashboard)
library(shinydashboardPlus)
mod_subGrid2_ui <- function(id) {
ns <- NS(id)
tagList(
plotOutput(ns("plot"))
)
}
mod_subGrid2_server <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
output$plot <- renderPlot({
shinipsum::random_ggplot()
})
})
}
#Setup dashboard
mod_Grid_ui <- function(id) {
ns <- NS(id)
shinydashboardPlus::dashboardPage(
skin = "midnight",
header = dashboardHeader(title = "test"),
sidebar = dashboardSidebar(
shinydashboard::sidebarMenu(
# Setting id makes input$tabs give the tabName of currently-selected tab
id = "tabs",
menuItem("Home", tabName = "home", icon = icon("tachometer-alt")),
menuItem("Portfolio", tabName = "portfolio", icon = icon("chart-line"), badgeLabel = "new",
badgeColor = "green")
)
),
body = shinydashboard::dashboardBody(
# Enable shinyjs
shinyjs::useShinyjs(),
shinydashboard::tabItems(
shinydashboard::tabItem("home",
shiny::tagList(
div(p("Content for 1st tab goes here -- GRID MODULE")),
mod_subGrid2_ui(ns("subGrid2_ui_1"))
)
),
shinydashboard::tabItem("portfolio",
shiny::tagList(
div(p("Content for 2nd goes here -- GRID MODULE (2x)")),
titlePanel(title = "The same module UI goes here"),
mod_subGrid2_ui(ns("subGrid2_ui_2"))
)
)
)
)
)
}
mod_Grid_server <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
mod_subGrid2_server("subGrid2_ui_1")
mod_subGrid2_server("subGrid2_ui_2")
## uncomment to try
# observeEvent(input$tabs,{
# if(input$tabs == "home"){
# # <subGrid> server fragment
# mod_subGrid2_server("subGrid2_ui_1")
# } else if(input$tabs == "portfolio"){
# mod_subGrid2_server("subGrid2_ui_1")
# }
# }, ignoreNULL = TRUE, ignoreInit = TRUE)
})
}
brochureApp(
page(
href = "/",
ui = tagList(
mod_Grid_ui("grid_1")
),
server = function(input, output, session) {
mod_Grid_server("grid_1")
}
),
wrapped = shiny::tagList
)
When using a module nested inside another module, you need to ns() the id of the nested UI function.
So here, mod_subGrid2_ui(ns("subGrid2_ui_1")).
Here is a minimal reprex:
mod_subGrid2_ui <- function(id) {
ns <- NS(id)
tagList(
plotOutput(ns("plot"))
)
}
mod_subGrid2_server <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
output$plot <- renderPlot({
shinipsum::random_ggplot()
})
})
}
mod_Grid_ui <- function(id) {
ns <- NS(id)
tagList(
mod_subGrid2_ui(ns("subGrid2_ui_1"))
)
}
mod_Grid_server <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
mod_subGrid2_server("subGrid2_ui_1")
})
}
brochureApp(
page(
href = "/",
ui = tagList(
mod_Grid_ui("grid_1")
),
server = function(input, output, session) {
mod_Grid_server("grid_1")
}
)
)

Problems nesting modules in shiny and using renderUI

I have to call a module within another module to display a dataset filter. So the construction of the filter is dynamic and is done within a renderUI.
The problem is that it doesn't show me the filter.
I have created a code that exemplifies my problem.
I have two modules:
inner and outer.
From outer and I call inner.
If the call is made from a renderUI of outer (filter3), the button is not displayed.
It must be a namespace (ns) problem but I can't figure out why.
innerUI <- function(id) {
ns <- NS(id)
hidden(
actionButton(
NS(id, "filter"),
label = NULL,
icon = icon("filter"),
style = "margin-left: 0px;"
)
)
}
outerUI <- function(id) {
ns <- NS(id)
wellPanel(
innerUI(ns("inner1")),
uiOutput(ns("list_dtf"))
)
}
innerServer <- function(id,
data = reactive(NULL),
hide = FALSE,
hover_text = NULL) {
# SERVER
moduleServer(id, function(input, output, session) {
# NAMESPACE
ns <- session$ns
print("!hide")
if (!hide) {
print(hide)
shinyjs::show("filter")
print('show("filter")')
print(id)
if(!is.null(hover_text)) {
addTooltip(session = session,
id = ns("filter"),
title = hover_text)
}
} else {
print("escondo el filter")
}
}
)
}
outerServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
ns <- session$ns
innerResult1 <- innerServer("inner1", hover_text="prueba tooltip 1")
innerResult3 <- innerServer( "inner3", hover_text="prueba tooltip 3")
output$list_dtf <- renderUI({
div(
h2("1 OTRO FILTRO:"),
innerUI(session$ns("inner3")),
h2("2 OTRO FILTRO:")
)
})
}
)
}
ui <- fluidPage(
wellPanel(
titlePanel("Select of Data File"),
useShinyjs(),
outerUI ("select_data_file"),
innerUI("inner2")
)
)
# server
server <- function(input, output, session){
outerServer ("select_data_file")
innerServer("inner2",hide = FALSE,hover_text="prueba tooltip2")
}
shinyApp(ui, server)
Try this
innerUI <- function(id) {
ns <- NS(id)
#hidden(
actionButton(
ns("filter"),
label = NULL,
icon = icon("filter"),
style = "margin-left: 0px;"
)
#)
}
innerServer <- function(id,
data = reactive(NULL),
hide = FALSE,
hover_text = NULL) {
# SERVER
moduleServer(id, function(input, output, session) {
# NAMESPACE
ns <- session$ns
print("!hide")
if (!hide) {
print(hide)
shinyjs::show("filter")
print('show("filter")')
print(id)
if(!is.null(hover_text)) {
addTooltip(session = session,
id = ns("filter"),
title = hover_text)
}
} else {
shinyjs::hide("filter")
print("escondo el filter")
}
}
)
}

eventReactive in shiny module

I would like to use an eventReactive-function in a shiny module. However that does not work as expected. What is wrong with my code or what do I have to add?
I have already tried observers but I want to use eventReactive because I need the return-value.
mod_test_UI <- function(id) {
ns <- NS(id)
actionButton(ns("test"), "Test")
}
mod_test <- function(input, output, session) {
ns <- session$ns
observe({
print(input$test)
})
result<- eventReactive(input$test, {
print("ABC")
})
}
ui <- tagList(
mod_test_UI("test-mod")
)
server <- function(input, output, session) {
callModule(mod_test, "test-mod")
}
# app
shinyApp(ui = ui, server = server)
You need to return a value within eventReactive as below:
mod_test_UI <- function(id) {
ns <- NS(id)
actionButton(ns("test"), "Test")
}
mod_test <- function(input, output, session) {
ns <- session$ns
observe({
print(input$test)
})
result<- eventReactive(input$test, {
return("ABC")
})
observe({
print(result())
})
}
ui <- tagList(
mod_test_UI("test-mod")
)
server <- function(input, output, session) {
callModule(mod_test, "test-mod")
}
# app
shinyApp(ui = ui, server = server)
The second observe just prints the value now contained in result() to the screen to prove that it works.
The return() in this case is not necessary and it could just be "ABC" as below:
result<- eventReactive(input$test, {
"ABC"
})

Resources