【发布时间】:2016-01-24 22:07:04
【问题描述】:
如何在 Shiny 的 selectInput 调用中添加要从中选择的类变量,以从 ggplot 生成条形图?在我的数据集总数中,我想选择 Class 变量以生成不同的 ggplot 条形图。我收到错误:“尝试选择少于一个元素”。
totals<-data.frame(Class=c("A","A","A","B","B","B","c","C","D","D","D","D"),
Type=c("Type1","Type2","Type3","Type1","Type2","Type3","Type1",
"Type2","Type1","Type2","Type3","Type4"),
Acres=c(45,543,434,434,434,455,683,345,567,77,52,86))
server.r 代码如下:
library(shiny)
library(ggplot2)
shinyServer(function(input, output, session) {
output$acresPlot <- reactivePlot(function() {
acresData <- data.frame(acres = totals$Acres, var = factor(totals[[input$variable]]))
p <- ggplot(acresData, aes(var, acres)) +
geom_bar(stat="identity")
print(p)
})
})
ui.r 代码如下:
shinyUI(pageWithSidebar(
headerPanel("My types by class"),
sidebarPanel(
selectInput("Class", "Class:",
c("A" = "A",
"B" = "B",
"C" = "C",
"D"="D"))),
mainPanel(
plotOutput("acresPlot")
)
))
【问题讨论】: