簡體   English   中英

如何讀取 Shiny 中的 csv 文件?

[英]How to read a csv file in Shiny?

我正在嘗試創建一個 shiny 儀表板,允許用戶 select 一個 csv 文件。 該文件僅包含訂單號和創建日期兩列。 此外,我希望用戶能夠 select 他們想要的日期范圍並獲得匯總計數統計信息。

到目前為止,我的代碼如下:

library(shiny)
library(plotly)
library(colourpicker)
library(ggplot2)


ui <- fluidPage(
  titlePanel("Case Referrals"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Select a file"),
      sliderInput("period", "Time period observed:",
                  min(data()[, c('dateCreated')]), max(data()[, c('dateCreated')]),
                  value = c(min(data[, c('dateCreated')]),max(data()[, c('dateCreated')])))
    ),
    mainPanel(
      DT::dataTableOutput("table")
    )
  )
)

# Define the server logic
server <- function(input, output) {
  
  # file input
  input_file <- reactive({
    if (is.null(input$file)) {
      return("")
    }
  })
  
  
  # summarizing data into counts
  data <- input_file()
  data <- subset(data, dateCreated >= input$period[1] & dateCreated <= input$period[2])


  output$table <- DT::renderDataTable({
    data
  })
  
  
  
}

shinyApp(ui = ui, server = server)

我收到一條錯誤消息:

Error in data()[, c("dateCreated")]: incorrect number of dimensions

誰能幫我理解問題可能是什么和/或提供一個更好的框架來做到這一點? 為了在 csv 文件中明確,createDate 變量被分解為下訂單時的各個日期。

謝謝!

我在錯誤的步驟中添加了注釋。

library(shiny)


ui <- fluidPage(
  titlePanel("Case Referrals"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Select a file"),
      
      # you cannot call data() in your ui. 
      # You would have to wrap this in renderUI inside of your server and use
      # uiOutput here in the ui
      sliderInput("period", "Time period observed:", min = 1, max = 10, value = 5)
    ),
    mainPanel(
      DT::dataTableOutput("table")
    )
  )
)

# Define the server logic
server <- function(input, output) {

  input_file <- reactive({
    if (is.null(input$file)) {
      return("")
    }

    # actually read the file
    read.csv(file = input$file$datapath)
  })

  output$table <- DT::renderDataTable({

    # render only if there is data available
    req(input_file())

    # reactives are only callable inside an reactive context like render
    data <- input_file()
    data <- subset(data, dateCreated >= input$period[1] & dateCreated <= input$period[2])

    data
  })



}

shinyApp(ui = ui, server = server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM