【问题标题】:How to read a csv file in Shiny?如何在 Shiny 中读取 csv 文件?
【发布时间】:2022-01-06 16:46:54
【问题描述】:

我正在尝试创建一个闪亮的仪表板,允许用户选择一个 csv 文件。该文件仅包含订单号和创建日期两列。此外,我希望用户能够选择他们想要的日期范围并获得汇总计数统计信息。

到目前为止,我的代码如下:

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 变量被分解为下订单时的各个日期。

谢谢!

【问题讨论】:

  • 您能分享一个您要上传的 .csv 示例吗?

标签: r file-upload shiny shiny-reactivity


【解决方案1】:

我在错误步骤中添加了 cmets。

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)

【讨论】:

  • 谢谢。这有助于解决我的问题。有没有办法将滑块更改为日期格式?
  • 如果是,请接受。 Fpr 日期输入见here
  • 抱歉,忘了问...如何获取所选日期范围的汇总计数?我试过了:data &lt;- data %&gt;% group_by(dateCreated) %&gt;% summarise(case_count = n()) 但它不起作用。
  • 请针对不同的问题提出不同的问题
  • 这是我最初问题的一部分。我需要为用户选择的数据范围提供计数统计。
猜你喜欢
  • 2014-09-15
  • 2021-03-11
  • 2021-12-14
  • 2016-06-25
  • 2017-06-06
  • 2018-12-14
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多