Skip to content

Instantly share code, notes, and snippets.

@marisae
Last active February 25, 2023 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marisae/2f506e9c2f768303abf3d34c215555b1 to your computer and use it in GitHub Desktop.
Save marisae/2f506e9c2f768303abf3d34c215555b1 to your computer and use it in GitHub Desktop.
[Webpage Scraping in R] #data #R
###################################
# Just download a file from a URL #
###################################
url = "https://www.michigan.gov/documents/coronavirus/Covid_Coverage_State_Level_Metrics_718470_7.xlsx"
savefile = "~/Desktop/Covid_Coverage_State_Level_Metrics_718470_7.xlsx"
download.file(url, savefile)
############################################
# Parse a webpage and do something with it #
############################################
library(rvest)
library(readr)
Examplepage = read_html("https://www.michigan.gov/coronavirus/0,9753,7-406-98159-523641--,00.html")
Exampletables = html_nodes(Examplepage, "table")
Exampledivs = html_nodes(Examplepage, "div")
ExampleData = html_table(Exampletables[[1]])
ggplot(ExampleData) + geom_col(aes(x = Region, y = `All Hospital Inpatient Bed Occupancy`/`All Hospital Inpatient Beds`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment