R/闪亮 Select 来自数据列的数据 Table
R/ Shiny Select DataFrom Column in Data Table
比如说我的数据是:
Year Car Doors Cylinder Transmission
2003 Nissan 4 4 MT
2006 Nissan 4 4 MT
2003 Honda 2 6 AT
我要Shiny return MT首秀。我该怎么做?
我的代码:
df_example <- cars %>%
filter(cylinder = 4,
doors = 4) %>%
slice(1)
我们可以不修改代码filter
library(dplyr)
cars %>%
slice(which(Cylinder == 4 & Doors == 4)[1])
-输出
# Year Car Doors Cylinder Transmission
#1 2003 Nissan 4 4 MT
如果我们需要提取'Transmission',使用pull
cars %>%
slice(which(Cylinder == 4 & Doors == 4)[1]) %>%
pull(Transmission)
注意:在filter
中,使用的表达式是赋值运算符(=
)而不是比较运算符(==
)。此外,R
区分大小写,即它需要准确的列名而不是小写的列名。根据显示的数据,列名是 Cylinder
和 Doors
而不是 cylinder
和 doors
数据
cars <- structure(list(Year = c(2003L, 2006L, 2003L), Car = c("Nissan",
"Nissan", "Honda"), Doors = c(4L, 4L, 2L), Cylinder = c(4L, 4L,
6L), Transmission = c("MT", "MT", "AT")), class = "data.frame",
row.names = c(NA,
-3L))
比如说我的数据是:
Year Car Doors Cylinder Transmission
2003 Nissan 4 4 MT
2006 Nissan 4 4 MT
2003 Honda 2 6 AT
我要Shiny return MT首秀。我该怎么做?
我的代码:
df_example <- cars %>%
filter(cylinder = 4,
doors = 4) %>%
slice(1)
我们可以不修改代码filter
library(dplyr)
cars %>%
slice(which(Cylinder == 4 & Doors == 4)[1])
-输出
# Year Car Doors Cylinder Transmission
#1 2003 Nissan 4 4 MT
如果我们需要提取'Transmission',使用pull
cars %>%
slice(which(Cylinder == 4 & Doors == 4)[1]) %>%
pull(Transmission)
注意:在filter
中,使用的表达式是赋值运算符(=
)而不是比较运算符(==
)。此外,R
区分大小写,即它需要准确的列名而不是小写的列名。根据显示的数据,列名是 Cylinder
和 Doors
而不是 cylinder
和 doors
数据
cars <- structure(list(Year = c(2003L, 2006L, 2003L), Car = c("Nissan",
"Nissan", "Honda"), Doors = c(4L, 4L, 2L), Cylinder = c(4L, 4L,
6L), Transmission = c("MT", "MT", "AT")), class = "data.frame",
row.names = c(NA,
-3L))