Working with Web Data in R

Reading and writing web data in R involves fetching data from online sources, such as websites or APIs, and processing it within R. This can be accomplished using packages like httr and rvest.

Reading Web Data

To read web data, you can use the httr package to make HTTP requests and fetch content from URLs. You can then parse the HTML content using the rvest package.

# Install and load the httr and rvest packages install.packages("httr") install.packages("rvest") library(httr) library(rvest) # Example: Reading and parsing HTML content from a website url <- "" response <- GET(url) html_content <- content(response, as = "text") parsed_html <- read_html(html_content) # Extract specific information from the parsed HTML title <- html_text(html_nodes(parsed_html, "title")) print(title)

Writing Web Data

Writing web data usually involves sending data to a web service using HTTP POST or PUT requests. You can use the httr package to achieve this.

# Example: Writing data to a web service using POST request url <- "" data_to_send <- list( name = "Alice", age = 25 ) response <- POST(url, body = data_to_send, encode = "json") print(content(response))

Fetching JSON Data from APIs

When dealing with web data, you often interact with APIs that return data in JSON format. You can use the httr package to fetch and parse JSON data.

# Example: Fetching JSON data from an API api_url <- "" response <- GET(api_url) json_data <- content(response, as = "parsed") print(json_data)

Web Scraping with rvest

rvest is a package specifically designed for web scraping, allowing you to extract specific information from web pages.

# Example: Web scraping with rvest url <- "" page <- read_html(url) headline <- page %>% html_nodes(".headline") %>% html_text() print(headline)


Reading and writing web data in R involves using packages like httr for making HTTP requests and rvest for parsing HTML content. These tools enable you to fetch data from websites and APIs, as well as scrape specific information for analysis and integration with your R projects.