【问题标题】:creating a bar chart with shiny r用闪亮的 r 创建条形图
【发布时间】: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

标签: r csv shiny


【解决方案1】:

这对我有用。请注意,我已将数据更改为我自己的示例数据,并且我假设有一列“年份”来指示数据属于 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)

【讨论】:

  • 谢谢!这是我的数据看起来像年份人口和家庭作为列标题 2000 997936 391043 2010 1229226 474030
  • 弗洛里安,我有两行数据(2000、2010)和三列,年份、人口和家庭。数据在这种格式的 csv 文件中,2000 997936 391043 和 2010 1229226 和 474030。我如何读入这些数据并执行上面的代码?很抱歉问这些基本问题,但我有点困惑。
  • 嗨,Joe,我的笔记本电脑目前没有,但如果明天不能解决,我会给你一个更新的例子。
  • 谢谢!!!我试图读取我的 csv 文件,它给了我“不正确的名称数量”。当我执行名称(数据)时,它会返回 > 名称(数据)[1]“年份”“人口”“家庭”“X”我认为它是从之前尝试将数据分配给该变量的“X” ,所以我也在想办法删除它。
  • 我修改了我的答案,所以它应该适用于您的数据。您可以执行 data$X
猜你喜欢
  • 2016-03-25
  • 2019-10-12
  • 1970-01-01
  • 2021-10-14
  • 2018-06-09
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
相关资源
最近更新 更多