【发布时间】:2021-09-29 04:42:24
【问题描述】:
我想使用用户选择的变量来过滤(然后绘制)数据。但是我收到一个错误,即不能强制“list”对象键入“double”。这是一个虚拟示例,我想说:“如果用户从变量列表中选择“齿轮”,则显示齿轮 = 4 的所有汽车的马力直方图。
library(shiny)
mtcars_cols <- mtcars %>% select(gear, carb)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Motor Car Trend Road Tests"),
# Sidebar
sidebarLayout(
varSelectInput("variables",
"Select variables: ",
mtcars_cols,
multiple = TRUE),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
dt <- mtcars %>%
filter(!!!input$variables == 4)
bins <- seq(min(x), max(x), length.out = 10 + 1)
# draw the histogram with the specified number of bins
hist(dt$hp, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】: