Shinyapp:如何在csv文件中调用匹配用户输入并显示直方图的列

时间:2017-11-02 17:01:19

标签: r csv shiny

我正在处理有关自行车共享服务(https://www.bixi.com/en/open-data)的公开数据。我有一个名为“Bixi17”的csv文件,其中包含开始日期,结束日期和自行车站代码。使用R Studio和Shinyapp,我正在尝试创建一个用户输入日期的应用程序,应用程序显示直方图(x轴=站点代码,y轴= n),但不起作用。我想弄清楚问题是什么。我是编程新手。

这是我的代码。

library(shiny)
load(file = "mydataset.Rdata")

ui <- fluidPage(
  titlePanel("Analyzing Bixi"),
  dateRangeInput("dates", label = h3("Date range"), start = NULL, end = NULL, min = 04-01, max = 09-30, format = "yyyy-mm-dd"),

  hr(),
  fluidRow(column(4, verbatimTextOutput("value"))),


  plotOutput(outputId = "distPlot"))




server <- function(input, output) {
  output$value <- renderPrint({ input$dates })

  output$distPlot <- renderPlot({
    a <- as.Date(input$dates)
    x <- as.numeric(Bixi17$start_date[a])
    hist(x)
  })


}


shinyApp(ui = ui, server = server)

这是“Bixi17”的总结。

> summary(Bixi17)
            start_date      start_station_code             end_date      
 2017-05-28 16:30:     95   Min.   : 5002      2017-05-23 17:23:     94  
 2017-07-18 17:10:     95   1st Qu.: 6105      2017-09-27 17:36:     94  
 2017-07-05 17:08:     94   Median : 6203      2017-08-02 17:34:     93  
 2017-08-08 17:10:     93   Mean   : 6325      2017-08-02 17:45:     91  
 2017-05-23 17:05:     92   3rd Qu.: 6389      2017-07-06 17:34:     90  
 2017-08-08 17:07:     92   Max.   :10002      2017-07-18 17:27:     89  
 (Other)         :4018161                      (Other)         :4018171  
 end_station_code    duration_sec      is_member          Start           
 Length:4018722     Min.   :  61.0   Min.   :0.0000   Min.   :2017-04-15  
 Class :character   1st Qu.: 382.0   1st Qu.:1.0000   1st Qu.:2017-06-10  
 Mode  :character   Median : 670.0   Median :1.0000   Median :2017-07-18  
                    Mean   : 837.5   Mean   :0.7993   Mean   :2017-07-15  
                    3rd Qu.:1121.0   3rd Qu.:1.0000   3rd Qu.:2017-08-23  
                    Max.   :7199.0   Max.   :1.0000   Max.   :2017-09-30  

1 个答案:

答案 0 :(得分:0)

我进入了链接并下载了一个示例CSV文件来运行您的代码。

尝试以下代码。

希望这有帮助。

# Convert your 'start_date' column in date format
Bixi17$start_date <- as.Date(Bixi17$start_date, format = "%m/%d/%Y %H:%M")

# Build a UI of the Shiny app
ui <- fluidPage(
  titlePanel("Analyzing Bixi"),
  # Define the start and end date on the date range input in correct string format
  dateRangeInput("dates", label = h3("Date range"), start = '2017-09-01', end = '2017-09-30', min = '2017-04-01', max = '2017-09-30', format = "yyyy-mm-dd"),
  hr(),
  fluidRow(column(4, verbatimTextOutput("value"))),
  plotOutput(outputId = "distPlot"))

# Build a server of the Shiny app
server <- function(input, output) {
  output$value <- renderPrint({ input$dates })
  output$distPlot <- renderPlot({
    a <- as.Date(input$dates)
    # Find the row indices within the selected date range
    selectedInd <- a[1] <= myData$start_date & a[2] >= myData$start_date
    # Use the row indices to filter out the station codes
    x <- as.numeric(myData$start_station_code[selectedInd])
    # as.numeric above is not necessary if the start_station_code column is already in integer format
    hist(x)
  })
}

shinyApp(ui = ui, server = server)