我要求用户上传一个CSV文件,然后让他们有机会选择一个"频道"它基于CSV文件的其中一个标题。当他们选择要图表的文件时,它将显示在Dygraph
输出中。
我遇到了一个我收到错误的问题:
Error in uploadedFile1$Time : $ operator is invalid for atomic vectors
有没有人对如何在我的脚本中修复此问题有任何想法?
此示例的数据如下: https://drive.google.com/file/d/0Bzg7bDcyZobbc1Zoc0VoN3ZzSmM/view?usp=sharing
这是我的ui.R
shinyUI(fluidPage(
navbarPage("Data Webpage",
tabPanel("Data Selection",
tabPanel("Upload a CSV File:",
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
selectInput("uploadChannels1", label = "Choose a Channel",
choices = NULL),
fileInput('file2', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
selectInput("uploadChannels2", label = "Choose a Channel",
choices = NULL),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
',')
),
mainPanel(
dygraphOutput('graph')
))
)
)))
这是我的Server.R
library(xts)
library(shiny)
library(dygraphs)
shinyServer(function(input, output, session) {
observeEvent(uploadedFile1, {
updateSelectizeInput(session, 'uploadChannels1', choices = names(uploadedFile1))
})
observeEvent(uploadedFile2, {
updateSelectizeInput(session, 'uploadChannels2', choices = names(uploadedFile2))
})
# 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)
uf1 <- strftime(strptime(uf1$Time, "%Y-%m-%d %H:%M:%S"), "%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)
uf2 <- strftime(strptime(uf2$Time, "%Y-%m-%d %H:%M:%S"), "%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()
})
})