R Challenge #2: Graceful Coding

R Challenge #2: Graceful Coding

Don't let the user crash. It's rude.

There are plenty of well-written articles on defensive programming, so I'll avoid repeating what you may already know. In short, if an error happens, give your code a strategy to fix it. That's the point of this R Code Challenge.

R Challenge #2 : Recover gracefully when an import fails

This challenge gives you the task of importing a CSV file from a web site. If the import fails, your code should load a local copy of the file. Easy if you know how...

Here's the video explaining the challenge - and showing a solution. (hint: try catch() )

Here's My Solution

This is the code I suggest - a deeper explanation is in the video above.


worldPopURL <- "https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv"

worldPopFile <- "worldPopulation.csv"

worldPopColClasses <- c("integer","character","integer",?
? ? ? ? ? ? ? ? ? ? ? ? "factor", "integer", "numeric",
? ? ? ? ? ? ? ? ? ? ? ? "numeric","numeric","numeric","numeric")?

worldPop <- tryCatch(read.csv(worldPopURL,colClasses = worldPopColClasses ),
? ? ? ? ? ? ? ? ? ? ?error = function(e) {
? ? ? ? ? ? ? ? ? ? ? ?message(paste("The error is:", e))
? ? ? ? ? ? ? ? ? ? ? ?read.csv(worldPopFile,colClasses = worldPopColClasses )
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ?)
        

More about tryCatch()

I did a video on try catch for the R weekly series. It's a powerful tool and has a lot of options you might not realize are there. Take a look...

The Hidden Story

Good sample data should be interesting and easy to obtain. Unfortunately, the internet is chaotic and data is short-lived. Data disappears, it gets updated, it gets moved. The data set I use in this series of code challenges seems stable - but every once in a while, it would go awol.

In the original code clinic, I used a public data set from the US Navy at Lake Pend Oreille. They provided a detailed list of the weather conditions on the lake - until they suddenly didn't. I was surprised at the number of people trying to complete the exercise and having no way to continue. We finally posted an incomplete set on GitHub.

I learned my lesson - and this is the challenge I pass on through this exercise: don't let the user fail!

How about you??

In this and future articles, I'll share some backstory behind each R challenge in this course. My peers have supplied other code challenges you might find interesting:?Javascript?...?Python?...?Java?...?Github?...?HTML?...?SQL?...?SQL for Data Science?...?PHP

Do you have some opinions on this challenge? Please share them in the comments below.

mnr

要查看或添加评论,请登录

Mark Niemann-Ross的更多文章

  • Documenting My Code ... For Me

    Documenting My Code ... For Me

    There are two signs of old age: old age, and ..

  • R Meets Hardware

    R Meets Hardware

    R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data…

    2 条评论
  • Party Buzz Kill: modifying data

    Party Buzz Kill: modifying data

    So Steve (SQL), Marsha (C), Bob (Python), and I (R) are at this party. We have TOTALLY cleared the room, especially now…

    2 条评论
  • Rain - Evapotranspiration = mm Water

    Rain - Evapotranspiration = mm Water

    "Eeee-VAP-oooo-TRANS-PURR-ation," I savor the word as I release it into our conversation. I'm still at the party with…

  • Party Buzz Kill: Data Storage

    Party Buzz Kill: Data Storage

    I'm at this party where Bob and Marsha and I are discussing the best languages for programming a Raspberry Pi. Bob…

    5 条评论
  • R Waters My Garden

    R Waters My Garden

    I'm at a party, and the topic of programming languages comes up. A quarter of the room politely leaves, another half…

    10 条评论
  • Caning and Naming

    Caning and Naming

    We've been back from Port Townsend for a week. Progress on the boat isn't as dramatic as it is when we're spending the…

    1 条评论
  • Irrigate with R and Raspberry Pi

    Irrigate with R and Raspberry Pi

    I’m working on my irrigation system. This requires a controller to turn it on and off.

    3 条评论
  • 5 Reasons to Learn Natural Language Processing with R

    5 Reasons to Learn Natural Language Processing with R

    Why learn R? Why learn Natural Language Processing? Here's five reasons..

    1 条评论
  • Performing Natural Language Processing with R

    Performing Natural Language Processing with R

    I recently released a course on Educative covering topics in Natural Language Processing. Different Learners -…

    1 条评论

社区洞察

其他会员也浏览了