Subscript out of bound [Please HELP]

shiny

#1

I think I didn’t explain my problem clearly, so no one can can give me an advice on this problem. Sorry for not explaining you clearly. I’m trying to format the output file from Vissim simulation to csv file. So I have 2 Fileinputs;
•FileInput 1 is for formatting the FZP file to CSV file, it can be uploaded multiple file but for now it can only upload 2 at max, but later on I would like the FileInput 1 can process more than 2 files.
•FileInput 2 is for merging the CSV file after converting the FZP file, because 1 run of VISSIM simulation can be multiple FZP files, so if I want to merge 2 runs or more I would like to use the FileInput 2 for merging it.

And my problem is in FileInput 1 because when I upload 2 files FZP it’s not causing an error, but when I only upload 1 file FZP it cause an error ‘Subscript Out of Bound’ even I already made a condition where the FileInput 1 in index 2 is Null then create new dataframe. Because, I’m trying to access individual file from the multiple uploaded file for calculating average of attributes in the FZP file by attribute called ‘VEHTYPE’. . So, how would I solve this? thank you in advance

UI:

#UI
library(shiny)
library(data.table)

shinyUI(fluidPage(
  titlePanel("File Input"),
  sidebarLayout(
    sidebarPanel(
      tags$hr(),
      fileInput("file1",
                "Choose FZP files",
                multiple = TRUE),
      fileInput("file2",
                label="Upload multiple CSVs here",
                multiple = TRUE),
      uiOutput("column_1"),
      downloadButton("download", "Filter Table"),
      downloadButton("download1", "Aggregate Table")
    ),
    mainPanel(
      uiOutput("tb")
    ))
))

Server:

#Server
library(shiny)
library(dplyr)
library(plyr)
library(data.table)

shinyServer(function(input, output) {
  
  data1 <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) {
      return(NULL)
    } else {
      inFile %>%
        rowwise() %>%
        do({
          df <- fread(input$file1[[1, 'datapath']], skip="t;", sep = ";", header=T, stringsAsFactor = F)
        })
    }
  })
  
  data2 <- reactive({
    inFile <- input$file1
    if (is.null(input$file1[[2, "datapath"]])) {
      subData <- data1()
      df <- subData[0,]
      df
      #data1()
    } else {
      inFile %>%
        rowwise() %>%
        do({
          df <- fread(input$file1[[2, 'datapath']], skip="t;", sep = ";", header=T, stringsAsFactor = F)
        })
    }
  })
  
  output$column_1 <- renderUI({
    if (is.null(data1())) {
      return(NULL)
    } else {
      selectInput("column1", "Feature selection:", names(data1()))
    }
  })
  
  average1 <- reactive({
    #Processing data1
    subsetData1 <- data1()
    calAvg1 <- subsetData1[, c("VEHTYPE",input$column1)] 
    calAvg1 <- aggregate(calAvg1[, ncol(calAvg1)], list(VEHTYPE = calAvg1$VEHTYPE), mean)
    total <- sum(calAvg1[, ncol(calAvg1)])/length(calAvg1$VEHTYPE)
    dfTotal1 <- data.frame("Total", total)
    names(dfTotal1) <- c("VEHTYPE", input$column1)
    newDF1 <- rbind(calAvg1, dfTotal1)
  })
  
  average2 <- reactive({
    #Processing data2
      subsetData2 <- data2()
      if(is.data.frame(subsetData2) && nrow(subsetData2)==0){
        subsetData2
      }else{
        calAvg2 <- subsetData2[, c("VEHTYPE",input$column1)] 
        calAvg2 <- aggregate(calAvg2[, ncol(calAvg2)], list(VEHTYPE = calAvg2$VEHTYPE), mean)
        total <- sum(calAvg2[, ncol(calAvg2)])/length(calAvg2$VEHTYPE)
        dfTotal2 <- data.frame("Total", total)
        names(dfTotal2) <- c("VEHTYPE", input$column1)
        newDF2 <- rbind(calAvg2, dfTotal2)
      }
  })
  
  finalDF <- reactive({
    
    if(is.data.frame(average2()) && nrow(average2())==0){
      average1()
    }else{
      final <- rbind(average1(), average2())
      #average2()
    }
    #Merge all dataframe
    #finalDF <- rbind(average1(), average2())
  })
  
  data3 <- reactive({ 
    req(input$file2) ## ?req #  require that the input is available
    
    multiFile <- input$file2
    
    df <- rbindlist(lapply(multiFile$datapath, fread),
                    use.names = TRUE, fill = TRUE)
    as.data.frame(df)
    return(df)
  })
  
  output$original <- renderDataTable({
    data1()
  })
  
  output$filterData <- renderTable({
    finalDF()
  })
  
  output$multiData <- renderTable({
    data3()
  })
  
  output$download <- downloadHandler(
    filename = function(){
      paste("data-", Sys.Date(), ".csv", sep = "")
    },
    content = function(file){
      write.csv(get_table(), file,row.names = F)
    }
  )
  
  output$download1 <- downloadHandler(
    filename = function(){
      paste("data-", Sys.Date(), ".csv", sep = "")
    },
    content = function(file){
      write.csv(data3(), file,row.names = F)
    }
  )
  
  output$tb <- renderUI({
    tabsetPanel(tabPanel("Original Data", dataTableOutput("original")),
                tabPanel("Filter Data", tableOutput("filterData")),
                tabPanel("Aggregate Data", tableOutput("multiData"))
    )
  })
})

FZP files look more or less like this:

$VISION
* File: Y:\03_Studentische Arbeiten\VT\IDP\Windu\Test Simulation\test1.inpx
* Comment: 
* Date: 03.08.2017 09:32:43
* PTV Vissim: 9.00 [04]
* 
* Table: Vehicles In Network
* 
* SIMSEC: SimSec, Simulation second (Simulation time [s]) [s]
* NO: No, Number (Number of the vehicle)
* LANE\LINK\NO: Lane\Link\No, Lane\Link\Number (Unique number of the link or connector)
* LANE\INDEX: Lane\Index, Lane\Index (Unique number of the lane)
* POS: Pos, Position (Distance on the link from the beginning of the link or connector) [m]
* POSLAT: PosLat, Position (lateral) (Lateral position at the end of the time step. Value range 0 - 1:  0: at the right lane edge  0.5: middle of the lane  1: at the left lane edge)
* EMISSIONSCO2: EmissionsCO2, Emissions CO2 (Quantity of carbon monoxide [grams / sec])
* EMISSIONSNOX: EmissionsNOx, Emissions NOx (Quantity of nitrogen oxides [grams / sec])
* FUELCONSUMPTION: FuelConsumption, Fuel consumption (Fuel consumption [US liquid gallon])
* INQUEUE: InQueue, In queue (Returns if the vehicle is in queue. Queue is defined by speed and headway treshholds. (see queue definition))
* NUMSTOPS: NumStops, Number of stops (Number of stops (cumulative): all situations in which a vehicle comes to a standstill (speed = 0), except stops at PT stops and in parking lots)
* SPEED: Speed, Speed (Speed at the end of the time step) [km/h]
* VEHTYPE: VehType, Vehicle type (Select Vehicle type from the list box)
* 
* SimSec; No; Lane\Link\No; Lane\Index; Pos; PosLat; EmissionsCO2; EmissionsNOx; FuelConsumption; InQueue; NumStops; Speed; VehType
*
$VEHICLE:SIMSEC;NO;LANE\LINK\NO;LANE\INDEX;POS;POSLAT;EMISSIONSCO2;EMISSIONSNOX;FUELCONSUMPTION;INQUEUE;NUMSTOPS;SPEED;VEHTYPE
0.50;1;1;1;0.80;0.50;;;;0;0;41.70;100
0.60;1;1;1;1.96;0.50;;;;0;0;41.90;100
0.70;1;1;1;3.13;0.50;;;;0;0;42.16;100
0.80;1;1;1;4.31;0.50;;;;0;0;42.50;100
0.90;1;1;1;5.49;0.50;;;;0;0;42.91;100
1.00;1;1;1;6.69;0.50;;;;0;0;43.39;100
1.10;1;1;1;7.90;0.50;;;;0;0;43.92;100
1.20;1;1;1;9.13;0.50;;;;0;0;44.44;100
1.30;1;1;1;10.37;0.50;;;;0;0;44.96;100
1.40;1;1;1;11.63;0.50;;;;0;0;45.48;100
1.50;1;1;1;12.90;0.50;;;;0;0;45.99;100
1.60;1;1;1;14.18;0.50;;;;0;0;46.49;100
1.70;1;1;1;15.48;0.50;;;;0;0;47.00;100
1.80;1;1;1;16.79;0.50;;;;0;0;47.50;100

Error:

Warning: Error in [[: subscript out of bounds
Stack trace (innermost first):
    118: [[.data.frame
    117: [[ 
    116: <reactive:data2> 
    105: data2
    104: <reactive:average2> 
     93: average2
     92: is.data.frame
     91: <reactive:finalDF>
     80: finalDF
     79: renderTable 
     78: func
     77: origRenderFunc
     76: output$filterData
      1: runApp

#2

Hi, it looks like you're trying to access the file contents through the name of the file input in the call fread(input$file1...). Instead, you should use inFile$datapath to access the uploaded files.

inFile$datapath is a character vector. If one file is uploaded, it will be a character of length one, and the only element will be the path to the uploaded file. If multiple files are uploaded, the length will be > 1.