0

I have a modularized Golem app using bs4Dash. I want to update the active sidebar tab from an actionBttn that is dynamically generated from renderUI. While updatebs4ControlbarMenu works as expected as shown here, it does not work in the modularized version of the application. What am I doing wrong? I suspect it is related to input[[btnID]] management across modules but I struggle to find the solution.

Working example without modules as shown here:

library(shiny)
library(shinyWidgets)
library(bs4Dash)
library(tidyverse)

shinyApp(
  ui = bs4DashPage(
    sidebar_collapsed = FALSE,
    controlbar_collapsed = TRUE,
    enable_preloader = FALSE,
    navbar = bs4DashNavbar(skin = "dark"),
    sidebar = bs4DashSidebar(
      inputId = "sidebarState",
      bs4SidebarMenu(
        id = "sidebr",
        bs4SidebarMenuItem(
          "Tab 1",
          tabName = "tab1"
        ),
        bs4SidebarMenuItem(
          "Tab 2",
          tabName = "tab2"
        )
      )
    ),
    
    bs4DashBody(
      bs4TabItems(
        bs4TabItem(
          tabName = "tab1",
          h1("Welcome!"),
          fluidRow(
            pickerInput(
              inputId = "car",
              label = "Car", 
              choices = row.names(mtcars),
              selected = head(row.names(mtcars), 3),
              multiple = TRUE,
              options = list(
                `actions-box` = TRUE)
            ),
            pickerInput(
              inputId = "gear",
              label = "Gear", 
              choices = unique(mtcars$gear),
              selected = unique(mtcars$gear),
              multiple = TRUE,
              options = list(
                `actions-box` = TRUE)
            )
          ),
          
          fluidRow(
            column(6,
                   uiOutput("uiboxes")
            )
          )
        ),
        
        bs4TabItem(
          tabName = "tab2",
          h4("Yuhuuu! You've been directed automatically in Tab 2!")
        )
      )
    )
  ),
  server = function(input, output, session) {
    
    submtcars <- reactive({
      req(input$car, input$gear)
      mtcars %>% 
        mutate(
          carnames = rownames(mtcars)) %>% 
        filter(
          carnames %in% input$car &
            gear %in% input$gear
        )
    })
    
    
    observeEvent( submtcars(), {
      n_ex <- nrow(submtcars())
      output$uiboxes <- renderUI({
        
        lapply(1:n_ex, FUN = function(j) {
          print(paste("j is ", j))
          bs4Box(
            title = submtcars()$carnames[j],
            width = 12,
            str_c("Number of gears:", submtcars()$gear[j]),
            
            btnID <- paste0("btnID", j),
            
            print(btnID),
            fluidRow(
              column(
                2,
                actionBttn(
                  inputId = btnID,
                  icon("search-plus")
                )
              )
            )
          )
        })
      })
      
      lapply(1:n_ex, function(j) {
        btnID <- paste0("btnID", j)
        observeEvent(input[[btnID]] , {
          updatebs4ControlbarMenu(
            session,
            inputId = "sidebr",
            selected = "tab2"
          )
        })
      })
    })
    
  }
)

Modularized attempt not working:

library(shiny)
library(shinyWidgets)
library(bs4Dash)
library(tidyverse)


mod_exlib_ui <- function(id){
  ns <- NS(id)
  tagList(
    fluidRow(
      pickerInput(
        inputId = ns("car"),
        label = "Car", 
        choices = row.names(mtcars),
        selected = head(row.names(mtcars), 3),
        multiple = TRUE,
        options = list(
          `actions-box` = TRUE)
      ),
      pickerInput(
        inputId = ns("gear"),
        label = "Gear", 
        choices = unique(mtcars$gear),
        selected = unique(mtcars$gear),
        multiple = TRUE,
        options = list(
          `actions-box` = TRUE)
      )
    ),
    
    fluidRow(
      column(6,
             uiOutput(ns("uiboxes"))
      )
    )
  )
}


mod_exlib_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    submtcars <- reactive({
      # req(input$car, input$gear)
      mtcars %>% 
        dplyr::mutate(
          carnames = rownames(mtcars)) %>% 
        dplyr::filter(
          carnames %in% input$car &
            gear %in% input$gear
        )
    })
    
    
    observeEvent( submtcars(), {
      n_ex <- nrow(submtcars())
      output$uiboxes <- renderUI({
        
        lapply(1:n_ex, FUN = function(j) {
          print(paste("j is ", j))
          bs4Box(
            title = submtcars()$carnames[j],
            width = 12,
            paste("Number of gears: ", submtcars()$gear[j]),
            
            btnID <- paste0("btnID", j),
            
            print(btnID),
            fluidRow(
              column(
                2,
                actionBttn(
                  inputId = ns(btnID),
                  icon("search-plus")
                )
              )
            )
          )
        })
      })
      
      lapply(1:n_ex, function(j) {
        btnID <- paste0("btnID", j)
        observeEvent(input[[btnID]] , {
          print(btnID)
          updatebs4ControlbarMenu(
            session,
            inputId = "sidebr",
            selected = "exdet2"
          )
        })
      })
    })
  })
}

app_ui <- tagList(
  bs4DashPage(
    navbar = bs4DashNavbar(),
    sidebar = bs4DashSidebar(
      expand_on_hover = TRUE,
      inputId = "sidebarState",
      bs4SidebarMenu(
        id = "sidebr",
        bs4SidebarMenuItem(
          "Tab 1",
          tabName = "tab1"
        ),
        bs4SidebarMenuItem(
          "Tab 2",
          tabName = "tab2"
        )
      )
    ),
    bs4DashBody(
      bs4TabItems(
        bs4TabItem(
          tabName = "tab1",
          h1("Welcome!"),
          mod_exlib_ui("exlib_ui_1")
        ),
        bs4TabItem(
          tabName = "tab2",
          h4("Yuhuuu! You've been directed automatically in Tab 2!")
        )
      )
    )
  )
)

app_server <- function( input, output, session ) {
  # Your application server logic 
  mod_exlib_server("exlib_ui_1")
}


shinyApp(
  ui = app_ui,
  server = app_server)
AJMA
  • 1,134
  • 2
  • 13
  • 28

1 Answers1

0

After exploring the example of function updatebs4TabSetPanel() that is in the same family, it seems that the selected value needs to be a number.
Hence, you can use this code with CRAN version 0.5.0:

         updatebs4ControlbarMenu(
            session,
            inputId = "sidebr",
            selected = "2" #"exdet2"
          )
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
  • It does not work. In the examples provided for `updatebs4TabSetPanel()`, `selected` is not numeric, but a string (i.e. `paste("Tab", input$controller2)`): https://rinterface.github.io/bs4Dash/reference/updatebs4TabSetPanel.html – AJMA Dec 12 '20 at 08:14
  • input$controller2 is the output of the slideInput which is numeric. I use version 0.5.0 that is on CRAN. Which version do you use ? – Sébastien Rochette Dec 12 '20 at 08:18
  • bs4Dash’ version 0.6.0.9000. However, I think the problem is more related to the modules and how ids are handled?. The same app works properly when not modularized as shown here: https://stackoverflow.com/a/65241808/7375944 – AJMA Dec 12 '20 at 08:23
  • v0.6.0 has never been released. It will directly jump from 0.5.0 to 2.0 which has many breaking changes. I suggest you go back to the CRAN version or completely change to v2. I assure you the modification of my code works with 0.5.0 on my side. If you want me to test your version, you will have to give me the commit SHA1 of the version you downloaded from github. You can see it with `packageDescription("bs4Dash")` – Sébastien Rochette Dec 12 '20 at 08:32
  • GithubSHA1: 5dfbc155d8f32f4d5e0f27aee2740da295f2ffe0 – AJMA Dec 12 '20 at 08:38
  • I still get `Version: 0.6.0.9000`. Not sure whether being brave and go to v2 :) Merci Sébastien – AJMA Dec 12 '20 at 08:40
  • I think David started to change behaviors of buttons after v0.5.0. Some of functions you use do not even exist anymore in v2. So I wouldn't stick with a dev version anyway. When I try to debug the function with your version, the R6 object does not act as expected. I wouldn't be surprise this cannot work whatever you try. – Sébastien Rochette Dec 12 '20 at 09:23
  • OK, I see. I may try a JavaScript approach then... https://github.com/RinteRface/bs4Dash/issues/50 – AJMA Dec 12 '20 at 09:36