Shiny File Output Error: Warning: Error in [<-.data.frame: new columns would leave holes after existing columns [No stack trace available]

Viewed 28

For some reason I am having trouble outputting the data frames I have made in R to CSVs using the fwrite within shiny application. I was able to figure out the first three outputs, "Results", and "Manifest". I can't catch my error for the last three outputs, "MA_Reporting", "RI_Reporting", and "CT_Reporting".

ui.R:

library(shiny)

ui <- fluidPage(
  titlePanel(strong(h1("Data Transformation App", align = "center"))),
  fileInput(inputId = "Sample_Call", label = h3("Upload Sample Call", aline = "center")),
  downloadButton(outputId = 'downloadData', 'Download Reporting CSV'),
  fileInput(inputId = "Results", label = h3("Upload Results", align = "center")),
  downloadButton(outputId = 'downloadData1', 'Download Manifest Update'),
  fileInput(inputId = "Export_Results", label = h3("Upload Export", align = "center")),
  downloadButton(outputId = 'MA_Reporting', 'Download Massachusetts State Reporting CSV'),
  downloadButton(outputId = 'RI_Reporting', 'Download Rhode Island State Reporting CSV'),
  downloadButton(outputId = 'CT_Reporting', 'Download Connecticutt State Reporting CSV')
)

server.R:

library(data.table)
server <- function(input, output)
{
  
  getSample_Call <- reactive({
    
    inFile <- input$Sample_Call
    
    if (is.null(input$Sample_Call))
      return(NULL)
    
    temp1 <- fread(inFile$datapath)
    #######################
    # Transformations in originalData object
    temp2 <- as.character(temp1$Sample)
    temp2 <- strsplit(temp2, split = "_")
    temp2 <- as.data.frame(temp2)
    temp2 <- as.data.frame(t(temp2))
    today <- Sys.Date()
    today <- format(today, format = "%Y-%m-%d")
    temp2[,3] <- c(today)
    temp2[,4] <- temp1$Assessment
    labels <- c("GExID",    "Serial No.",   "Testing Date", "Result")
    colnames(temp2) <- labels
    controls <- which(as.numeric(as.character(temp2$`Serial No.`)) < 100)
    temp2<- temp2[-controls,]
    rownames(temp2) <- NULL
    file <- temp2
    ##############################
    
  })
  
  
  output$downloadData <- downloadHandler(filename = function() 
  { 
    paste("Results_", Sys.Date(), ".csv", sep="")
  },
  
  content = function(file) {
    
    fwrite(getSample_Call(), file)
    
  })
  
  getResults <- reactive({
    
    inFile <- input$Results
    
    if (is.null(input$Results))
      return(NULL)
    
    temp1 <- fread(inFile$datapath)
    #######################
    # Transformations in originalData object
    ID <- as.character(temp1$Sample)
    save <- unique(ID)
    temp <- strsplit(save, split = "_")
    temp <- as.data.frame(temp)
    temp <- as.data.frame(t(temp))
    temp[,3] <- save
    today <- Sys.Date()
    today <- format(today, format = "%Y-%m-%d")
    temp[,4] <- c(today)
    temp[,5:11] <- c("")
    temp[,12] <- temp1[(which(temp1$Target == "N1 (FAM)")),13]
    temp[,13] <-c("")
    temp[,14] <- temp1[(which(temp1$Target == "RP (ATTO)")),13]
    temp[,15] <- as.character(lapply(temp$Cq, function(x) if(x < 27){
      c("+")
    }
    else
      c("-")))
    temp[,16] <-c("")
    temp[,17] <- as.character(lapply(temp$Cq.1, function(x) if(x < 27){
      c("+")
    }
    else
      c("-")))
    temp[,18] <- as.character(lapply(temp$Cq, function(x) if(x < 27){
      c("Positive")
    }
    else
      c("Negative")))
    
    colnames(temp) <- NULL
    rownames(temp) <- NULL
    controls <- which(as.numeric(as.character(temp[,2])) < 11)
    temp <- temp[-controls,]
    if (length(which(substr(temp[,1],1,1) == "E")) > 0)
    {
      pools <- which(substr(temp[,1],1,1) == "E")
      temp <- temp[-pools,]
    }
    file1 <- temp
    ##############################
  })
  
  
  output$downloadData1 <- downloadHandler(
    
    filename = function() { 
      paste("Manifest_", Sys.Date(), ".csv", sep="")
    },
    
    content = function(file1) {
      
      fwrite(getResults(), file1)
      
    })
  
  getExport_Results <- reactive({
    
    inFile <- input$Export_Results
    
    if (is.null(input$Export_Results))
      return(NULL)
    
    temp1 <- fread(inFile$datapath)
    #######################
    # Transformations in originalData object
    length <- nrow(temp1)
    temp1_vector <- c("")
    temp1_vector <- temp1_vector[-0]
    i <- 0
    for (i in 1:length) {
      if (temp1[i,14] == "Saliva")
      {
        temp1_vector <- paste(temp1_vector, "SalivaDirect")
      }
      if (temp1[i,14] == "NP Swab")
      {
        temp1_vector <- paste(temp1_vector, "OneCovid")
        
      }
      i <- i + 1
    } 
    temp1_vector <- strsplit(temp1_vector, split = " ")
    temp1_vector <- unlist(temp1_vector)
    temp1_vector <- temp1_vector[-1]
    temp1[,16] <- temp1_vector
    #MASSACHUSETTS
    temp_MA <- temp1[which(temp1$STATE == "MA"),]
    temp_MA$Middle_Name <- c('')
    temp_MA$Race <- c('')
    
    #RHODE ISLAND
    temp_RI <- temp1[which(temp1$STATE == "RI"),]
    temp2_RI <- matrix('', ncol = 25, nrow = nrow(temp_RI))
    temp2_RI <- as.data.frame(temp2_RI)
    temp2_RI$V1 <- temp_RI$Specimen_Number
    temp2_RI$V2 <- temp_RI$Last_Name
    temp2_RI$V3 <- temp_RI$First_Name
    temp2_RI$V4 <- temp_RI$Middle_Name
    temp2_RI$V5 <- temp_RI$DOB
    temp2_RI$V6 <- temp_RI$Sex
    temp2_RI$V7 <- temp_RI$`Ethnicity (Hispanic/non-Hispanic)`
    temp2_RI$V8 <- temp_RI$Race
    temp2_RI$V9 <-c('')
    temp2_RI$V10 <- temp_RI$ADDRESS
    temp2_RI$V11 <- c('')
    temp2_RI$V12 <- temp_RI$CITY
    temp2_RI$V13 <- temp_RI$STATE
    temp2_RI$V14 <- temp_RI$ZIP
    temp2_RI$V15 <- temp_RI$Patient_Phone
    temp2_RI$V16 <- temp_RI$Specimen_Source
    temp2_RI$V17 <- temp_RI$Specimen_Date
    temp2_RI$V18 <- temp_RI$Result_Date
    temp2_RI$V19 <- temp_RI$Result
    temp2_RI$V20 <- temp_RI$Test
    temp2_RI$V21 <- c("Genomic Expression")
    temp2_RI$V22 <- c("100 Cummings Center, Suite 451C")
    temp2_RI$V23 <- c("Beverly")
    temp2_RI$V24 <- c("MA")
    temp2_RI$V25 <- c("01915")
    colnames(temp2_RI) <- c("ID", "Last_Name", "First_Name",    "Middle_Initial",   "DOB",  "Sex",  "Race",
                            "Race_Other",   "Ethnicity",    "Street",   "Unit", "City", "State",    "Zip",
                            "HOME_PHONE",   "Test_Name",    "Test_Date",    "Result_Date",  "Result",
                            "PCP_Name", "Test_Site_Name",   "Test_Site_Address",    "Test_Site_City",
                            "Test_Site_State",  "Test_Site_Zip")
    temp2_RI <- as.data.frame(temp2_RI)
    #CONNECTICUT
    temp_CT <- temp1[which(temp1$STATE == "CT"),]
    temp2_CT <- matrix('', ncol = 47, nrow = nrow(temp_CT))
    temp2_CT <- as.data.frame(temp2_CT)
    colnames(temp2_CT) <- c("Testing Location", "Patient Last Name",    "Patient First Name",
                            "Patient Middle Initial",   "Patient Address",  "Address 2",    "Patient City",
                            "Patient State",    "Patient Zip Code", "Patient County",   "Patient Phone",
                            "Patient Email",    "Date of Birth",    "Patient Gender",   "Patient Race",
                            "Patient Ethnicity",    "Patient Occupation", "Patient MRN",    "Ordering Lab",
                            "Ordering Facility",    "Ordering Provider Last Name",  
                            "Ordering Provider First Name", "Ordering Provider NPI",    
                            "Ordering Provider Phone number",   "Ordering Provider Address",
                            "Ordering Provider City",   "Ordering Provider State",  
                            "Ordering Provider Zipcode",    "Specimen ID",  "Specimen Source",
                            "Test Method",  "Result",   "Result value", "Result units", 
                            "Specimen Collection Date", "Specimen Received Date",   "Date Tested",
                            "Date Reported",    "Device Identifier",    "Works in Healthcare Setting?",
                            "Patient has COVID-19 Symptoms?",   "Date of onset of symptoms/illness",
                            "Is patient pregnant? (if female)", "Reside in congregate setting?",
                            "First test for COVID-19?", "Hospitalized?",    "Admitted to ICU?")
    temp2_CT$`Testing Location` <- c("05D999999")
    temp2_CT$`Patient Last Name` <- temp_CT$Last_Name
    temp2_CT$`Patient First Name` <- temp_CT$First_Name
    temp2_CT$`Patient Middle Initial` <- temp_CT$Middle_Name
    temp2_CT$`Patient Address` <- temp_CT$ADDRESS
    temp2_CT$`Address 2` <- c('')
    temp2_CT$`Patient City` <- temp_CT$CITY
    temp2_CT$`Patient State` <- temp_CT$STATE
    temp2_CT$`Patient Zip Code` <- temp_CT$ZIP
    temp2_CT$`Patient County` <- c('')
    temp2_CT$`Patient Phone` <- temp_CT$Patient_Phone
    temp2_CT$`Patient Email` <- c('')
    temp2_CT$`Date of Birth` <- temp_CT$DOB
    temp2_CT$`Patient Gender` <- temp_CT$Sex
    temp2_CT$`Patient Race` <- temp_CT$Race
    temp2_CT$`Patient Ethnicity` <- temp_CT$`Ethnicity (Hispanic/non-Hispanic)`
    temp2_CT$`Patient Occupation` <- c('')
    temp2_CT$`Patient MRN` <- c('')
    temp2_CT$`Ordering Lab` <- temp_CT$Ordering_Provider
    temp2_CT$`Ordering Facility` <- temp_CT$Ordering_Facility
    temp2_CT$`Ordering Provider Last Name` <- c('')
    temp2_CT$`Ordering Provider First Name` <- c('')
    temp2_CT$`Ordering Provider NPI` <- c('')
    temp2_CT$`Ordering Provider Phone number` <- temp_CT$Ordering_Provider_Phone
    temp2_CT$`Ordering Provider Address` <- temp_CT$Ordering_Provider_Address
    temp2_CT$`Ordering Provider City` <- temp_CT$Ordering_Provider_City
    temp2_CT$`Ordering Provider State` <- temp_CT$Ordering_Provider_State
    temp2_CT$`Ordering Provider Zipcode` <- temp_CT$Ordering_Provider_Zip
    temp2_CT$`Specimen ID` <- temp_CT$Specimen_Number
    temp2_CT$`Specimen Source` <- temp_CT$Specimen_Source
    temp2_CT$`Test Method` <- c("94500-6")
    temp2_CT$`Result value` <- temp_CT$Result
    length2 <- nrow(temp_CT)
    i <- 1
    for (i in 1:length2)
    {
      if (temp_CT[i,18] == "positive")
      {
        temp2_CT[i,32] <- c(10828004)
      }
      if (temp_CT[i,18] == "negative")
      {
        temp2_CT[i,32] <- c(260385009)
      }
      i <- i + 1
    }
    temp2_CT$`Result units` <- c('')
    temp2_CT$`Specimen Collection Date` <- temp_CT$Specimen_Date
    temp2_CT$`Specimen Received Date` <- temp_CT$Result_Date
    temp2_CT$`Date Tested` <- temp_CT$Result_Date
    temp2_CT$`Date Reported` <- temp_CT$Result_Date
    temp2_CT$`Device Identifier` <- c('')
    temp2_CT$`Works in Healthcare Setting?` <- c('')
    temp2_CT$`Patient has COVID-19 Symptoms?` <- c('')
    temp2_CT$`Date of onset of symptoms/illness` <- c('')
    temp2_CT$`Is patient pregnant? (if female)` <- c('')
    temp2_CT$`Reside in congregate setting?` <- c('')
    temp2_CT$`First test for COVID-19?` <- c('')
    temp2_CT$`Hospitalized?` <- c('')
    temp2_CT$`Admitted to ICU?` <- c('')
    ##############################
  })
  
  
  output$MA_Reporting <- downloadHandler(
    
    filename = function() { 
      paste("MA_Reporting_", Sys.Date(), ".csv", sep="")
    },
    
    content = function(temp_MA) {
      
      fwrite(getExport_Results(), temp_MA)
      
    })
  
  output$RI_Reporting <- downloadHandler(
    
    filename = function() { 
      paste("RI_Reporting_", Sys.Date(), ".csv", sep="")
    },
    
    content = function(temp2_RI) {
      
      fwrite(getExport_Results(), temp2_RI)
      
    })
  
  output$CT_Reporting <- downloadHandler(
    
    filename = function() { 
      paste("CT_Reporting_", Sys.Date(), ".csv", sep="")
    },
    
    content = function(temp2_CT) {
      
      fwrite(getExport_Results(), temp2_CT)
      
    })
}`

Any ideas on what the error might be?

0 Answers
Related