R 2个过滤器的无功函数
我尝试了Flexdashboard中的代码,但部分工作正常。这里有3个用于性能、类别和产品系列的过滤器:R 2个过滤器的无功函数,r,plot,R,Plot,我尝试了Flexdashboard中的代码,但部分工作正常。这里有3个用于性能、类别和产品系列的过滤器: 当性能为“绘图”,类为“全部”,产品系列为“全部”时,将显示绘图,并且绘图是完美的 当性能为“绘图”时,类为“A”且产品系列为空,则显示绘图,该绘图是完美的 但是,当性能为“绘图”,类别为“A”,产品系列为“产品系列1”时,产品系列1不显示绘图,而是所有三个系列都有一个绘图 基本上我需要这些过滤器来满足我的愿望。有没有办法做上述事情 --- title: "ABC INC" runtime:
---
title: "ABC INC"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
source_code: embed
theme: cosmo
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(ggplot2)
library(plotly)
library(reshape)
```
```{r}
df <- structure(list(Class = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
Product = c("Product 1", "Product 2", "Product 3", "Product 4", "Product 5", "Product 1", "Product 2", "Product 3", "Product 4", "Product 5"),
`Product Family` = c("Product Family 1", "Product Family 1", "Product Family 1", "Product Family 2", "Product Family 3", "Product Family 1", "Product Family 1", "Product Family 1", "Product Family 2", "Product Family 3"),
`Month Ending Date` = structure(c(1420070400, 1422748800, 1425168000, 1427846400, 1430438400, 1420070400, 1422748800, 1425168000, 1427846400, 1430438400),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Spend = c(95, 93, 98, 100, 93, 95, 93, 98, 100, 93)),
row.names = c(NA, -10L),
class = c("tbl_df", "tbl", "data.frame"))
df <- as.data.frame(df)
colnames(df) <- gsub(" ","_",colnames(df))
df$Month_Ending_Date <- as.Date(df$Month_Ending_Date)
```
Performance
=======================
Filters {.sidebar}
------------------
```{r}
selectInput("Plot",h5("Performance"), choices = c("","Plot"))
selectInput("Class",h5("Class"), choices = c("All",levels(factor(df$Class))), multiple = TRUE)
selectInput("ProductFamily",h5("Product Family"), choices = c("All",levels(factor(df$Product_Family))), multiple = TRUE)
```
Output
------------------
### Chart A {data-heigh=500}
```{r}
plotOutput("graph",height = "5000px",width = "1000px")
sel_data <- reactive({
df[df$Class %in% input$Class | df$Product_Family %in% input$ProductFamily,]
})
graph <- reactive({
if (input$Plot == "Plot" && input$Class == "All" && input$ProductFamily == "All") {
ggplot(df,aes(x=Month_Ending_Date,y=Spend,color = Product)) +
geom_point()+facet_wrap("Product_Family") + theme(legend.position = "none") +
theme(legend.position = "none")
} else {
ggplot(sel_data(),aes(x=Month_Ending_Date,y=Spend,color = Product)) +
geom_point()+facet_wrap("Product_Family") +
theme(legend.position = "none") +
geom_text(data = sel_data(), aes(x=Month_Ending_Date, label=round((Spend/1000000),2)),
size=3,angle = 45, color = "black",vjust=-0.25)+theme(legend.position = "bottom")
}
})
output$graph <- renderPlot({
graph()
})
```
---
标题:“ABC公司”
运行时间:闪亮
输出:
flexdashboard::flex_仪表板:
方向:行
垂直布局:滚动
源代码:嵌入
主题:宇宙
---
```{r设置,include=FALSE}
库(flexdashboard)
图书馆(readxl)
图书馆(tidyverse)
图书馆(GG2)
图书馆(绘本)
图书馆(重塑)
```
```{r}
df考虑扩展sel_data()
reactive函数,以处理空输入和所有选择:
sel_data <- reactive({
# ADJUST INPUTS FOR ALL COLUMN FACTORS
if(input$Class == "All" | is.na(input$Class) | is.null(input$Class))
input$Class <- levels(factor(df$Class))
if(input$Product_Family == "All" | is.na(input$Product_Family) | is.null(input$Product_Family))
input$Product_Family <- levels(factor(df$Product_Family))
# ALL POSSIBLE CHOICES
choices_df <- expand.grid(Class = levels(factor(df$Class)),
Product_Family = levels(factor(df$Product_Family))
)
# SUBSET CHOICES TO INPUT VALUES
sub_df <- with(choices_df, choices_df[Class %in% input$Class &
Product_Family %in% input$Product_Family,])
# MERGE TO MAIN FOR FILTERED RECORDS
merge(df, sub_df, by=c("Class", "Product_Family"))
})
sel\u data尝试调整sel\u data()过滤器以使用AND(&
)而不是OR(
):df[df$Class%in%input$Class&df$Product\u Family%in%input$ProductFamily,]
我不知道你在说什么。如果我替换或由和替换。我应该过滤所有这些过滤器。比方说,我只过滤性能和类。我应该得到输出。但是如果我像你说的那样替换,我应该过滤所有3个过滤器。希望你明白我的意思我需要知道我的A班表现如何。但在我了解了A类的性能之后,我需要详细说明,比如如果我选择产品系列1,我应该只得到1个绘图。我说得清楚了吗?没有,没有,伙计:)如果你能编辑代码并分享解决方案,我将不胜感激。我尽了最大努力,但无法解决您收到的错误或意外结果是什么?演示显示该过程覆盖任何一对类和产品系列,即使是空的。