0

I have a Shiny web application that I am allowing users to upload their files in to. I am performing a couple lines of data manipulation to make it easier to plot on my Dygraph output.

My Data

In my code, I need to remove the Time column that is uploaded with each file (each file has a Time column), and then replace it with a time column that I saved as a .csv file (called time).

I receive the error that Error: replacement has 3001 rows, data has 2990. How do should I resolve this problem? My time column is specially formatted in Excel to work with the Dygraph output, which is why I replace all uploaded time columns with this (called time) loaded in my workspace.

  uploadedData <- reactive({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    uploadedFile <- read.csv(inFile$datapath, header=input$header, sep=input$sep, 
             quote=input$quote)
    uploadedFile[1]<- NULL
    uploadedFile$Time <- time
    uploadedFile$Time <- as.POSIXct(strptime(uploadedFile$Time,"%m/%d/%Y %H:%M:%S" ))
    uploadedFile <- xts(uploadedFile[,-1], order.by=uploadedFile[,1])
    })

  output$graph <- renderDygraph({
    uploadedFile <- uploadedData()
    updateSelectizeInput(session, 'uploadChannels', choices = names(uploadedFile))
    fileInput <- input$uploadChannesl
    component5 <- uploadedFile[, fileInput]
    dygraph(component5, main = "Temperature Rise Data Plot") %>%
      dyAxis("y", label = "Temp (F)") %>%
      dyAxis("x", label = "Time (min)")%>%
      dyRangeSelector() %>%
      dyOptions(colors = RColorBrewer::brewer.pal(8, "Dark2"))
  })

Revision - I revised the items inside of xts, but whenever I choose a channel, it always reverts back to the Scan channel. If I choose "Watts" the wattage chart briefly pops up until it quickly reverts back to the Scan chart.

  output$graph <- renderDygraph({
    uploadedFile <- input$file1

    if (is.null(uploadedFile))
      return(NULL)

    uploadedFile <- read.csv(uploadedFile$datapath, header=input$header, sep=input$sep, 
                             quote=input$quote)
    uploadedFile$Time <- as.POSIXct(strptime(uploadedFile$Time,"%H:%M:%S"))
    uploadedFile$ctime <- strptime(paste(uploadedFile$Time), "%Y-%m-%d %H:%M:%S")
    updateSelectizeInput(session, 'uploadChannels', choices = names(uploadedFile))
    fileInput <- input$uploadChannels
    component5 <- uploadedFile[, fileInput]
     xts(component5, uploadedFile$Time)  %>%
      dygraph()
     })

})

ui.R

shinyUI(fluidPage(
  navbarPage("Engineering Laboratory Data",         
 tabPanel("Upload your Own File:",
                      sidebarLayout(
                        sidebarPanel(
                          fileInput('file1', 'Choose CSV File',
                                    accept=c('text/csv', 
                                             'text/comma-separated-values,text/plain', 
                                             '.csv')),
                          tags$hr(),
                          checkboxInput('header', 'Header', TRUE),
                          radioButtons('sep', 'Separator',
                                       c(Comma=',',
                                         Semicolon=';',
                                         Tab='\t'),
                                       ','),
                          radioButtons('quote', 'Quote',
                                       c(None='',
                                         'Double Quote'='"',
                                         'Single Quote'="'"),
                                       '"'),
                          selectInput("uploadChannels", label = "Choose a Channel",
                                         choices = NULL)
                        ),
                        mainPanel(
                              dygraphOutput('graph')
                        ))



                        ))))
Gary
  • 2,137
  • 3
  • 23
  • 41
  • Why can you not format the time-columns from the uploaded data? – Heroka Dec 16 '15 at 20:32
  • I haven't had much luck with manipulating the time columns inside of R. The only thing that seemed to work was formatting it as `m/d/y hh:mm:ss` inside of Excel, and then using `as.POSIXct` inside of R. Otherwise, I am getting an error that it can not calculate the periodicity of 1 observation. – Gary Dec 16 '15 at 20:35
  • I don't know what the data looks like, but to me it seems better to try to format the time-columns right. Because how do you know they match? – Heroka Dec 16 '15 at 20:38
  • I added a link to my data above. – Gary Dec 16 '15 at 20:42
  • Links are not part of a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Dec 16 '15 at 20:42
  • I added this line: `as.POSIXct(uploadedFile$Time, format="%H:%M:%S")`, and I am told that order.by requires an appropriate time-based object. – Gary Dec 16 '15 at 21:09
  • what is the difference between the column Time and time? – MLavoie Dec 16 '15 at 22:43

1 Answers1

1

here is my take. I did not have your ui.R so I made one up. it's simplified but it's working but I am not sure if this is the output you want. My apologies if I am completely wrong :-)

server.R
library(shiny)
library(dygraphs)

shinyServer(function(input, output) {

  uploadedFile <- read.csv("/Users/drisk/Downloads/CSV_1.csv", header=TRUE, sep=",")
  uploadedFile$Time <- as.POSIXct(strptime(uploadedFile$Time,"%H:%M:%S"))
  uploadedFile$ctime <- strptime(paste(uploadedFile$Time), "%Y-%m-%d %H:%M:%S")

  output$graph <- renderDygraph({
xts(uploadedFile[,3], uploadedFile[,6])  %>%
      dygraph()
  })

})

ui.R

library(shiny)
library(dygraphs)

shinyUI(fluidPage(


    mainPanel(
      dygraphOutput("graph"))
))

and here is a screenshot enter image description here

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • to make it easier for us could you add the ui.R that goes with the server.R file! – MLavoie Dec 17 '15 at 16:16
  • Updated! My current issue, is when I choose a specific channel, it reverts immediately back to the Scan channel. If I click `Watts` in the Selectinput, I see the Wattage chart for a brief second before it reverts back to a chart of the Scan. – Gary Dec 17 '15 at 16:34
  • is it possible something is missing in the ui.R? it's throwing me errors – MLavoie Dec 17 '15 at 16:41
  • Does it work now? I added a few parentheses. The ui.R pasted is a smaller part of a larger ui.R code that I have. What error are you getting? – Gary Dec 17 '15 at 16:44
  • sorry but can't find a solution to your problem. I never used updateSelectizeInput before. I would have probably used selectInput() outside renderDygraph – MLavoie Dec 17 '15 at 17:17
  • No problem! You got me started on the right track! You helped me overcome a hurdle that I was using silly roundabout ways to solve. Thank you! – Gary Dec 17 '15 at 17:24