首页 > 解决方案 > 带有分类和数值数据过滤器的闪亮应用程序

问题描述

我有一个包含分类变量和数值变量的数据集。

我的数据看起来像这样

     Region        Country Project.ID            Client   PG Percent.of.CoE Status
1         Africa          Sudan       1001          Vodafone PG 1             50 Signed
2         Europe         Russia       1002   Vodafone Russia PG 2             50    Low
3        Europe  United Kingdom       1003            Orange PG 3             50 Signed
4  Latin America           Peru       1004 Co-operative Bank PG 3             50 Signed
5           Asia       Malaysia       1005       AB Malaysia PG 2             14 Signed
6         Europe         France       1006            Orange PG 4             50   High
7         Africa   South Africa       1007        Coris Bank PG 1             40 Signed
8           Asia          China       1008         Gulf Bank PG 2             50    Low
9  North America  United States       1009               ABI PG 1             50 Signed
10        Europe        Germany       1010                O2 PG 2             50 Medium
11 Latin America      Argentina       1011              ACEP PG 3             40    Low
12 North America         Canada       1012 BCN United States PG 1            100 Signed

样本数据存储在这里

我想用这些数据做什么?我想创建一个简单的应用程序,它具有过滤分类和数字变量。我当前的 UI 看起来像这样,这是我想要的 UI。 我当前的 UI 看起来像这样,这是我想要的 UI。

第一个过滤器完美运行。第二个不是由于数据结构。

试图解决它,我改变了数据格式,使用收集,见下面的代码。结果我的数据看起来像这样。

Percent.of.CoE variable             value
1              50   Region            Africa
2              50   Region            Europe
23             40  Country         Argentina
24            100  Country            Canada
25             50   Client          Vodafone
26             50   Client   Vodafone Russia
47             40       PG              PG 3
48            100       PG              PG 1
49             50   Status            Signed
50             50   Status               Low

我不确定这是正确的格式。但任何解决方案都会奏效。

我的代码

library(shiny)
library(shinythemes)
library(tidyverse)

# Global code

# Read file on a local machine
data_pg <- read.csv("pg1.csv", header = TRUE, stringsAsFactors = FALSE)

# Transform into tidy data, removing long/lat variables. 
data_pg_df3 <- data_pg %>% select(Region, Country, Client, PG, Status, 
Percent.of.CoE) %>% gather(key = "variable", value = "value", - 
c("Percent.of.CoE"))

# UI code

ui <- fluidPage(theme = shinytheme("united"),
            titlePanel(h1("Test", align = "center")),
            sidebarLayout(
              sidebarPanel(
                           selectInput("dataInput", "Choose to filter by:",
                                       choices = c("Region",
                                                   "Country",
                                                   "Client",
                                                   "PG",
                                                   "Status"),
                                       selected = "Choose to display by"),
                           sliderInput("percentInput1", "Percent of CoE", min = 0, 
                                       max = 100, value = c(0, 0))
              ),


              mainPanel(

                # Output
                tabsetPanel(type = "tabs",
                            tabPanel("Plot",  plotOutput("plot", height = 850)))
                )
              )
            )


#  Server code
server <- function(input, output) {

# 1. Select among columns
selectedData <- reactive({
filter(data_pg_df3, variable == input$dataInput)
}) 

output$plot <- renderPlot({
ggplot(selectedData(), aes(x = value, fill = value)) + geom_bar(stat = "count") + theme(axis.title = element_blank())
})

如何编写第二个过滤器?我做到了。但是错误的,也可能是错误的过滤。但我认为我的数据框不适合这个。

 # # 2. Select among percents
 # selectedPercent <- reactive({
 # filter(data_pg_df3, Percent.of.CoE >= input$percentInput1[1] & 
 Percent.of.CoE <= input$percentInput1[2])
 # })
 # 
 # output$plot <- renderPlot({
 # ggplot(selectedPercent(), aes(x = value, fill = value)) + geom_bar(stat = "count") + theme(axis.title = element_blank())
 # })


}
shinyApp(ui = ui, server = server)

我想按变量过滤,然后按百分比过滤,只留下选定范围内的项目。

标签: rggplot2shiny

解决方案


我认为这与您的数据结构无关。尝试以下操作:

server <- function(input, output) {

# 1. Select among columns
filtered_data_1 <- reactive({
filter(data_pg_df3, variable == input$dataInput)
}) 

filtered_data_2 <- reactive({
filter(filtered_data_1(), Percent.of.CoE == input$percentInput1)
}) 

output$plot <- renderPlot({
ggplot(filtered_data_2(), aes(x = value, fill = value)) + geom_bar(stat = "count") + theme(axis.title = element_blank())
})

关键是将一个反应式传递给另一个反应式。或者,您可以在同一个反应式中应用两个过滤器:

server <- function(input, output) {

# 1. Select among columns
filtered_data <- reactive({
    data_pg_df3 %>%
        filter(variable == input$dataInput)
        filter(Percent.of.CoE == input$percentInput1)
}) 

output$plot <- renderPlot({
ggplot(filtered_data(), aes(x = value, fill = value)) + geom_bar(stat = "count") + theme(axis.title = element_blank())
})

这可以通过多种方式使用您的原始数据结构来完成。例如,您可以只过滤Percent.of.CoE然后将给出的input$dataInput列传递给您的 ggplot 审美。


推荐阅读