2015-12-16 161 views
0

我有一個Shiny Web應用程序,我允許用戶上傳他們的文件。我正在執行幾行數據操作,以便更容易地繪製在我的Dygraph輸出上。替換R中的列與另一列長度不同

My Data

在我的代碼,我需要刪除與每個文件(每個文件都有一個時間列)上傳的時間列,然後我保存爲.csv文件時間列更換(稱爲time)。我收到Error: replacement has 3001 rows, data has 2990的錯誤。我應該如何解決這個問題?我的時間列在Excel中專門格式化以與Dygraph輸出一起使用,這就是爲什麼我用我的工作區中加載的這個(稱爲time)替換所有上傳的時間列的原因。

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")) 
    }) 

修訂 - 我修改了項目XTS內,但每當我選擇一個頻道,它總是回覆到掃描通道。如果我選擇「瓦特」,瓦數圖表會短暫彈出,直到它快速恢復到掃描圖表爲止。

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') 
         )) 



         )))) 
+0

爲什麼你能不能從上傳的數據格式化時間列? – Heroka

+0

我還沒有很好的操作R中的時間列。似乎工作的唯一方法是在Excel中將其格式化爲'm/d/y hh:mm:ss',然後使用'as。否則,我得到一個錯誤,它無法計算1次觀察的週期性。 – Gary

+0

我不知道數據是什麼樣子,但對我來說,嘗試格式化時間列似乎更好。因爲你怎麼知道他們匹配? – Heroka

回答

1

這是我服用。我沒有你的用戶,所以我做了一個。它的簡化,但它的工作,但我不知道這是否是你想要的輸出。我的道歉,如果我完全錯了:-)

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")) 
)) 

這裏是截圖 enter image description here

+0

爲了讓我們更容易,你可以添加與server.R文件一起使用的ui.R! – MLavoie

+0

更新!我目前的問題是,當我選擇一個特定的頻道時,它會立即恢復到掃描頻道。如果我點擊Selectinput中的'Watts',我會看到瓦數圖表一會兒,然後它恢復到掃描圖表。 – Gary

+0

是否有可能ui.R中缺少某些內容?它是拋出我的錯誤 – MLavoie

相關問題