调用父级Shiny服务器中的反应性数据集的Shiny模块 [英] Shiny Module that calls a reactive data set in parent Shiny server

查看:68
本文介绍了调用父级Shiny服务器中的反应性数据集的Shiny模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望移植一些较旧的Shiny应用程序以使用Shiny Modules,但尝试移植我的反应式表达式时遇到麻烦.

I'm looking to port some older Shiny apps to use Shiny Modules, but running into trouble trying to port over my reactive expressions.

根据文档:

目标不是阻止模块与其模块进行交互 包含应用程序,而是使这些交互变得明确.如果一个 模块需要使用一个反应式,取反应式 表达式作为函数参数.

The goal is not to prevent modules from interacting with their containing apps, but rather, to make these interactions explicit. If a module needs to use a reactive expression, take the reactive expression as a function parameter.

我有现有的反应式表达式,它们从API等导入数据,我想传递这些表达式,但似乎找不到语法.如果我在下面修改给定的发光模块示例,我会遇到相同的问题.

I have existing reactive expressions that import data from APIs etc. that I would like to pass in, but can't seem to find the syntax. If I modify the given Shiny module example below I can get to the same problem.

任何人都可以修改以下内容,以便您可以将car_data()反应性数据传递到模块中吗?我已经尝试了几乎可以想到的isolatecar_data/car_data()的每种组合,并且感到很困惑:)

Could anyone modify the below so that you can pass in the car_data() reactive data into the module? I've tried just about every combination of isolate and car_data/car_data() I can think of and am stumped :)

我宁愿不需要在模块本身中调用数据,因为在我的情况下,我试图概括适用于许多数据集的ETL函数.

I would prefer to not need to call the data within the module itself, as in my case I'm trying to generalise an ETL function applicable to lots of datasets.

library(shiny)
library(ggplot2)

linkedScatterUI <- function(id) {
  ns <- NS(id)

  fluidRow(
    column(6, plotOutput(ns("plot1"), brush = ns("brush"))),
    column(6, plotOutput(ns("plot2"), brush = ns("brush")))
  )
}

linkedScatter <- function(input, output, session, data, left, right) {
  # Yields the data frame with an additional column "selected_"
  # that indicates whether that observation is brushed
  dataWithSelection <- reactive({
    brushedPoints(data(), input$brush, allRows = TRUE)
  })

  output$plot1 <- renderPlot({
    scatterPlot(dataWithSelection(), left())
  })

  output$plot2 <- renderPlot({
    scatterPlot(dataWithSelection(), right())
  })

  return(dataWithSelection)
}

scatterPlot <- function(data, cols) {
  ggplot(data, aes_string(x = cols[1], y = cols[2])) +
    geom_point(aes(color = selected_)) +
    scale_color_manual(values = c("black", "#66D65C"), guide = FALSE)
}

ui <- fixedPage(
  h2("Module example"),
  linkedScatterUI("scatters"),
  textOutput("summary")
)

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

  ### My modification 
  ### making the reactive outside of module call
  car_data <- reactive({
    mpg
    })

  ## This doesn't work
  ## What is the syntax for being able to call car_data()?
  df <- callModule(linkedScatter, "scatters", car_data(),
                   left = reactive(c("cty", "hwy")),
                   right = reactive(c("drv", "hwy"))
  )

  output$summary <- renderText({
    sprintf("%d observation(s) selected", nrow(dplyr::filter(df(), selected_)))
  })
}

shinyApp(ui, server)

推荐答案

将car_data之后的括号删除:

Drop the parens after car_data:

df <- callModule(linkedScatter, "scatters", car_data,
                   left = reactive(c("cty", "hwy")),
                   right = reactive(c("drv", "hwy"))
  )

该模块似乎想要未解决"的反应堆.括号解决"了它们.

The module seems to want "unresolved" reactives. The parentheses "resolves" them.

这篇关于调用父级Shiny服务器中的反应性数据集的Shiny模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆