简体   繁体   中英

Reactive Dataframe in Rshiny

I have a data frame which consist of multiple categories and month. Each row is 1 interaction by the participant so I want to show by the month and the different categories their interaction count throughout the year through a heatmap. A total of 4.9 million rows which means there is a total of 4.9million interactions throughout the year.

I tried to use an input to indicate the column name and tried to reactively change the X axis but it does not seem to work.

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: 热图

Current Output: 在此处输入图像描述

my random 20 (dput) for my RDS file:

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 is a string, however the dplyr verbs and aes expect a symbol. There are some older answers out there recommending aes_string or manually converting the string to a symbol.

However, now the recommended option is to use the .data pronoun, see the dplyr documentation and mastering shiny book .

In your case, it would be:

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

and

aes(x = .data[[input$Category]], y = Month, fill = InteractionCount)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM