1

I made a function and I am getting some errors. However, these errors can be overlooked. Is there any function I can use to skip the errors and continue to process the function as is? Thanks!

I made a function with the try statement but i still get an error:

#Function to merge the data with the code descriptions.
myfunction = function(mydata){

  ##Sets the variables to factors to merge with the code descriptions.
  mydata[] <- lapply(mydata, factor)

  ##Adds "ID" to the end of all the variables to make the merges compatible.
  names(mydata) <- paste(names(mydata), "ID", sep = "")

  ##Uses the dplyr package to make the merges for the variables needed.
  try(mydata = mydata %>% left_join(dischargestatuscode, by = "DischargeStatusID") %>% select(-DischargeStatusID, -Code))
  try(mydata = mydata %>% left_join(statecode, by = "StateID") %>% select(-StateID, -Code))
  try(mydata = mydata %>% left_join(gendercode, by = "GenderID") %>% select(-GenderID, -Code))
  try(mydata = mydata %>% left_join(racecode, by = "RaceID") %>% select(-RaceID, -Code))
  try(mydata = mydata %>% left_join(agecode, by = "AgeID") %>% select(-AgeID, -Code, -(MinAge:MaxAge)))
  try(mydata = mydata %>% left_join(diagnosistypecode, by = "DiagnosisCodeTypeID") %>% select(-DiagnosisCodeTypeID, -Code))
  try(mydata = mydata %>% left_join(hcpcscode, by = "HCPCCodeID") %>% select(-HCPCCodeID, -Code))
  try(mydata = mydata %>% left_join(countycode, by = "CountyID") %>% select(-CountyID, -Code, -StateID))
  try(mydata = mydata %>% left_join(admissionsourcecode, by = "AdmissionSourceID") %>% select(-AdmissionSourceID, -Code))
  try(mydata = mydata %>% left_join(admissiontypecode, by = "AdmissionTypeID") %>% select(-AdmissionTypeID, -Code))
  try(mydata = mydata %>% left_join(msdrgcodes, by = "ClaimRelatedDRGID") %>% 
    select(-ClaimRelatedDRGID, -Code, -MdcCategoryID, -type))
  try(mydata = mydata %>% left_join(nchpatientstatuscode, by = "NCHPatientStatusID") %>% select(-NCHPatientStatusID, -Code))

  ##If there is a ICDAccessCode column, the first function works, if there is a ProcedureID column the second function works.
  try(mydata = mydata %>% left_join(icd9codes, by = "ICDAccessCodeID") %>% 
    select(-ICDAccessCodeID, -(ICD9Code:CodeType), -LongDescription))
  try(mydata = mydata %>% left_join(icd9codes, by = "ProcedureIDID") %>% 
    select(-ICDAccessCodeID, -(ICD9Code:CodeType), -LongDescription, -ProcedureIDID, -ICD9Desc))

  ##Removes the "ID" used for compatbility.
  names(mydata) <- gsub("ID", "", names(mydata))
  InpatientHistory <<- mydata
}

myfunction(InpatientHistory)

Error I get when i use try:

Error in try(mydata = mydata %>% left_join(dischargestatuscode, by = "DischargeStatusID") %>%  : 
  unused argument (mydata = mydata %>% left_join(dischargestatuscode, by = "DischargeStatusID") %>% select(-DischargeStatusID, -Code))
Christopher Yee
  • 535
  • 2
  • 5
  • 14
  • Downvote with no comment <- bad form – Brandon Bertelsen May 27 '15 at 15:36
  • 1
    `=` is not equivalent with `<-`. The parser believes you want to specify an argument to `try`. Use `<-` for assignment inside `try`. Of course, you might not get the expected result if one of your expressions would have thrown an error. You should write error handlers. And do not use `<<-` or `->>`. Those should be used under very exceptional circumstances only. Follow R's functional language paradigm. – Roland May 27 '15 at 17:24
  • perfect, thank you all for the help! – Christopher Yee May 27 '15 at 17:44

1 Answers1

4

Use a tryCatch expression around the function that can throw the error message:

testFunction <- function (date_in) {
  return(tryCatch(as.Date(date_in), error=function(e) NULL))
}

You could also use try if you don't have a proper error handling function, as mentioned in the comments, but the R language manual indicates that:

try is implemented using tryCatch; for programming, instead of try(expr, silent = TRUE), something like tryCatch(expr, error = function(e) e) (or other simple error handler functions) may be more efficient and flexible.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • If you don't use a proper error handler you should just use `try`. – Roland May 27 '15 at 15:40
  • thanks, i think it is on the right track. I posted the function i am using, but i am still getting an error. I may be doing this very wrong. Sorry ive only been using R for about a month now. – Christopher Yee May 27 '15 at 15:45
  • @ChristopherYee can you post `InpatientHistory` or tell me how to reproduce it? – Hack-R May 27 '15 at 15:48
  • I really wish i could, but that data is confidential and i would get in a lot of trouble haha. But it is basically a medical data table that is coded and I am looking to put the code descriptions in from the various description tables. I don't know if that helps though. – Christopher Yee May 27 '15 at 15:49
  • I understand. They usually want you to reproduce your error with a non-sensitive dataset (like one of the ones built into some R package) then post the reproducible version of it here. Maybe you can load something like `data(AirPassengers)` and see if you can get the same error, then post it here? – Hack-R May 27 '15 at 15:51
  • Ill see if i can reproduce that using the air passengers data set. – Christopher Yee May 27 '15 at 15:57
  • i posted the error i get when i use try to the original post. i hope that may be able to help for now. – Christopher Yee May 27 '15 at 15:59