跳过函数中的错误

时间:2015-05-27 15:32:12

标签: r

我做了一个功能,但是我遇到了一些错误。但是,这些错误可能会被忽视。是否有任何函数可以用来跳过错误并继续按原样处理函数?谢谢!

我使用try语句创建了一个函数,但仍然出现错误:

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

我在使用try时遇到错误:

Error in try(mydata = mydata %>% left_join(dischargestatuscode, by = "DischargeStatusID") %>%  : 
  unused argument (mydata = mydata %>% left_join(dischargestatuscode, by = "DischargeStatusID") %>% select(-DischargeStatusID, -Code))

1 个答案:

答案 0 :(得分:4)

在可能抛出错误消息的函数周围使用tryCatch表达式:

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

如果您没有正确的错误处理功能,也可以使用try,如评论中所述,但R语言手册表明:

  

try是使用tryCatch实现的;用于编程,而不是   try(expr, silent = TRUE),类似tryCatch(expr, error = function(e) e)(或其他简单的错误处理函数)可能更多   高效灵活。