R Shiny中dplyr响应式过滤器

4
我正在尝试设置一个仪表板,用户可以通过年份、状态和产品筛选数据。理想情况下,每个产品都有两个相关的变量,满意度得分和重要性得分。从数据集中过滤时,应计算各个感兴趣的部分的摘要均值。然后,平均重要性和平均满意度得分合并到数据帧中,并绘制在单个图中。
以下是我的进展...
我的用户界面。
library(shiny)
library(dplyr)
library(shinydashboard)
library(tidyverse)

ui <- dashboardPage(
  dashboardHeader(title="Membership Satisfaction"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Demographics Dashboard", tabName = "demos", icon = 
               icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(

     tabItem(tabName = "demos",
             sidebarPanel(
                checkboxGroupInput("inpt","Select variables to plot", 
               choices = 
                                 c("Web" = 1,"Huddle" = 3, "Other" = 5, 
               "Test" = 7)),
            checkboxGroupInput("role", 
                               "Select Primary Role of Interest", 
                               choices = c("Student" = 1, "Not" = 2)),
            checkboxGroupInput("range", 
                               "Select year(S) of Interest", 
                               choices = c("2016"=2,"July 2017"=1))),
          fluidPage(

            plotOutput("plot")

          )))))

我的服务器:

  server <- function(input,output){

  library(tidyverse)


  x <- reactive({
    inpt <- as.double(input$inpt)
    role <- as.double(input$role)
    range <- as.double(input$range)

    GapAnalysis_LongFormB %>%
      filter(Product %in% inpt,
         status %in% role,
         year %in% range) %>%
       summarize(avg = mean(Score, na.rm = TRUE)) %>%
        pull(-1)
        })


  y <- reactive({
    inpt <- as.double(input$inpt)+1
    role <- as.double(input$role)
    range <- as.double(input$range)

 GapAnalysis_LongFormB %>%
    filter(Product %in% inpt,
         status %in% role,
         year %in% range) %>% 
   summarize(avg = mean(Score, na.rm = TRUE))%>%
   pull(-1)
  })

 xyCoords<- reactive({
   x <- x()
   y <- y()

   data.frame(col1=x, col2=y)
   })



  output$plot <- renderPlot({

    xyCoords <- xyCoords()    

    xyCoords %>% 
     ggplot(aes(x = col1, y = col2)) +
     geom_point(colour ="green", shape = 17, size = 5 )+
     labs(x = "Mean Satisfaction", y = "Mean Importance") +
     xlim(0,5) + ylim(0,5) +
     geom_vline(xintercept=2.5) + 
     geom_hline(yintercept =  2.5)
    })

}



shinyApp (ui = ui, server = server)

以下是变量结构:

> dput(head(GapAnalysis_LongFormB))
structure(list(status = c(1, 5, 5, 1, 1, 5), year = c(1, 1, 1, 
1, 1, 1), Product = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
"2", "3", "4"), class = "factor"), Score = c(2, 5, 3, 5, 4, 4
)), .Names = c("status", "year", "Product", "Score"), row.names = c(NA, 
6L), class = "data.frame")

它可以工作 - 但没有完全满足我的需求。目前,在绘图之前,它需要在所有三个复选框输入变量(inpt,role,range)中输入内容。我需要它要求一个产品,但对于每个额外的输入都进行绘制。这意味着,如果他们选择Web,则会绘制Web的平均值。如果他们选择Web和2017年,则会绘制2017年Web的平均值。非常感谢您的帮助!

我没有运行这段代码,但是看一下 as.integer(input$inpt+1)。这不应该是 as.integer(input$inpt) + 1 吗? - akrun
我认为它们都产生了相同的结果(至少当我运行代码时没有发生任何变化) - Ellie
1个回答

2

更改

我认为以下几点可能会引起一些麻烦:

首先,您正在使用input$range,尽管您从未定义过input$range。您已经定义了一个input$yrs,因此我将其更改为input$range

接下来,您在filter中使用了==,当您应该使用%in%。这允许多个选择,而不仅仅是单个选择。如果您只想要单个选择,请改用radioButtons()而不是checkboxGroupInput()

在您的summarize中,您使用了额外且不必要的子集。我们已经在数据集上使用了完全相同的filter,因此没有必要在summarize中应用子集。

最后,我认为您的xyCoords可能会遇到一些严重的问题。由于您在两个数据集上使用不同的过滤器,因此xy的向量长度可能会有所不同。这将导致问题。我的建议是您通过使用full_join以某种方式将两个数据集合并在一起,以确保xy始终具有相同的长度。这更多是与dplyr相关,而不是与shiny相关。

我还更改了您的一些reactive对象。

UI:

library(shiny)
library(shinydashboard)
library(tidyverse)

ui <- dashboardPage(
  dashboardHeader(title="Membership Satisfaction"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Demographics Dashboard", tabName = "demos", icon = 
                 icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(

      tabItem(tabName = "demos",
              sidebarPanel(
                checkboxGroupInput("inpt","Select variables to plot", choices = 
                                     c("Web" = 1,"Huddle" = 3, "Other" = 5, "Test" = 7)),
                checkboxGroupInput("role", 
                                   "Select Primary Role of Interest", 
                                   choices = c("Student" = 1, "Not" = 2)),
                checkboxGroupInput("range", 
                                   "Select year(S) of Interest", 
                                   choices = c("2016"=2,"July 2017"=1))),
              fluidPage(

                plotOutput("plot")

              )))))

服务器:

server <- function(input,output){

  library(tidyverse)

  GapAnalysis_LongFormImpt <- structure(list(status = c(1, 5, 5, 1, 1, 5), year = c(1, 1, 1, 
                                                                                    1, 1, 1), Product = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", 
                                                                                                                                                        "2", "3", "4"), class = "factor"), Score = c(1, 1, 3, 2, 2, 1
                                                                                                                                                        )), .Names = c("status", "year", "Product", "Score"), row.names = c(NA, 
                                                                                                                                                                                                                            6L), class = "data.frame")


  GapAnalysis_LongFormSat <- structure(list(status = c(5, 5, 1, 1, 5, 1), year = c(1, 1, 1, 
                                                                                   1, 1, 1), Product = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
                                                                                                                                                       "2", "3", "4"), class = "factor"), Score = c(2, 3, 2, 1, 1, 1
                                                                                                                                                       )), .Names = c("status", "year", "Product", "Score"), row.names = c(NA, 
                                                                                                                                                                                                                           6L), class = "data.frame")

  x <- reactive({
    inpt <- as.double(input$inpt)
    role <- as.double(input$role)
    range <- as.double(input$range)

    GapAnalysis_LongFormSat %>%
      filter(Product %in% inpt,
             status %in% role,
             year %in% range) %>%
      summarize(Avg = mean(Score, na.rm = TRUE)) %>%
      pull(-1)
  })


  y <- reactive({
    inpt <- as.double(input$inpt)
    role <- as.double(input$role)
    range <- as.double(input$range)

    GapAnalysis_LongFormImpt %>%
      filter(Product %in% inpt,
             status %in% role,
             year %in% range) %>% 
      summarize(Avg = mean(Score, na.rm = TRUE))%>%
      pull(-1)
  })

  xyCoords<- reactive({
    x <- x()
    y <- y()

    data.frame(col1=x, col2=y)})

  output$plot <- renderPlot({
    xyCoords <- xyCoords()

    xyCoords %>% 
      ggplot(aes(x = col1, y = col2)) +
      geom_point(colour ="green", shape = 17, size = 5 )+
      labs(x = "Mean Satisfaction", y = "Mean Importance") +
      xlim(0,5) + ylim(0,5) +
      geom_vline(xintercept=2.5) + 
      geom_hline(yintercept =  2.5)})

}



shinyApp (ui = ui, server = server)

1
你的代码中有一些拼写错误,应该是 summarize(Avg =,只有一个等号。 - MLavoie
谢谢!这只允许在填充所有字段后绘制一次,有没有办法让它为每个分解/变量绘制? - Ellie
对原始代码进行了更正... 至于绘制每个变量/分解图,根据您所拥有的代码,我建议使用checkboxGroupInput中的selected参数添加默认选择所有内容。 - Dave Gruenewald
@DaveGruenewald,我现在遇到了颜色/形状编码的问题...也许你可以帮忙吗??问题链接-> https://stackoverflow.com/questions/50139261/ggplot-how-to-color-variables-that-have-parent-variables - Ellie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接