r - R Shiny Reactive dplyr 过滤器以形成新的数据框以进行绘图
问题描述
我正在尝试构建一个反应图,该图使用对用户输入反应的 dplyr 过滤器函数来拉取正确的 x,y 坐标进行绘图。
我的用户界面:
choices <- c("Web" = 1,"Huddle" = 3, "Other" = 5, "Test" = 7)
role <- c("Student" = 1, "Not" = 2)
range <- c("2016"=2,"July 2017"=1)
ui <- dashboardPage(
dashboardHeader(title="Membership Satisfaction"),
dashboardSidebar(
sidebarMenu(
menuItem("Value Dashboard", tabName = "dashboard", icon =
icon("dashboard")),
menuItem("Services Dashboard", tabName = "service", icon =
icon("dashboard")),
menuItem("Demographics Dashboard", tabName = "demos", icon =
icon("dashboard"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "demos",
sidebarPanel(
checkboxGroupInput("inpt","Select variables to plot", choices =
choices),
checkboxGroupInput("role",
"Select Primary Role of Interest",
choices = role),
checkboxGroupInput("yrs",
"Select year(S) of Interest",
choices = range)),
fluidPage(
plotOutput("test")
)))))
和我的服务器:
server <- function(input,output){
x <- reactive({
example1 %>%
dplyr::filter(Product == as.integer(input$inpt))%>%
dplyr::filter(year == as.integer(input$range)) %>%
dplyr::filter(status == as.integer(input$role)) %>%
pull(-2)
})
y <- reactive({
example1 %>%
dplyr::filter(Product == as.integer(input$inpt+1))%>%
dplyr::filter(year == as.integer(input$range)) %>%
dplyr::filter(status == as.integer(input$role)) %>%
pull(-1)
})
z <- reactive({data.frame(x= x(), y = y())})
output$test <- renderPlot({
ggplot(z(), aes(x,y))+
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)
笔记:
Example1 是一个按组、年份和产品汇总平均值的数据框。
X 和 Y 变量过滤以获得特定的产品代码并仅存储平均值。Y 是相应的且始终是乘积 (x+1)。它获取 y 的平均值并存储这些值。变量 Z 然后创建一个 x,y 坐标点的数据框来绘制。
Example1 数据头:
> example1
# A tibble: 24 x 5
# Groups: year, status [12]
year status Product AvgComImpt AvgComSat
<dbl> <dbl> <fct> <dbl> <dbl>
1 1. 1. 1 NaN 2.70
2 1. 1. 2 3.13 NaN
3 1. 2. 1 NaN 2.43
4 1. 2. 2 3.33 NaN
5 1. 3. 1 NaN 2.40
6 1. 3. 2 3.60 NaN
7 1. 5. 1 NaN 3.03
8 1. 5. 2 3.30 NaN
9 1. 7. 1 NaN 3.50
10 1. 7. 2 4.00 NaN
> dput(head(example1))
structure(list(year = c(1, 1, 1, 1, 1, 1), status = c(1, 1, 2,
2, 3, 3), Product = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1",
"2"), class = "factor"), AvgComImpt = c(NaN, 3.12844036697248,
NaN, 3.33333333333333, NaN, 3.6), AvgComSat = c(2.7037037037037,
NaN, 2.42857142857143, NaN, 2.4, NaN)), .Names = c("year", "status",
"Product", "AvgComImpt", "AvgComSat"), row.names = c(NA, -6L), class =
c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = c("year", "status"), drop = TRUE,
indices = list(
0:1, 2:3, 4:5), group_sizes = c(2L, 2L, 2L), biggest_group_size = 2L, labels
= structure(list(
year = c(1, 1, 1), status = c(1, 2, 3)), row.names = c(NA,
-3L), class = "data.frame", vars = c("year", "status"), drop = TRUE, .Names
= c("year",
"status")))
理论上它应该像这样工作:
X 会得到这个:
[1] 3.128440 3.333333 3.600000 3.296296 4.000000 4.000000 4.000000 4.234131
4.254562 4.386861 4.090909 4.387218
Y会得到这个:[1] 2.703704 2.428571 2.400000 3.034483 3.500000 2.666667 4.000000 3.856167 4.045455 3.825000 3.818182 3.996377
然后我的 Z 会得到这个:
1 3.128440 2.703704
2 3.333333 2.428571
3 3.600000 2.400000
4 3.296296 3.034483
5 4.000000 3.500000
6 4.000000 2.666667
7 4.000000 4.000000
8 4.234131 3.856167
9 4.254562 4.045455
10 4.386861 3.825000
11 4.090909 3.818182
12 4.387218 3.996377
但是,当我运行它时,它在应用程序中显示:“错误:结果的长度必须为 2,而不是 0。”
当我选择要绘制的变量时,它会更改为:“错误:结果的长度必须为 1,而不是 0。”
我想知道如果没有选择输入并且由于过滤器抓取用户输入而引发错误,这是否与它有任何关系。但如果是这样的话,我认为在所有输入区域中进行选择至少会为此提供一个情节 - 但事实并非如此。如果这是我做错了,我不知道从哪里开始......任何帮助将不胜感激!
关于如何使此代码正常工作的任何输入/想法?谢谢!!!!
解决方案
推荐阅读
- latex - 乳胶在我的桌子上叠加了两列
- r - r 为什么调用包 R.oo 并产生错误
- azure-devops - 天蓝色管道中的组名称变量可以是动态的吗?
- java - Flink流 - 使用StreamingFileSink时更改部分文件名?
- fortran - C_FUNLOC 的结果是标量还是数组?
- javascript - 这是什么符号?someFunction([{key: value}, {key: value})
- vba - 不能使用 ADODB.Connect 删除 SQLite 数据库中的查询
- javascript - 如何更改 JavaScript 中按钮的颜色以应用于另一个函数?
- javascript - 将框架升级到 .net 4.6 后,javascript 停止工作
- c# - 尝试打开第二个工作簿返回 null