使用从一个发光模块到另一个发光模块的数据

时间:2018-08-10 19:20:55

标签: r shiny

我正在尝试使用一个闪亮模块中的值,并将其传递给另一个闪亮模块以进行打印。因此,当用户从第一个下拉列表中选择orange时,它将显示打印you have selected orange。但是到目前为止,它打印的是you have selected ATC,不过我正在通过的id。下面是我正在使用的代码。谢谢。

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)

我使用反应性值观察者事件自变量"ImProxy" is missing, with no default

尝试了此问题Passing data within Shiny Modules from Module 1 to Module 2的解决方案

1 个答案:

答案 0 :(得分:2)

您的代码有两个问题:

  • ImProxy是用户定义的变量。您尚未定义它,也没有将它作为参数传递。
  • 您正在使用id作为tabBox的标题。

两者均已在下面更正。

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)
相关问题