R Shiny 中的反应式数据框

2023-11-29

我有一个由多个类别和月份组成的数据框。每行是参与者的 1 次交互,因此我想通过热图按月份和不同类别显示他们全年的交互计数。 总共 490 万行,这意味着全年总共有 490 万次交互。

我尝试使用输入来指示列名称,并尝试被动地更改 X 轴,但它似乎不起作用。

library(shiny)
library (igraph)
library (tidygraph)
library (ggraph)
library (plotly)

interaction <- readRDS("participant_interaction.rds")

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Social Network Interaction"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          helpText(" Visualise the Social Network Interaction of the Population
          in Ohio"),
          
            selectInput(inputId = "Category",
                        label = "Choose a Category",
                        choices = c( "Household Size" = "Household_Size",
                                     "Have Kids" = "Have_Kids",
                                     "Education Level" = "Education_Level",
                                     "Interest Group" = "Interest_Group",
                                     "Age Group" = "Age_Group"
                        ),
                        selected = "Household_Size")
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("heatmapPlot")
        )
    )
)


server <- function(input, output, session) {
  
  dataset <- reactive({
    interaction %>%
      group_by(Month,input$Category)%>%
      summarise(InteractionCount = n()) %>%
      ungroup
  })

    output$heatmapPlot <- renderPlot({
      ggplot(dataset(),aes(x= input$Category,y = Month,fill = InteractionCount)) +
        geom_tile()
    })
}

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

Desired Output: Heatmap

Current Output: enter image description here

我的 RDS 文件的随机 20 (dput):

structure(list(Participant_ID = c(374, 167, 899, 299, 443, 889, 
997, 521, 953, 436, 218, 422, 4, 227, 126, 12, 57, 386, 255, 
307), Month = structure(c(7L, 8L, 9L, 9L, 6L, 10L, 11L, 7L, 4L, 
6L, 10L, 11L, 8L, 12L, 7L, 8L, 7L, 11L, 7L, 10L), .Label = c("Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec"), class = c("ordered", "factor")), Income = c(37891.66, 
50156.67, 59877.49, 56393.02, 27899.58, 61258.05, 60728.21, 44858.08, 
36665.14, 27970.52, 27803.1, 63058.55, 135076.17, 46147.7, 30712.52, 
93774.11, 168620.22, 82925.21, 29451.88, 40328.79), Expenses = c(-26931.38, 
-32313.29, -25363.68, -23341.46, -24747.06, -18336.36, -17067.74, 
-22054.58, -23258.78, -22504.3, -23833.48, -17322.96, -28322.98, 
-16633.05, -23661.04, -21135.57, -20642.13, -23707.51, -26028.96, 
-30125.43), Household_Size = c(2, 2, 2, 3, 1, 2, 1, 3, 1, 1, 
1, 1, 3, 1, 2, 3, 3, 2, 3, 3), Have_Kids = c(FALSE, FALSE, FALSE, 
TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, 
TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE), Age = c(57, 
49, 59, 35, 56, 19, 48, 39, 45, 36, 38, 27, 43, 59, 26, 34, 56, 
39, 59, 38), Education_Level = c("High School or College", "High School or College", 
"Graduate", "High School or College", "High School or College", 
"Bachelors", "Graduate", "High School or College", "Graduate", 
"High School or College", "High School or College", "Graduate", 
"Bachelors", "High School or College", "High School or College", 
"High School or College", "Graduate", "Graduate", "High School or College", 
"Graduate"), Interest_Group = c("F", "F", "B", "I", "F", "A", 
"I", "I", "G", "J", "B", "D", "H", "H", "A", "D", "A", "H", "H", 
"H"), Joviality = c(0.85417223, 0.846680285, 0.544405537, 0.5149016, 
0.758121962, 0.126644557, 0.128853966, 0.477456741, 0.846871205, 
0.949412047, 0.971074532, 0.647441392, 0.857396691, 0.490213553, 
0.72895287, 0.241615182, 0.422849796, 0.479804894, 0.852187763, 
0.962210067), Age_Group = structure(c(9L, 7L, 9L, 4L, 9L, 1L, 
7L, 5L, 6L, 5L, 5L, 3L, 6L, 9L, 3L, 4L, 9L, 5L, 9L, 5L), .Label = c("20 & Below", 
"21-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", 
"56-60"), class = "factor")), row.names = c(1970773L, 932225L, 
4348108L, 1549925L, 2381951L, 4296595L, 4822673L, 2808545L, 4594431L, 
2337980L, 1146486L, 2241290L, 16905L, 1187579L, 697338L, 71056L, 
302316L, 2012670L, 1319716L, 1594018L), class = "data.frame")


input$Category是一个字符串,但是dplyr动词和aes期待一个符号。有一些较旧的答案推荐aes_string或手动将字符串转换为符号。

但是,现在推荐的选项是使用.data代词,参见dplyr文档 and 掌握闪亮的书.

在你的情况下,它将是:

group_by(Month, .data[[input$Category]])

and

aes(x = .data[[input$Category]], y = Month, fill = InteractionCount)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R Shiny 中的反应式数据框 的相关文章

随机推荐

  • 使用 Gradle 构建 uberjar

    我想构建一个 uberjar 又名 fatjar 其中包含项目的所有传递依赖项 我需要添加哪些行build gradle 这就是我目前所拥有的 task uberjar type Jar from files sourceSets main
  • 使用 jni 库构建 AOSP 应用程序

    我正在尝试在 AOSP 内构建 Android 应用程序 我已经定义了Android bp文件如下 cc prebuilt library shared name libPrintString target android arm srcs
  • 获取文件的绝对路径

    如何在 Unix 上将相对路径转换为 C 中的绝对路径 有没有方便的系统功能 在 Windows 上有一个GetFullPathName函数可以完成这项工作 但我在 Unix 上没有找到类似的东西 Use 真实路径 The realpath
  • “在此上下文中需要子类型标记”到底是什么?

    I get Subtype mark required in this context at 子类型掩码到底是什么 为什么它在这里抱怨 main adb Open Route Route 1 3 others gt new Location
  • buildTypes 无法应用于 groovy.lang.Closure

    我在我的项目 gradle 文件中收到此警告 警告 16 5 buildTypes 无法应用于 groovy lang Closure 我的 buildTypes 部分是 buildTypes debug debuggable true r
  • ffmpeg 在单个命令中剪切视频并刻录字幕

    我想从视频中剪下一段并刻录该段的字幕 我可以分三步完成 剪切视频 ffmpeg ss 25 00 to 26 00 i vid mp4 c copy out mp4 剪掉副标题 ffmpeg i sub srt ss 25 00 to 26
  • 如何隐藏实际的下载文件夹位置

    我心里有一个问题 如何mod rewrite增加安全性 我有一个 php 文件 它在线显示 pdf 文件 例如www exaple com id 234它会查询数据库并获取实际的文件夹位置 实际文件夹位置是uploads 我正在使用类似的东
  • 在新的 Android 模拟器上禁用首次运行的欢迎程序

    我正在编写一个测试 需要直接从启动器启动应用程序 因为我无法通过意图启动来正确模拟它 问题是 当我在新的模拟器上运行测试时 我使用的是 Travis CI 但它可以在我的家用 PC 上轻松重现 模拟器会以 首次运行 问候语覆盖层开始 这会阻
  • Kafka 生产者 - 如何在不停机的情况下更改主题并保留消息顺序?

    这个问题是关于架构和kafka主题迁移的 原来的问题 没有向后兼容性的架构演变 https docs confluence io current schema registry avro html 我请求社区给我建议或分享文章 我可以从中获
  • 如何在 Ivy 中整合版本管理,就像 Maven 中的parent-pom 一样?

    我们有很多项目需要使用通用版本号 使用 Ant Ivy 执行此操作的最佳实践是什么 您是否只是从 Ant 继承了一堆包含版本号的属性 还是有像 Maven 那样更正式的机制 正如你所指出的 我认为这是新的问题extends功能被设计来解决
  • Mongodb 查询特定的月份|年份而不是日期

    如何查询 mongodb 中的特定月份 而不是日期范围 我需要月份来列出当月的客户生日列表 在 SQL 中会是这样的 SELECT FROM customer WHERE MONTH bday 09 现在我需要在 mongodb 中翻译它
  • 从 Play 商店 xamarin 获取最新的应用程序版本

    如何从 Google Play 商店获取最新的 Android 应用程序版本 之前曾使用下面的代码来做到这一点 using var webClient new System Net WebClient var searchString it
  • eclipse插件代码在指定位置创建IProject

    IProgressMonitor progressMonitor new NullProgressMonitor IWorkspaceRoot root ResourcesPlugin getWorkspace getRoot IProje
  • JButton 的不透明度/半透明度?

    我有如下简单的 GUI 代码 我想在其中制作JButton一个是半透明的 这样后面的图像JButton是可见的 package dealORnodeal import java awt BorderLayout import java aw
  • 如何选择并删除每第三列

    我有一组数据 其中每第三列都是相同的 我只想保留第一列 其他相同的列必须删除 起初我尝试了这段代码 但它删除了错误的列 因为在每个循环中其他列的位置都被改变了 Sub DeleteMultipleColumns Dim i As Integ
  • Rails 模型字段未更新到数据库

    使用型号 class Car lt ActiveRecord Base attr accessor model edition attr accessible model edition has many wheels end class
  • Google Actions 返回抱歉,尚不支持电源控制

    我通过扩展助手来实现我的设备 如中所述指示 我可以看到我的设备注册了以下特征 googlesamples assistant devicetool 列表 model Device Model Id assistant 19etc Proje
  • .NET:如何获取空对象的类型?

    我有一个带有 out 参数的方法 尝试进行类型转换 基本上 public void GetParameterValue out object destination object paramVal I want to return this
  • Android 地图、标记和内存泄漏

    我正在阅读 android 文档http developer android com reference com google android gms maps MapFragment html我看到了这句话 从 GoogleMap 获取的
  • R Shiny 中的反应式数据框

    我有一个由多个类别和月份组成的数据框 每行是参与者的 1 次交互 因此我想通过热图按月份和不同类别显示他们全年的交互计数 总共 490 万行 这意味着全年总共有 490 万次交互 我尝试使用输入来指示列名称 并尝试被动地更改 X 轴 但它似