0

is there a way to do error handling using function "read_excel" from R when the file exists, but it can't be read for some other reason (e.g., wrong format or something)?

Just to illustrate, my piece of code is as follows:

f <- GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(tmpdir = here("data/temp"), fileext = ".xlsx")))
dt <- read_excel(tf)

where url contains the http file address.

I would like to check if read_excel returns an error to do the proper handling and prevent the markdown stops.

Thanks in advance!

1 Answers1

0

Looks like a duplicate question. The code below is modified from the answer found here. The someOtherFunction in the code below is where one can have some function to run if there is an error.

f <- GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(tmpdir = here("data/temp"), fileext = ".xlsx")))

t <- try(read_excel(tf))

if("try-error" %in% class(t)) SomeOtherFunction()
  • Thank you so much Sidhartha, for the fastest response ever! I really apreciated - it worked very fine:) (didnt know the function "try") thanks again – Luis Correia Apr 19 '20 at 06:37