Easy error handling in R with purrr’s possibly

It is aggravating to see your code choke component of the way as a result of although striving to apply a functionality in R. You may perhaps know that something in a person of people objects brought on a issue, but how do you monitor down the offender?

The purrr package’s quite possibly() functionality is a person quick way.

In this instance, I’ll demo code that imports several CSV documents. Most files’ value columns import as figures, but a person of these arrives in as quantities. Functioning a functionality that expects figures as enter will induce an mistake.

For set up, the code beneath hundreds numerous libraries I want and then employs base R’s listing.documents() functionality to return a sorted vector with names of all the documents in my information directory. 

my_information_documents <- list.files("data_files", full.names = TRUE) %>%

I can then import the very first file and look at its framework. 

x <- rio::import("data_files/file1.csv")
'data.frame':	3 obs. of  3 variables:
 $ Category     : chr  "A" "B" "C"
 $ Value        : chr  "$4,256.48 " "$438.22" "$945.12"
 $ MonthStarting: chr  "12/1/20" "12/1/20" "12/1/20"

Both the Worth and Month columns are importing as character strings. What I eventually want is Worth as quantities and MonthStarting as dates. 

Copyright © 2020 IDG Communications, Inc.