如何使用另一个模块的反应式数据帧更新闪亮模块

2024-01-01

该模块的目标是创建一个根据数据选择器模块的输出而变化的反应性条形图。不幸的是,条形图没有更新。它停留在选定的第一个变量上。

我尝试创建观察者函数来更新条形图,但无济于事。我还尝试将选择器服务器模块嵌套在 barplot 模块中,但出现错误:警告:UseMethod 中的错误:没有适用于“mutate”的方法应用于类“c('reactiveExpr', 'reactive'”的对象, '功能')”

我只需要某种方法来告诉 barplot 模块在输入的数据发生变化时进行更新。

条形图模块:

#UI

barplotUI <- function(id) {
  tagList(plotlyOutput(NS(id, "barplot"), height = "300px"))
}

#Server
#' @param data Reactive element from another module: reactive(dplyr::filter(austin_map, var == input$var)) 
barplotServer <- function(id, data) {
  moduleServer(id, function(input, output, session) {
    #Data Manipulation
    bardata <- reactive({
      bar <-
        data  |>
        mutate(
          `> 50% People of Color` = if_else(`% people of color` >= 0.5, 1, 0),
          `> 50% Low Income` = if_else(`% low-income` >= 0.5, 1, 0)
        )
      
      total_av <- mean(bar$value)
      poc <- bar |> filter(`> 50% People of Color` == 1)
      poc_av <- mean(poc$value)
      lowincome <- bar |> filter(`> 50% Low Income` == 1)
      lowincome_av <- mean(lowincome$value)
      bar_to_plotly <-
        data.frame(
          y = c(total_av, poc_av, lowincome_av),
          x = c("Austin Average",
                "> 50% People of Color",
                "> 50% Low Income")
        )
      
      return(bar_to_plotly)
    })
    
    #Plotly Barplot
    output$barplot <- renderPlotly({
      plot_ly(
        x = bardata()$x,
        y = bardata()$y,
        color = I("#00a65a"),
        type = 'bar'
        
      ) |>
        config(displayModeBar = FALSE)
      
    })
  })
}

EDIT : 数据选择器模块

dataInput <- function(id) {
  tagList(
    pickerInput(
      NS(id, "var"),
      label = NULL,
      width = '100%',
      inline = FALSE,
      options = list(`actions-box` = TRUE,
                     size = 10),
      choices =list(
            "O3",
            "Ozone - CAPCOG",
            "Percentile for Ozone level in air",
            "PM2.5",
            "PM2.5 - CAPCOG",
            "Percentile for PM2.5 level in air")
    )
  )
}

dataServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    austin_map <- readRDS("./data/austin_composite.rds")
    austin_map <- as.data.frame(austin_map)
    austin_map$value <- as.numeric(austin_map$value)
    
    list(
      var = reactive(input$var),
      df = reactive(austin_map |> dplyr::filter(var == input$var))
    )
    
  })
}

简化的应用程序

library(shiny)
library(tidyverse)
library(plotly)

source("barplot.r")
source("datamod.r")


ui = fluidPage(
  fluidRow(
    dataInput("data"),
    barplotUI("barplot")
    )
  )

server <- function(input, output, session) {
  data <- dataServer("data")
  variable <- data$df
  
  
  barplotServer("barplot", data = variable())
  
}

shinyApp(ui, server)


正如我在评论中所写,将反应式数据集作为参数传递给模块服务器与传递任何其他类型的参数没有什么不同。

这是一个 MWE 来说明这个概念,通过mtcars或者选择模块和显示模块之间的随机值的数据帧。

关键点是选择模块返回reactive [data],不是反应性的价值 [data()] 到主服务器功能,然后reactive,不是反应性的价值作为参数传递给绘图模块。

library(shiny)
library(ggplot2)

# Select module
selectUI <- function(id) {
    ns <- NS(id)
    selectInput(ns("select"), "Select a dataset", c("mtcars", "random"))
}

selectServer <- function(id) {
    moduleServer(
        id,
        function(input, output, session) {
            data <- reactive({
                if (input$select == "mtcars") {
                    mtcars
                } else {
                    tibble(x=runif(10), y=rnorm(10), z=rbinom(n=10, size=20, prob=0.3))
                } 
            })
            
            return(data)
        }
    )
}

# Barplot module
barplotUI <- function(id) {
    ns <- NS(id)
    
    tagList(
        selectInput(ns("variable"), "Select variable:", choices=c()),
        plotOutput(ns("plot"))
    )
}

barplotServer <- function(id, plotData) {
    moduleServer(
        id,
        function(input, output, session) {
            ns <- NS(id)
            
            observeEvent(plotData(), {
                updateSelectInput(
                    session, 
                    "variable", 
                    choices=names(plotData()), 
                    selected=names(plotData()[1])
                )
            })
            
            output$plot <- renderPlot({
                # There's an irritating transient error as the dataset
                # changes, but handling it would
                # detract from the purpose of this answer
                plotData() %>% 
                    ggplot() + geom_bar(aes_string(x=input$variable))

            })
        }
    )
}

# Main UI
ui <- fluidPage(
    selectUI("select"),
    barplotUI("plot")
)

# Main server
server <- function(input, output, session) {
    selectedData <- selectServer("select")
    barplotServer <- barplotServer("plot", plotData=selectedData)
}

# Run the application 
shinyApp(ui = ui, server = server)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用另一个模块的反应式数据帧更新闪亮模块 的相关文章

随机推荐

  • 使用 Java 8 Lambda 选取列表中的元素,直到满足条件

    我正在尝试转变思维方式 以函数式方式进行思考 最近遇到了一种情况 我需要从列表中选取元素 直到满足条件 但我找不到一种简单 自然的方法来实现这一目标 显然我还在学习 假设我有这个清单 List
  • R 中使用 sf 和 sp 的投影差异

    我有一个已从 GeoTIFF 转换为 shapefile 的网格 我想将 shapefile 转换并导出为 GeoPackage 并更改投影 以便在 GIS 中打开时使用英国国家网格作为地理坐标系 然而 这似乎只能使用sp并不是sf 似乎没
  • HADOOP YARN - 应用程序已添加到调度程序,但尚未激活。由于集群资源为空而跳过 AM 分配

    我正在评估一个项目的 YARN 我正在尝试让简单的分布式 shell 示例正常工作 我已将应用程序提交到 已提交 阶段 但它从未启动 这是从这一行报告的信息 ApplicationReport report yarnClient getAp
  • _WIN64 未在 x64 项目中定义

    我使用的是 VS2008 项目属性设置为 活动 x64 奇怪的是 WIN64 没有定义 WPARAM 和 LPARAM 仍然是 32 位 我应该手动定义 WIN64吗 如果是这样 我应该把 define WIN64放在哪里 预处理器设置似乎
  • MS Word JavaScript API - 内容控件的事件处理程序

    有没有办法在单击 Office js 中的 contentControl 时触发函数 我正在为 MS Word 编写一个加载项 并且已插入内容控件 这些控件将与我的加载项中的列表关联 我需要在单击内容控件时触发一个函数 以便我可以滚动到列表
  • React-router 6 导航到使用参数

    在 v5 中我有这样的结构 path someurl id exact true render params gt
  • 在哪里可以找到 Mac OS X Lion 的“make”程序?

    刚刚将我的计算机升级到 Mac OS X Lion 然后进入终端并输入 make 但它显示 bash make 找不到命令 make 命令去哪儿了 您需要从以下位置安装 Xcode应用商店 http itunes apple com us
  • 需要隐藏 ansible 任务中失败的登录

    我是 ansible 任务的新手 正在创建一个执行登录操作的 yml 如果登录失败 则需要调用一些脚本 name Logging Action shell usr local bin cqlsh u xyzyx p 1234abc regi
  • 直觉类型理论的组合逻辑等价物是什么?

    我最近完成了一门以 Haskell 和 Agda 一种依赖类型函数编程语言 为特色的大学课程 并且想知道是否有可能用组合逻辑代替其中的 lambda 演算 在 Haskell 中 使用 S 和 K 组合器似乎可以实现这一点 从而使其成为无点
  • java 解析布尔值可能为空的布尔值

    我注意到一个问题java lang 布尔值 https docs oracle com javase 7 docs api java lang Boolean html类无法解析空值 我知道它有parseBoolean https docs
  • 如何隐藏

    我试图隐藏这些 p p
  • Lollipop 的应用程序兼容性、兼容性和支持库(如果最低 SDK = 14)

    我们有一个现有的 Android 应用程序 支持 API 级别 8 至 18 我们使用兼容性库 19 1 0 现在我们正在更改 升级为 最低 SDK 14 目标 android 22 现在考虑到不同版本都有 v4 v7 v13 支持 兼容性
  • ResolveEventArgs.RequestingAssembly 为 Null

    我正在尝试通过反射动态加载程序集 我有这样的文件夹结构 project BIN myApp exe SOMEEXTENTION1 someExtention1 dll itsDependency1 dll SOMEEXTENTION2 so
  • R 以编程方式更改 IP 地址

    目前通过将不同的字符串传递给来更改 user agenthtml session method 还有一种方法可以在抓取网站时更改计时器上的 IP 地址吗 您可以通过以下方式使用代理 它会更改您的IP use proxy如下 html ses
  • Cucumber + 测试 JS 警报

    我正在尝试使用 Cucumber on Rails 测试 JS 确认对话框 我有一个 window onbeforeunload 事件处理程序 如果您尝试离开该页面 它会提示您一个确认对话框 但我不知道如何测试它 有人知道如何做到这一点吗
  • 我可以使用最新的稳定 TypeScript 还是应该坚持使用 AngularCLI 附带的版本?

    我找不到任何关于是否使用可用的最新稳定版本升级 TypeScript 版本的官方建议 npm 版本自动设置在package json创建新项目时通过 AngularCLI 目前是 typescript 2 4 2 这意味着最新的2 4 x版
  • 在单个 ROC 图上绘制线性判别分析、分类树和朴素贝叶斯曲线

    数据显示在页面的最底部 称为 LDA scores 这是一个分类任务 我在数据集上执行了三种监督机器学习分类技术 提供所有编码以显示这些 ROC 曲线是如何生成的 我很抱歉提出了一个有问题的问题 但近两周来我一直在尝试使用不同的代码组合来解
  • 如何合并多个 BIRT 报告

    我们目前拥有一整套报告设计 涵盖了我们应用程序的各个部分 并且这些报告是根据用户的需求生成的 我希望能够将其中几个报告捆绑成一个报告以返回给用户 我最初破解了一个自定义报告生成器 它使用报告库文件中的段生成报告设计文件 然后运行生成的设计
  • 在 PERL 中从 Windows 访问 Microsoft SQL Server

    我正在使用 SQL Server 驱动程序 但这是我得到的以下错误 DBI connect Driver SQL Server database host cartertest failed Microsoft ODBC Driver Ma
  • 如何使用另一个模块的反应式数据帧更新闪亮模块

    该模块的目标是创建一个根据数据选择器模块的输出而变化的反应性条形图 不幸的是 条形图没有更新 它停留在选定的第一个变量上 我尝试创建观察者函数来更新条形图 但无济于事 我还尝试将选择器服务器模块嵌套在 barplot 模块中 但出现错误 警