我是R的新手,正在寻求帮助。我有一个有两行(2000年、2010年)和两列(2000年和2010年分别为997936户、391043户、1229226户和474030户)的csv。我试图创建一个反应条形图使用闪亮与单选按钮,以选择2000年或2010年的数据,但无法得到它的工作。我知道这与我过滤的方式有关,但我搞不清楚。这里是我的代码,我将真诚地感谢任何帮助。正如你从评论中看到的,我已经尝试了很多。
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)发布于 2017-07-22 02:00:05
这对我很有效。请注意,我已经将数据更改为我自己的样本数据,并且我假设有一个列' year‘来指示数据属于2000年还是2010年。反应随后被用作绘图函数的输入。我希望这能帮助你找到正确的方向。
data = data.frame(Population=sample(1:20,10),Households = sample(1:20,10), year=sample(c(2000,2010),10,replace=T))
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){
reactive_data = reactive({
selected_year = as.numeric(input$YEAR)
return(data[data$year==selected_year,])
})
output$bar <- renderPlot({
color <- c("blue", "red")
our_data <- reactive_data()
barplot(colSums(our_data[,c("Population","Households")]),
ylab="Total",
xlab="Census Year",
names.arg = c("Population", "Households"),
col = color)
})
}
shinyApp(ui=ui, server=server)https://stackoverflow.com/questions/45243643
复制相似问题