-1

I am trying to use a value from one shiny module and pass it to a second shiny module to print it. So when user select orange from first dropdown it show print you have selected orange. But as of now it prints you have selected ATC which is nothing but the id I am passing . Below is the code I am using.Thank you.

library(shiny)
library(shinydashboard)
library(shinyWidgets)

dropDownUI <- function(id, div_width = "col-xs-12 col-md-8") {

  ns <- NS(id)

  div(column(3, uiOutput(ns("class_level"))),
      column(3,uiOutput(ns("selected_product_ui"))
      ))
}

chartTableBoxUI <- function(id, div_width = "col-xs-12 col-md-8") {
  ns <- NS(id)

  div(tabBox(width = 12, title = id,
             tabPanel(icon("bar-chart"),
                      textOutput(ns("selected_var")))
   )
  )
}

chartTableBox <- function(input, output, session, data,ImProxy) {

  output$selected_var <- renderText({
    ns <- session$ns
    paste("You have selected",ns(ImProxy$selected_class))
  })
}

dropDown <- function(input, output, session) {

  ns <- session$ns

  observe({output$class_level <- renderUI({
    selectInput(
      ns("selected_class"),
      label = h4("Classification Level"),
      choices = list(
        "apple " = "apple",
        "orange " = "orange"),
      selected = "orange"
    )})
  })

  a<-reactive({input$selected_class})

  output$selected_product_ui <- renderUI({
    req(input$selected_class)
    Sys.sleep(0.2)
    ns <- session$ns

    if (input$selected_class == "apple") {
      my_choices <- c("foo","zoo","boo")
    } else if (input$selected_class == "orange") {
      my_choices <- c("22","33","44")
    } else {
      my_choices <- c("aa","bb","cc")
    }

    selectInput(inputId = ns("selected_product"),
                label = h4("Product Family"),
                choices = my_choices)
  })

}

sidebar <- dashboardSidebar(sidebarMenu(
  menuItem("aaa",tabName = "aaa"),
  menuItem("bbb", tabName = "bbb"),
  menuItem("ccc", tabName = "ccc")
))

body <-   ## Body content
  dashboardBody(tabItems(
    tabItem(tabName = "aaa",
            fluidRow(dropDownUI(id = "dropdown"),
                     fluidRow(chartTableBoxUI(id = "ATC"))
            )
    )))
# Put them together into a dashboardPage
ui <-   dashboardPage(
  dashboardHeader(title = "Loyalty Monthly Scorecard"),
  sidebar,
  body
)

server = {
  shinyServer(function(input, output, session) {
    callModule(dropDown, id = "dropdown")
    callModule(chartTableBox, id = "ATC", data = MyData)

  })
}

shinyApp(ui = ui, server = server)

I tried the solution from this question Passing data within Shiny Modules from Module 1 to Module 2 using reactive values and observer event aargument "ImProxy" is missing, with no default

SNT
  • 1,283
  • 3
  • 32
  • 78
  • 7
    Possible duplicate of [Passing data within Shiny Modules from Module 1 to Module 2](https://stackoverflow.com/questions/46555355/passing-data-within-shiny-modules-from-module-1-to-module-2) – Gregor de Cillia Aug 11 '18 at 22:47
  • @GregordeCillia I did modify my question. – SNT Aug 12 '18 at 02:37

1 Answers1

3

There are two issues with your code:

  • ImProxy is a user defined variable. You have not defined it, nor have you passed it as an argument.
  • You are using the id as the title of your tabBox.

Both are corrected below.

library(shiny)
library(shinydashboard)
library(shinyWidgets)

dropDownUI <- function(id, div_width = "col-xs-12 col-md-8") {

  ns <- NS(id)

  div(column(3,uiOutput(ns("class_level"))),
      column(3,uiOutput(ns("selected_product_ui"))
      ))
}

chartTableBoxUI <- function(id, div_width = "col-xs-12 col-md-8") {
  ns <- NS(id)

  div(tabBox(width = 12, title = textOutput(ns("title_var")), ## fixing issue 2
             tabPanel(icon("bar-chart"),
                      textOutput(ns("selected_var")))
  )
  )
}

chartTableBox <- function(input, output, session, data,a) { ## fixing issue 1

  output$selected_var <- renderText({
    paste("You have selected",a())
  })

  output$title_var <- renderText({ a() }) ## fixing issue 2


}

dropDown <- function(input, output, session) {

  ns <- session$ns

  observe({output$class_level <- renderUI({
    selectInput(
      ns("selected_class"),
      label = h4("Classification Level"),
      choices = list(
        "apple " = "apple",
        "orange " = "orange"),
      selected = "orange"
    )})
  })

  a<-reactive({input$selected_class})

  output$selected_product_ui <- renderUI({
    req(input$selected_class)
    Sys.sleep(0.2)
    ns <- session$ns

    if (input$selected_class == "apple") {
      my_choices <- c("foo","zoo","boo")
    } else if (input$selected_class == "orange") {
      my_choices <- c("22","33","44")
    } else {
      my_choices <- c("aa","bb","cc")
    }

    selectInput(inputId = ns("selected_product"),
                label = h4("Product Family"),
                choices = my_choices)
  })

  return(a) ## fixing issue 1
}

# Put them together into a dashboardPage
ui =   dashboardPage(
  dashboardHeader(title = "Loyalty Monthly Scorecard"),
  dashboardSidebar(sidebarMenu(
    menuItem("aaa",tabName = "aaa")
  )),
  dashboardBody(tabItems(
    tabItem(tabName = "aaa",
            fluidRow(dropDownUI(id = "dropdown"),
                     chartTableBoxUI(id = "ATC") # this text
            )
    )))
)

server = {
  shinyServer(function(input, output, session) {
    a = callModule(dropDown, id = "dropdown")
    callModule(chartTableBox, id = "ATC", data = MyData, a = a)

  })
}

shinyApp(ui = ui, server = server)
Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41