【发布时间】:2017-07-21 17:38:26
【问题描述】:
我是 R 新手,正在寻求帮助。我有一个两行(2000 年、2010 年)和两列(2000 年人口 997936、家庭 391043 和 2010 年分别为 1229226 和 474030)的 csv。我正在尝试使用带单选按钮的闪亮创建一个反应性条形图来选择 2000 或 2010 数据,但无法使其工作。我知道这与我过滤的方式有关,但我无法弄清楚。这是我的代码,我真诚地感谢任何帮助。从 cmets 可以看出,我已经尝试了很多。
library(shiny)
data <- read.csv("hillsctypop.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE)
ui <- fluidPage(
titlePanel(title = h4("Hillsborough County Population by Census", align="center")),
sidebarPanel(
radioButtons("YEAR", "Select the Census Year",
choices = c("2000", "2010"),
selected = "2000")),
mainPanel(
plotOutput("bar",height = 500))
)
server <- function(input,output){
#year = reactive(data({input$YEAR}))
# filtered <- reactive({
#data %>%
#filter(Type == input$year)
#})
output$bar <- renderPlot({
# barplot(as.matrix(data))
# barplot(data()[,2,4,])
#x <- data[1, ]
color <- c("blue", "red")
barplot(as.integer(data$Population, data$Households),
main = input$YEAR,
ylab="Total",
xlab="Census Year",
names.arg = c("Population", "Households"),
col = color)
#legend("topright", legend = c("Population", "Households"),
# fill = c("Blue", "red"))
})
}
shinyApp(ui=ui, server=server)
【问题讨论】:
-
您需要将数据子集化到
input$YEAR