Shiny app with filters for categorical and numerical data
我有一个包含分类和数字变量的数据集。
我的数据看起来像这样
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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。
第一个过滤器运行良好。第二个不是由于数据结构。
尝试解决该问题,我更改了tha的数据格式,使用了collect(收集),请参见下面的代码。结果,我的数据如下所示。
1 2 3 4 5 6 7 8 9 10 11 | 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 |
我不确定此格式是否正确。但是任何解决方案都可以。
我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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()) }) |
如何编写第二个过滤器?我做到了但是错误的过滤也可能是错误的。但是我认为我的数据框对此不利。
1 2 3 4 5 6 7 8 9 10 11 12 13 | # # 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) |
我想按变量过滤,然后按百分比过滤,只保留选定范围内的项目。
我也解决了。
我的服务器看起来像这样。总的来说,我是对的,上面的答案也是正确的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # Server server <- function(input, output) { # 1. Select among columns selectedData <- reactive({ filter(data_pg_df3, variable == input$dataInput) %>% filter(Percent.of.CoE >= input$percentInput1[1] & Percent.of.CoE <= input$percentInput1[2]) }) output$plot <- renderPlot({ ggplot(selectedData(), aes(x = value, fill = value)) + geom_bar(stat ="count") + theme(axis.title = element_blank()) }) } |
对我而言,最棘手的部分是UI。
而不是
1 2 | sliderInput("percentInput1","Percent of CoE", min = 0, max = 100, value = c(0, 0)) |
我放了
1 2 | sliderInput("percentInput1","Percent of CoE", min = 0, max = 100, value = c(1, 99)) |
这解决了我的问题。现在可以完美运行了。
如前所述,我将带有参数的sliderInput用作参数,因为我需要选择一个数据范围。
我认为这与您的数据结构无关。尝试如下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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()) }) |
关键是将一种反应性传递给另一种。或者,您可以在同一个反应式中应用两个过滤器:
1 2 3 4 5 6 7 8 9 10 11 12 | 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()) }) |
这可以通过多种方式使用原始数据结构来完成。例如,您可以只过滤