我正在尝试建立一个仪表板,用户可以在其中按年份,状态和产品过滤数据。理想情况下,它应该在每个产品具有2个变量(满意度得分和重要性得分)相关联的地方运行。从数据集中过滤时,应针对用户感兴趣的各个细分市场计算汇总平均值。分数组合成一个data.frame并绘制在一个图上。

这是我在的地方

我的UI

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")

它有效-仅不能完全满足我的需要。当前,它在绘制之前需要所有3个复选框输入变量(整数,角色,范围)中的输入。我需要它来需要一种产品,但需要为每个其他输入绘图。意思是,如果他们选择Web,它将绘制Web的均值。如果他们选择Web和2017年,则将绘制2017年Web的平均值。

任何帮助都非常感谢!!!!

最佳答案

变化

我认为这里有些事情会引起一些麻烦:

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

接下来,将==filter结合使用,而应使用%in%代替。这允许多个选择,而不仅仅是单个选择。如果只想选择一个,请使用radioButtons()而不是checkboxGroupInput()

summarize中,您正在使用其他不必要的子集。我们已经在数据集上使用了完全相同的filter,因此无需在summarize中应用子集。

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

我还更改了一些reactive对象。

使用者介面:

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)

关于r - R Shiny DPLyr无功滤波器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50118326/

10-16 08:44