用另一列不同长度的列替换R中的列

时间:2015-12-16 20:16:28

标签: r shiny

我有一个Shiny网络应用程序,我允许用户上传他们的文件。我正在执行几行数据操作,以便更容易在我的Dygraph输出上进行绘图。

My Data

在我的代码中,我需要删除随每个文件上传的Time列(每个文件都有一个Time列),然后将其替换为我保存为.csv文件的时间列(称为time文件Error: replacement has 3001 rows, data has 2990)。

我收到Dygraph的错误。我该如何解决这个问题?我的时间列在Excel中经过特殊格式化以使用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")) }) )替换的原因。

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

})

修订版 - 我修改了xts中的项目,但每当我选择一个频道时,它总是会恢复为扫描频道。如果我选择&#34; Watts&#34;瓦数图表会短暂弹出,直到它快速恢复到扫描图表。

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



                        ))))

ui.R

$scope.removeFavoriteRecipe = function(favorite){
    var deleteIt = confirm("Are you sure you want to remove this recipe from your favorites?");
    if(deleteIt){
        console.log(favorite._id)
        $http.delete("/removeFavoriteRecipe/" + favorite._id)
        .then(function(returnData){
            if(returnData.data.err){
                $scope.err = returnData.data.err;
            }else{
                $scope.err = "";
                $window.location.reload();
            }
        })
    }
}

1 个答案:

答案 0 :(得分:1)

这是我的看法。我没有你的ui.R所以我做了一个。它已经简化但是有效,但我不确定这是否是您想要的输出。如果我完全错了,我道歉: - )

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