【发布时间】:2020-03-13 16:56:42
【问题描述】:
我是创建 R Shiny 应用程序的新手。到目前为止,我正在制作我的应用程序的一部分,我试图根据选择要分析的变量生成不同的图。我将以内置数据集iris 为例。
library(shiny)
library(tidyverse)
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel("Create plots of mean variables by species. ",
varSelectInput("vars", h5("Choose a variable to display."),
data = iris,
selected = "Sepal.Length"),
sliderInput("toprange", h5("Display Number of Species"),
min = 1, max = 3, value = 3)),
#in my actual dataset there are more than 30 different levels.
mainPanel(plotOutput("bars"))
)
)
server <- function(input, output) {
output$bars <- renderPlot({
species_plot(input$vars, input$toprange)
})
}
shinyApp(ui = ui, server = server)
这是用于创建绘图的函数:
species_plot <- function(variable, min) {
iris %>%
group_by(Species) %>%
filter(Species != "") %>%
summarize(avg = mean({{variable}})) %>%
top_n(avg, min) %>%
ggplot(aes(x = reorder(Species, avg), y = avg)) +
geom_col() +
labs(x = "Species", y = paste("Mean", toString(sym(variable)))) +
ggtitle(paste("Mean", toString(sym(variable)), "by Species")) +
coord_flip()
}
当我运行应用程序时,侧边栏上的所有内容都会显示,但在主面板上会弹出一个错误“需要 TRUE/FALSE 的缺失值”,我不确定这是从哪里来的。例如,我在任何地方都看不到会输出此错误的条件。
【问题讨论】: