基于 pickerInput 的子集数据框
Subset dataframe based on pickerInput
我有一个这样的数据框:
我想根据多项选择对数据框进行子集化。 I used pickerInput
function from shinyWidgets
but i dont get the desired result, some observations are omitted : When x
and y
are selected, the resulted dataframe is incorrect, I got这 :
而不是这个
我们该如何解决?这就是我所做的:
library("shiny")
library(DT)
library(shinyWidgets)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("id", "variable:",
choices = c( "x", "y","z"),
options = list(`actions-box` = TRUE),
selected = "z",
multiple = TRUE )),
mainPanel(
dataTableOutput("example")
)
)
),
server = function(input, output) {
df <- data.frame(variable = c(rep("x",4),rep("y",4),rep("z",4)),
x1 = 1:12)
output$example <- renderDT({
df2 <- df %>%
filter(variable == input$id )
})
}
)
一些帮助将不胜感激
问题是使用 ==
而不是 %in%
。 ==
是逐元素运算符,它在 rhs
上只有一个元素时有效,因为它会循环,而长度 > 1 且不等于 lhs
向量的长度,它会回收,但随后比较会得到不正确的输出。
> library(dplyr)
> df %>% mutate(new = variable == c("x", "y"))
variable x1 new
1 x 1 TRUE
2 x 2 FALSE
3 x 3 TRUE
4 x 4 FALSE
5 y 5 FALSE
6 y 6 TRUE
7 y 7 FALSE
8 y 8 TRUE
9 z 9 FALSE
10 z 10 FALSE
11 z 11 FALSE
12 z 12 FALSE
> df %>% mutate(new = variable %in% c("x", "y"))
variable x1 new
1 x 1 TRUE
2 x 2 TRUE
3 x 3 TRUE
4 x 4 TRUE
5 y 5 TRUE
6 y 6 TRUE
7 y 7 TRUE
8 y 8 TRUE
9 z 9 FALSE
10 z 10 FALSE
11 z 11 FALSE
12 z 12 FALSE
如果我们检查第一个比较,'x'、'y'将与前两行进行比较,然后再循环,再次'x'、'y'是比较等等,直到它到达最后一行(在某些情况下,当元素数量不是 rhs 向量长度的倍数时会发出警告)
library(DT)
library(shinyWidgets)
library(dplyr)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("id", "variable:",
choices = c( "x", "y","z"),
options = list(`actions-box` = TRUE),
selected = "z",
multiple = TRUE )),
mainPanel(
dataTableOutput("example")
)
)
),
server = function(input, output) {
df <- data.frame(variable = c(rep("x",4),rep("y",4),rep("z",4)),
x1 = 1:12)
output$example <- renderDT({
df2 <- df %>%
filter(variable %in% input$id )
})
}
)
-输出
我有一个这样的数据框:
我想根据多项选择对数据框进行子集化。 I used pickerInput
function from shinyWidgets
but i dont get the desired result, some observations are omitted : When x
and y
are selected, the resulted dataframe is incorrect, I got这 :
而不是这个
我们该如何解决?这就是我所做的:
library("shiny")
library(DT)
library(shinyWidgets)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("id", "variable:",
choices = c( "x", "y","z"),
options = list(`actions-box` = TRUE),
selected = "z",
multiple = TRUE )),
mainPanel(
dataTableOutput("example")
)
)
),
server = function(input, output) {
df <- data.frame(variable = c(rep("x",4),rep("y",4),rep("z",4)),
x1 = 1:12)
output$example <- renderDT({
df2 <- df %>%
filter(variable == input$id )
})
}
)
一些帮助将不胜感激
问题是使用 ==
而不是 %in%
。 ==
是逐元素运算符,它在 rhs
上只有一个元素时有效,因为它会循环,而长度 > 1 且不等于 lhs
向量的长度,它会回收,但随后比较会得到不正确的输出。
> library(dplyr)
> df %>% mutate(new = variable == c("x", "y"))
variable x1 new
1 x 1 TRUE
2 x 2 FALSE
3 x 3 TRUE
4 x 4 FALSE
5 y 5 FALSE
6 y 6 TRUE
7 y 7 FALSE
8 y 8 TRUE
9 z 9 FALSE
10 z 10 FALSE
11 z 11 FALSE
12 z 12 FALSE
> df %>% mutate(new = variable %in% c("x", "y"))
variable x1 new
1 x 1 TRUE
2 x 2 TRUE
3 x 3 TRUE
4 x 4 TRUE
5 y 5 TRUE
6 y 6 TRUE
7 y 7 TRUE
8 y 8 TRUE
9 z 9 FALSE
10 z 10 FALSE
11 z 11 FALSE
12 z 12 FALSE
如果我们检查第一个比较,'x'、'y'将与前两行进行比较,然后再循环,再次'x'、'y'是比较等等,直到它到达最后一行(在某些情况下,当元素数量不是 rhs 向量长度的倍数时会发出警告)
library(DT)
library(shinyWidgets)
library(dplyr)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("id", "variable:",
choices = c( "x", "y","z"),
options = list(`actions-box` = TRUE),
selected = "z",
multiple = TRUE )),
mainPanel(
dataTableOutput("example")
)
)
),
server = function(input, output) {
df <- data.frame(variable = c(rep("x",4),rep("y",4),rep("z",4)),
x1 = 1:12)
output$example <- renderDT({
df2 <- df %>%
filter(variable %in% input$id )
})
}
)
-输出