Webscraping Tables in R: Datapasta Copy-and-Paster
Written by Matt Dancho
This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.
Here are the links to get set up. 👇
I wish I knew about Datapasta sooner
This R package saves so much time.
Datapasta is an amazing package that allows us to copy-and-paste any HTML or Excel Tables into R. I only wish I knew about it sooner. We'll use Datapasta to:
- Copy Stock Data from Yahoo! Finance
- Copy Revenue Data on the Largest Companies in the World from Wikipedia
Before we get started, get the Cheat Sheet
Datapasta is great for importing raw data from HTML tables on the web. But, you'll still need to learn how to transform / wrangle the data and produce visualizations. For those topics, I'll use the Ultimate R Cheat Sheet to refer to
ggplot2 code in my workflow.
Quick example - Clicking the "CS" next to "dplyr" opens the Data Transformation with Dplyr Cheat Sheet.
Now you're ready to quickly reference
dplyr functions. Ok, onto the tutorial.
Datapasta RStudio Addin
Datapasta contains an RStudio Add-In for Pasting web-tables stored in your "clipboard" (what happens when you "copy" something).
Example 1: Copying Stock Data from Yahoo! Finance
Let's go through a quick example. We can navigate to Yahoo! Finance and search for a ticker symbol like AAPL.
Next, use the Datapasta Addin to "paste as tribble". This pastes our data into our R script file.
timetk to wrangle and visualize the data. (Refer to the ultimate R cheat sheet for documentation on dplyr and timetk). We can see a spike in volume on last day of the month.
Example 2: Getting Revenue Data for World Largest Companies
First, head over to Wikipedia and search for the "list of largest companies".
Use datapasta to "paste as data.table". Then do some data wrangling with dplyr. Then visualize with ggplot2. And in a few lines of code you can create this chart showing that Walmart is dominating in Revenue. (Refer to the ultimate R cheat sheet for documentation on dplyr and ggplot2).
You just quickly scraped HTML tables using the copy-and-paster Rstudio Add-In known as datapasta . This is an amazing productivity boost!!
This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks. Join today.
👇 Top R-Tips Tutorials you might like:
- mmtable2: ggplot2 for tables
- ggdist: Make a Raincloud Plot to Visualize Distribution in ggplot2
- ggside: Plot linear regression with marginal distributions
- DataEditR: Interactive Data Editing in R
- openxlsx: How to Automate Excel in R
- officer: How to Automate PowerPoint in R
- DataExplorer: Fast EDA in R
- esquisse: Interactive ggplot2 builder
- gghalves: Half-plots with ggplot2
- rmarkdown: How to Automate PDF Reporting
- patchwork: How to combine multiple ggplots
- Geospatial Map Visualizations in R
Want these tips every week? Join R-Tips Weekly.