遇到Dygraph输出的Time Objects问题

时间:2016-01-18 19:05:42

标签: r excel csv

我正在为一个Shiny App编写一个脚本,允许我的用户上传他们自己的CSV文件(任何人上传的所有文件都与这里的样本数据集类似:https://drive.google.com/open?id=0Bzg7bDcyZobbc1Zoc0VoN3ZzSmM)。然后,Shiny应用程序应允许用户使用SelectInput选择其中一个标题,然后在Dygraph输出中将其绘制成图形。我遇到的问题是将Time对象转换为适当格式以创建Dygraph输出的正确方法。

现在,我收到错误:

e

问题是,当我手动将时间列在Excel中修改为格式为Error in lapply(X = x, FUN = "[", ..., drop = drop) : argument is missing, with no default 时,它可以正常工作。在上传之前,我不想让我的用户在Excel中修改.csv。

server.R

h:mm:ss

ui.R

library(xts)
library(shiny)
library(dygraphs)

shinyServer(function(input, output, session) {
# Upload the CSV File 
  uploadedFile1 <- reactive({
    validate(need(input$file1, FALSE)) # This is like a better "if (is.null(input$file1)) return(NULL)"
    uf1 <- read.csv(input$file1$datapath, header=TRUE, stringsAsFactors=FALSE)
    uf2$Time <- strptime(uf2$Time, format= "%H:%M:%S")
  })

  uploadedFile2 <- reactive({
    validate(need(input$file2, FALSE)) # This is like a better "if (is.null(input$file2)) return(NULL)"
    uf2 <- read.csv(input$file2$datapath, stringsAsFactors=F)
    uf2$Time <- strptime(uf2$Time, format= "%H:%M:%S")

  })

  observeEvent(uploadedFile1(), {
    updateSelectizeInput(session, 'uploadChannels1', choices = names(uploadedFile1()))
  })
  observeEvent(uploadedFile2(), {
    updateSelectizeInput(session, 'uploadChannels2', choices = names(uploadedFile2()))
  })

  output$graph <- renderDygraph({


    # Clean up the loaded CSV File, convert Time column to a Time Object for Dygraph.
    uploadedFile1 <- uploadedFile1()
    uploadedFile2 <- uploadedFile2()
    uploadedFile1$Time <- as.POSIXct(strptime(uploadedFile1$Time,"%H:%M:%S"))
    uploadedFile2$Time <- as.POSIXct(strptime(uploadedFile2$Time,"%H:%M:%S"))

    uploadedFile1$ctime <- strptime(paste(uploadedFile1$Time), "%Y-%m-%d %H:%M:%S")
    uploadedFile2$ctime <- strptime(paste(uploadedFile2$Time), "%Y-%m-%d %H:%M:%S")

    # Update the SelectInput and store the value in component5 to be used in the graph.
    selectedInput1 <- input$uploadChannels1
    selectedInput2 <- input$uploadChannels2
    component5 <- uploadedFile1[, selectedInput1]
    component6 <- uploadedFile2[, selectedInput2]
    cbinded <- cbind(component5, component6)

        xts(cbinded, uploadedFile1$Time, uploadedFile2$Time)  %>% 
      dygraph()

    })

})

0 个答案:

没有答案