繁体   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