在 Shiny 中按列名过滤雷达图
Filtering a radar chart by column names in Shiny
多亏了下面的代码,我可以在雷达图上显示 7 辆汽车(A 到 G)的特征,我可以通过单击左侧的复选框来选择要显示的汽车数量图表 :
if (interactive()) {
library(shiny)
library(ECharts2Shiny)
dat <- data.frame(Car.A = c(4300, 10000, 25000, 35000, 50000),
Car.B = c(5000, 14000, 28000, 31000, 42000),
Car.C = c(2400, 5000, 9020, 9200, 26000),
Car.D = c(8300, 11000, 15000,25000, 40000),
Car.E = c(23000, 14000, 28000, 31000, 42000),
Car.F = c(4000, 2000, 9000, 29000, 35000),
Car.G = c(4800, 1000, 5000, 21000, 15000))
row.names(dat) <- c("Feture 1", "Feature 2", "Feature 3", "Feature 4", "Feature 5")
# Server function -------------------------------------------
server <- function(input, output) {
renderRadarChart(div_id = "test",
data = dat)
}
# UI layout -------------------------------------------------
ui <-
fluidRow(
column(width = 3,
box(width = NULL, selectInput("variable", "choose any of the following", names(dat), multiple=TRUE)
)),
column(width = 5,
box(width = NULL,
loadEChartsLibrary(),
tags$div(id="test", style="width:100%;height:400px;"),
deliverChart(div_id = "test")
))
)
# Run the application --------------------------------------
shinyApp(ui = ui, server = server)
}
# }
但是这个选项不适合我,我宁愿使用 select 列表输入控件:
# Server function -------------------------------------------
server <- function(input, output) {
renderRadarChart(div_id = "test",
dat %>% dplyr::select(!!!input$variable)
)
}
# UI layout -------------------------------------------------
ui <-
fluidRow(
column(width = 3,
box(width = NULL, selectInput("variable", "choose any of the following", names(dat), multiple=TRUE)
)),
column(width = 5,
box(width = NULL,
loadEChartsLibrary(),
tags$div(id="test", style="width:100%;height:400px;"),
deliverChart(div_id = "test")
))
)
但不幸的是,这个过滤器选项不起作用,我只设法得到这个错误消息:
Error: only defined on a data frame with all numeric variables
我做错了什么?
对您的服务器代码进行微调应该可以解决该错误。试试这个
# Server function -------------------------------------------
server <- function(input, output) {
observe({
req(input$variable)
data <- dat %>% dplyr::select(!!!input$variable)
renderRadarChart(div_id = "test",data)
})
}
多亏了下面的代码,我可以在雷达图上显示 7 辆汽车(A 到 G)的特征,我可以通过单击左侧的复选框来选择要显示的汽车数量图表 :
if (interactive()) {
library(shiny)
library(ECharts2Shiny)
dat <- data.frame(Car.A = c(4300, 10000, 25000, 35000, 50000),
Car.B = c(5000, 14000, 28000, 31000, 42000),
Car.C = c(2400, 5000, 9020, 9200, 26000),
Car.D = c(8300, 11000, 15000,25000, 40000),
Car.E = c(23000, 14000, 28000, 31000, 42000),
Car.F = c(4000, 2000, 9000, 29000, 35000),
Car.G = c(4800, 1000, 5000, 21000, 15000))
row.names(dat) <- c("Feture 1", "Feature 2", "Feature 3", "Feature 4", "Feature 5")
# Server function -------------------------------------------
server <- function(input, output) {
renderRadarChart(div_id = "test",
data = dat)
}
# UI layout -------------------------------------------------
ui <-
fluidRow(
column(width = 3,
box(width = NULL, selectInput("variable", "choose any of the following", names(dat), multiple=TRUE)
)),
column(width = 5,
box(width = NULL,
loadEChartsLibrary(),
tags$div(id="test", style="width:100%;height:400px;"),
deliverChart(div_id = "test")
))
)
# Run the application --------------------------------------
shinyApp(ui = ui, server = server)
}
# }
但是这个选项不适合我,我宁愿使用 select 列表输入控件:
# Server function -------------------------------------------
server <- function(input, output) {
renderRadarChart(div_id = "test",
dat %>% dplyr::select(!!!input$variable)
)
}
# UI layout -------------------------------------------------
ui <-
fluidRow(
column(width = 3,
box(width = NULL, selectInput("variable", "choose any of the following", names(dat), multiple=TRUE)
)),
column(width = 5,
box(width = NULL,
loadEChartsLibrary(),
tags$div(id="test", style="width:100%;height:400px;"),
deliverChart(div_id = "test")
))
)
但不幸的是,这个过滤器选项不起作用,我只设法得到这个错误消息:
Error: only defined on a data frame with all numeric variables
我做错了什么?
对您的服务器代码进行微调应该可以解决该错误。试试这个
# Server function -------------------------------------------
server <- function(input, output) {
observe({
req(input$variable)
data <- dat %>% dplyr::select(!!!input$variable)
renderRadarChart(div_id = "test",data)
})
}