来自第一个闪亮模块的反应性表达式未在第二个模块中识别

时间:2018-10-29 09:13:07

标签: r shiny

我正在努力编写有光泽的模块,以使人们可以使用一系列的两个模块来选择单个股票。

在下面的最小示例中:

1)模块1选择并返回股票的被动子集

2)模块2根据模块1返回的子集选择单个股票

下面的代码抛出 lapply(obj,function(val){错误:找不到对象“显示”

请:如何正确定义变量“显示”,以便将其识别为可在模块2(即select2_mod)中用作输入的反应式表达式?

对于以下调试方面的任何帮助,我们将不胜感激。

library(shiny)

stock_vector1 <- c("A", "B", "C", "D", "E", "F") ## Complete vector

### First Module that returns a subset of complete stock vector

select1_modUI <- function(id,stock_vector1){
  ns <- NS(id)
  tagList(
    selectInput(ns('stock_vector2'),
                'Subset of Stocks',
                stock_vector1,
                selected = 'A', 
                multiple = TRUE),
    textOutput(ns('text'))
  )
}

select1_mod <- function(input, output, session,stock_vector1) {
  output$text <- renderText(input$stock_vector2)
  val <- reactive({input$stock_vector2})
  return(val)
}

### Second Module that selects an individual stock

select2_modUI <- function(id,show) {
  ns <- NS(id)
  tagList(
    selectInput(ns('individual_stock'),
                'Select Individual Stock',
                choices = show(),
                selected = 1),
    textOutput(ns('temp'))
  )
}

select2_mod <- function(input, output, session,show) {
  observeEvent(show(), {print(show())})
    output$temp <- renderPrint(input$individual_stock)
  }

# Main app

ui <- fluidPage(
  tabsetPanel(
     tabPanel('Select1',
              h2('Select1'),
              select1_modUI('m1',stock_vector1)),
     tabPanel('Select2',
              h2('Select2'),
              select2_modUI('m2',display))
     )
)

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

  display <- callModule(select1_mod,'m1',stock_vector1)
  callModule(select1_mod,'m1',stock_vector1)
  callModule(select2_mod,'m2',display)

}

# Run the app
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题。

library(shiny)
stock_vector1 <- c("A", "B", "C", "D", "E", "F")

### First Module that returns a subset of complete stock vector
select1_modUI <- function(id){
  ns <- NS(id)
  tagList(
    selectInput(ns('stock_vector2'), 'Subset of Stocks', choices = stock_vector1, selected = "A", multiple = TRUE),
    textOutput(ns('text'))
  )
}

select1_mod <- function(input, output, session){
  output$text <- renderText(input$stock_vector2)
  val <- reactive({input$stock_vector2})
  return(val())
}

### Second Module that selects an individual stock
select2_modUI <- function(id){
  ns <- NS(id)
  tagList(
    selectInput(ns('individual_stock'), 'Select Individual Stock', choices = NULL),
    textOutput(ns('temp'))
  )
}

select2_mod <- function(input, output, session){
  observe({ updateSelectInput(session, 'individual_stock', choices = display())})
  output$temp <- renderPrint(input$individual_stock)
}

# Main app
ui <- fluidPage(
  tabsetPanel(
    tabPanel('Select1',
             h2('Select1'),
             select1_modUI('m1')),
    tabPanel('Select2',
             h2('Select2'),
             select2_modUI('m2'))
))

server <- function(input, output, session) {
  display <<- reactive(callModule(select1_mod, "m1"))
  callModule(select2_mod,'m2')
}

# Run the app
shinyApp(ui = ui, server = server)

存在一些次要(但主要)错误。返回无功值时,必须始终在名称后使用()。另外,我没有使用选项作为参数,而是使用了自动传递的全局变量(这是<<-的原因)。我不确定您是否知道对变量show所做的操作,该变量是保留字,尤其是()