如何渲染根据我的输入而变化并显示每条线的彩色线的线图

时间:2019-03-04 17:58:05

标签: r shiny

我创建了这个闪亮的应用程序,现在我想向该应用程序添加线条图。 数据位于.csv文件中

我能够以表格格式生成数据,并且希望包含一个对输入有反应的折线图。

shelter <- read.csv("shelter.csv",stringsAsFactors=FALSE)

Shelter,Year,Cat,Dog,Rabbit,Other
Pitt,2013,31,22,19,23
Pitt,2014,23,54,65,15
Pitt,2015,56,62,28,24
Pitt,2016,65,23,33,32
Pitt,2017,49,74,36,18
Phila,2013,11,32,26,35
Phila,2014,66,65,145,27
Phila,2015,69,64,121,18
Phila,2016,84,81,195,9
Phila,2017,79,35,96,7
Allen,2013,161,36,26,11
Allen,2014,24,97,84,21
Allen,2015,101,74,24,19
Allen,2016,254,74,112,3
Allen,2017,95,63,247,22
Harris,2013,78,60,168,17
Harris,2014,29,85,39,16
Harris,2015,201,75,245,7
Harris,2016,27,55,88,9
Harris,2017,65,46,71,11
Read,2013,94,95,68,20
Read,2014,98,91,94,19
Read,2015,125,73,203,21
Read,2016,87,101,119,5
Read,2017,148,98,149,6
York,2013,56,73,65,14
York,2014,61,74,95,7
York,2015,99,89,84,2
York,2016,121,120,84,11
York,2017,67,68,85,2

#Code:

library(shiny)
ui <- fluidPage(

  titlePanel('Animal Shelter Data:'),
  sidebarLayout(
    sidebarPanel(
      selectInput("Shelter", label = h4("Select a Shelter:"),choices =shelter$Shelter),

      checkboxGroupInput("Category", label = h4("Category"), 
                     choices = list("Cat" , "Dog" , "Rabbit", "Other"),
                     selected = list("Cat" , "Dog" , "Rabbit", "Other")),

      checkboxGroupInput("Year", label = h4("Select Year(s)"),
                     choices = unique(shelter$Year),
                     selected = list('2013', '2014', '2015', '2016','2017'))
    ),

    mainPanel(
      tableOutput("shelterdata"),
      plotOutput("lineplot")
    )
  ) 
)

server <- function(input, output) {

  output$shelterdata <- renderTable({
    shelterfilter <- subset(shelter[shelter$Shelter == input$Shelter & shelter$Year %in% input$Year,])
    shelterfilter[c('Shelter', 'Year', input$Category)]
  })


}

shinyApp(ui = ui, server = server)

我想绘制一个线条图,该线条图根据我的input$Shelterinput$Categoryinput$Year而变化,并为每只动物显示一条色线:

  • x轴=年
  • y轴=动物数量

1 个答案:

答案 0 :(得分:0)

此答案需要curTitle.setFocusable(true); curTitle.setFocusableInTouchMode(true); curTitle.requestFocus(); //curTitle.requestFocusFromTouch();tidyrmagrittr软件包。这段代码可以放在ggplot2函数中。

server