r - 带有分类和数值数据过滤器的闪亮应用程序
问题描述
我有一个包含分类变量和数值变量的数据集。
我的数据看起来像这样
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。
第一个过滤器完美运行。第二个不是由于数据结构。
试图解决它,我改变了数据格式,使用收集,见下面的代码。结果我的数据看起来像这样。
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)
我想按变量过滤,然后按百分比过滤,只留下选定范围内的项目。
解决方案
我认为这与您的数据结构无关。尝试以下操作:
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 审美。
推荐阅读
- javascript - 如何将来自axios post的数据存储在localstorage中
- r - 将存储在列表中的数据框列中的向量传递给 glm
- sql - 如何在现有位置插入新行之前更新唯一表行号
- reactjs - React TypeError:无法读取 null 的属性“propName”
- c++ - 给定基类方法的编译时覆盖
- javascript - 访问回调函数范围之外的变量?
- vue.js - 如何使用 vuetify@2.0.0-beta7
- linux - Shell:将带参数的函数作为函数参数传递
- r - 如何在 pheatmap 中获得连续的比例尺
- selenium-webdriver - driver.findElement(By.xpath("[@id="smallWidgetDiv"]/div[1]/div/div/div/div")); - 语法错误