我有一個Shiny
Web應用程序,我允許用戶上傳他們的文件。我正在執行幾行數據操作,以便更容易地繪製在我的Dygraph
輸出上。替換R中的列與另一列長度不同
在我的代碼,我需要刪除與每個文件(每個文件都有一個時間列)上傳的時間列,然後我保存爲.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')
))
))))
爲什麼你能不能從上傳的數據格式化時間列? – Heroka
我還沒有很好的操作R中的時間列。似乎工作的唯一方法是在Excel中將其格式化爲'm/d/y hh:mm:ss',然後使用'as。否則,我得到一個錯誤,它無法計算1次觀察的週期性。 – Gary
我不知道數據是什麼樣子,但對我來說,嘗試格式化時間列似乎更好。因爲你怎麼知道他們匹配? – Heroka