### Install RedditExtractor Package Source: https://github.com/ivan-rivera/redditextractor/blob/master/README.md Installs the RedditExtractor package from CRAN or GitHub. Use the version argument for specific versions or if R version is < 4.1. ```r install.packages("RedditExtractoR") ``` ```r devtools::install_github('ivan-rivera/RedditExtractor') ``` ```r devtools::install_version("RedditExtractoR", version = "2.1.5", repos = "http://cran.us.r-project.org") ``` -------------------------------- ### Install RedditExtractoR Package Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Install the RedditExtractoR package from CRAN for the stable version, from GitHub for the development version, or an older version for R < 4.1. ```r # From CRAN (stable) install.packages("RedditExtractoR") ``` ```r # From GitHub (development) devtools::install_github("ivan-rivera/RedditExtractor") ``` ```r # Install older version (for R < 4.1) devtools::install_version("RedditExtractoR", version = "2.1.5", repos = "http://cran.us.r-project.org") ``` -------------------------------- ### Get Thread Content and Comments Source: https://github.com/ivan-rivera/redditextractor/blob/master/README.md Retrieves metadata and comments for specified thread URLs. The output is split into 'threads' and 'comments' data frames. ```r threads_contents <- get_thread_content(top_cats_urls$url[1:2]) # for the sake of simplicity str(threads_contents$threads) # thread metadata ``` ```r str(threads_contents$comments) ``` -------------------------------- ### Get User Content by Username Source: https://github.com/ivan-rivera/redditextractor/blob/master/README.md Retrieve all content associated with a specific Reddit username, including their profile information, comments, and threads. The function returns a list where each element corresponds to a user. ```r user <- "nationalgeographic" nat_geo_user <- get_user_content(user) str(nat_geo_user[[user]]$about) # List of 7 # $ created_utc : chr "2017-08-24" # $ name : chr "nationalgeographic" # $ is_employee : logi FALSE # $ is_mod : logi TRUE # $ is_gold : logi TRUE # $ thread_karma : num 279068 # $ comment_karma: num 87406 ``` ```r str(nat_geo_user[[user]]$comments) # 'data.frame': 997 obs. of 11 variables: # $ url : chr "https://www.reddit.com/r/history/comments/anhdnl/im_historian_author_and_musician_mark_lee_gardner/" "https://www.reddit.com/r/history/comments/anhdnl/im_historian_author_and_musician_mark_lee_gardner/" "https://www.reddit.com/r/history/comments/anhdnl/im_historian_author_and_musician_mark_lee_gardner/" "https://www.reddit.com/r/history/comments/anhdnl/im_historian_author_and_musician_mark_lee_gardner/" ... # $ date_utc : chr "2019-02-05" "2019-02-05" "2019-02-05" "2019-02-05" ... # $ subreddit : chr "history" "history" "history" "history" ... # $ thread_author : chr "nationalgeographic" "nationalgeographic" "nationalgeographic" "nationalgeographic" ... # $ comment_author: chr "nationalgeographic" "nationalgeographic" "nationalgeographic" "nationalgeographic" ... # $ thread_title : chr "I\031m historian, author, and musician Mark Lee Gardner and I can tell you a lot about Jesse James, the infamou"| __truncated__ "I\031m historian, author, and musician Mark Lee Gardner and I can tell you a lot about Jesse James, the infamou"| __truncated__ "I\031m historian, author, and musician Mark Lee Gardner and I can tell you a lot about Jesse James, the infamou"| __truncated__ "I\031m historian, author, and musician Mark Lee Gardner and I can tell you a lot about Jesse James, the infamou"| __truncated__ ... # $ comment : chr "You're welcome. Part of Jesse and Frank's success in eluding law enforcement (in addition to fast horses!) was"| __truncated__ "In 1874, the Missouri legislature passed the Suppression of Outlawry Act that set aside a \"state secret servic"| __truncated__ "Probably the quick-draw gunfight: two men staring each other down in the middle of the street and attempting to"| __truncated__ "That was actually part of the problem. There was often little coordination between towns and the state. In fa"| __truncated__ ... # $ score : num 2 2 9 4 8 6 6 10 13 2 ... # $ up : num 2 2 9 4 8 6 6 10 13 2 ... # $ downs : num 0 0 0 0 0 0 0 0 0 0 ... # $ golds : num 0 0 0 0 0 0 0 0 0 0 .. ``` ```r str(nat_geo_user[[user]]$threads) # 'data.frame': 999 obs. of 10 variables: # $ url : chr "https://www.nationalgeographic.com/environment/2019/02/2018-fourth-warmest-year-ever-noaa-nasa-reports/?cmpid=o"| __truncated__ "https://www.reddit.com/r/history/comments/anhdnl/im_historian_author_and_musician_mark_lee_gardner/" "https://www.nationalgeographic.com/environment/2019/02/climate-change-alters-oceans-blues-greens/" "https://v.redd.it/ehqa55sbcge21" ... # $ date_utc : chr "2019-02-06" "2019-02-05" "2019-02-05" "2019-02-04" ... # $ subreddit: chr "u_nationalgeographic" "history" "u_nationalgeographic" "u_nationalgeographic" ... # $ author : chr "nationalgeographic" "nationalgeographic" "nationalgeographic" "nationalgeographic" ... ``` -------------------------------- ### Load RedditExtractoR Package Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Load the RedditExtractoR library into your R session to use its functions. ```r library(RedditExtractoR) ``` -------------------------------- ### Retrieve User Profile and History Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Use `get_user_content()` with a username or vector of usernames to fetch user profile information (`$about`), comments (`$comments`), and submitted threads (`$threads`). If a user or their content is not found, corresponding elements will be `NA`. ```r library(RedditExtractoR) # Single user nat_geo <- get_user_content("nationalgeographic") # parsing user nationalgeographic... # Profile metadata str(nat_geo[["nationalgeographic"]]$about) # List of 8: # $ created_utc : chr "2017-08-24" # $ timestamp : num 1503532800 # $ name : chr "nationalgeographic" # $ is_employee : logi FALSE # $ is_mod : logi TRUE # $ is_gold : logi TRUE # $ thread_karma : num 279068 # $ comment_karma: num 87406 # User's comment history head(nat_geo[["nationalgeographic"]]$comments[, c("date_utc", "subreddit", "comment", "score")], 2) # date_utc subreddit comment score # 1 2019-02-05 history You're welcome. Part of Jesse... 2 # 2 2019-02-05 history In 1874, the Missouri legislat... 2 # User's thread submission history head(nat_geo[["nationalgeographic"]]$threads[, c("date_utc", "subreddit", "title", "score")], 2) # date_utc subreddit title score # 1 2019-02-06 u_nationalgeographic The last five years were the hott... 303 # 2 2019-02-05 history I'm historian, author, and musicia... 62 # Multiple users at once users_data <- get_user_content(c("nasa", "natgeo", "PBS")) # parsing user nasa... # parsing user natgeo... # parsing user PBS... # Compare karma across users karma_summary <- sapply(users_data, function(u) { if (!is.null(u$about) && !all(is.na(u$about))) { c(thread_karma = u$about$thread_karma, comment_karma = u$about$comment_karma) } else { c(thread_karma = NA, comment_karma = NA) } }) t(karma_summary) ``` -------------------------------- ### Inspect Top Subreddits by Subscribers Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Inspect the top 5 subreddits sorted by subscriber count, displaying the subreddit name, number of subscribers, and its description. ```r # Inspect top results sorted by subscribers ds_subreddits[order(-ds_subreddits$subscribers), c("subreddit", "subscribers", "description")][1:5, ] # subreddit subscribers description # 1 datascience 1234567 A place for data science pract... # 2 MachineLearning 987654 Reddit's home for ML discussions # ... ``` -------------------------------- ### Find Subreddits by Keyword Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Search Reddit's subreddit directory for communities matching a keyword. Returns a data frame with community metadata including subscriber counts and descriptions. ```r library(RedditExtractoR) # Find subreddits related to "data science" ds_subreddits <- find_subreddits("data science") str(ds_subreddits) # 'data.frame': ~248 obs. of 7 variables: # $ id : chr "2fwo" "3kfue" ... # $ date_utc : chr "2011-03-22" "2014-11-11" ... # $ timestamp : num 1300752000 ... # $ subreddit : chr "datascience" "learnmachinelearning" ... # $ title : chr "Data Science" "Learn Machine Learning" ... # $ description: chr "A place for data science practitioners..." ... # $ subscribers: num 1234567 456789 ... ``` -------------------------------- ### find_subreddits() — Discover Subreddits by Keyword Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Searches Reddit's subreddit directory for communities matching a keyword. Returns a data frame with community metadata including subscriber counts and descriptions, useful for discovering relevant communities before deeper data collection. ```APIDOC ## find_subreddits() ### Description Searches Reddit's subreddit directory for communities matching a keyword. Returns a data frame with community metadata including subscriber counts and descriptions. ### Parameters #### Path Parameters None #### Query Parameters - **keyword** (string) - Required - The keyword to search for subreddits. ### Request Example ```r library(RedditExtractoR) # Find subreddits related to "data science" ds_subreddits <- find_subreddits("data science") ``` ### Response #### Success Response (data.frame) - **id** (string) - The unique ID of the subreddit. - **date_utc** (string) - The creation date of the subreddit in UTC. - **timestamp** (numeric) - The Unix timestamp of the subreddit's creation. - **subreddit** (string) - The name of the subreddit. - **title** (string) - The title of the subreddit. - **description** (string) - The description of the subreddit. - **subscribers** (numeric) - The number of subscribers to the subreddit. ### Response Example ```r # Example structure of the returned data frame # str(ds_subreddits) # 'data.frame': ~248 obs. of 7 variables: # $ id : chr "2fwo" "3kfue" ... # $ date_utc : chr "2011-03-22" "2014-11-11" ... # $ timestamp : num 1300752000 ... # $ subreddit : chr "datascience" "learnmachinelearning" ... # $ title : chr "Data Science" "Learn Machine Learning" ... # $ description: chr "A place for data science practitioners..." ... # $ subscribers: num 1234567 456789 ... ``` ``` -------------------------------- ### Retrieve Thread Content and Comments Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Use `get_thread_content()` with a vector of thread URLs to fetch thread metadata and all associated comments. The results are returned as a named list containing two data frames: `$threads` for metadata and `$comments` for comments. Both tables include a `url` column for joining, and comment hierarchy is encoded in `comment_id`. ```r library(RedditExtractoR) # Step 1: get URLs top_cats <- find_thread_urls(subreddit = "cats", sort_by = "top", period = "month") # Step 2: retrieve content for the top 3 threads content <- get_thread_content(top_cats$url[1:3]) # Inspect thread metadata str(content$threads) # 'data.frame': 3 obs. of 15 variables: # $ url : chr "https://www.reddit.com/r/cats/comments/..." # $ author : chr "CasterQ" ... # $ date : chr "2023-08-15" ... # $ timestamp : num 1692144000 ... # $ title : chr "This went on for over 5 min" ... # $ text : chr "" ... # $ subreddit : chr "cats" ... # $ score : num 320 322 198 ... # $ upvotes : num 320 322 198 ... # $ downvotes : num 0 0 0 ... # $ up_ratio : num 1 0.99 0.97 ... # $ total_awards_received: num 2 1 0 ... # $ golds : num 0 0 0 ... # $ cross_posts : num 0 1 0 ... # $ comments : num 12 121 44 ... # Inspect comments str(content$comments) # 'data.frame': 177 obs. of 10 variables: # $ url : chr "https://www.reddit.com/r/cats/comments/..." # $ author : chr "stinkadinkalink" ... # $ date : chr "2023-08-15" ... # $ timestamp : num 1692144000 ... # $ score : num 21 10 6 ... # $ upvotes : num 21 10 6 ... # $ downvotes : num 0 0 0 ... # $ golds : num 0 0 0 ... # $ comment : chr "such a cute cat but omg how many monitors..." ... # $ comment_id : chr "1" "1_1" "1_1_1" "2" ... # Join threads and comments by URL library(dplyr) full_data <- content$comments |> left_join(content$threads[, c("url", "title", "score")], by = "url") # Filter top-scoring comments top_comments <- content$comments[content$comments$score > 10, ] ``` -------------------------------- ### Find Subreddits by Keyword Source: https://github.com/ivan-rivera/redditextractor/blob/master/README.md Use this function to search for subreddits related to a specific keyword. The output is a data frame containing subreddit details. ```r cat_subreddits <- find_subreddits("cats") # 'data.frame': 248 obs. of 6 variables: # $ id : chr "3gl3k" "2vi0z" "30tmh" "2tteh" ... # $ date_utc : chr "2016-09-28" "2012-11-08" "2014-03-05" "2012-03-29" ... # $ subreddit : chr "MemeEconomy" "Awwducational" "TwoSentenceHorror" "Justrolledintotheshop" ... # $ title : chr "MemeEconomy" "Awwducational" "Two-Sentence Horror Stories: Bite-sized scares. " "Just Rolled Into the Shop" ... # $ description: chr "/r/MemeEconomy is a place where individuals can buy, sell, share, make, and invest in templates freely.\n\n\nv2"| __truncated__ "Don't just waste time, learn something too!" "Give us your scariest story in two sentences (or less)!" "For those absolutely stupid things that you see people bring, roll, or toss into your place of business and the"| __truncated__ ... # $ subscribers: num 1430743 2965466 678423 1365589 586672 .. ``` -------------------------------- ### get_user_content() Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Retrieves profile information, comments, and submitted posts for one or more Reddit usernames. Returns a named list where each element corresponds to a username and contains their profile data. ```APIDOC ## `get_user_content()` — Retrieve Reddit User Profiles and History Accepts a single username or a character vector of usernames and returns a named list. Each entry contains three elements: `$about` (a list of profile metadata), `$comments` (a data frame of the user's comments), and `$threads` (a data frame of the user's submitted posts). If a user does not exist or has no content, the corresponding elements are `NA`. ### Parameters * **usernames** (character vector) - A single username or a vector of usernames. ### Returns A named list where each key is a username. Each value is another list containing: * **`$about`**: A list of profile metadata (created_utc, timestamp, name, is_employee, is_mod, is_gold, thread_karma, comment_karma). * **`$comments`**: A data frame of the user's comments (date_utc, subreddit, comment, score, etc.). * **`$threads`**: A data frame of the user's submitted posts (date_utc, subreddit, title, score, etc.). ### Example ```r library(RedditExtractoR) # Single user nat_geo <- get_user_content("nationalgeographic") # Profile metadata str(nat_geo[["nationalgeographic"]]$about) # User's comment history head(nat_geo[["nationalgeographic"]]$comments[, c("date_utc", "subreddit", "comment", "score")], 2) # User's thread submission history head(nat_geo[["nationalgeographic"]]$threads[, c("date_utc", "subreddit", "title", "score")], 2) # Multiple users at once users_data <- get_user_content(c("nasa", "natgeo", "PBS")) # Compare karma across users karma_summary <- sapply(users_data, function(u) { if (!is.null(u$about) && !all(is.na(u$about))) { c(thread_karma = u$about$thread_karma, comment_karma = u$about$comment_karma) } else { c(thread_karma = NA, comment_karma = NA) } }) t(karma_summary) ``` ``` -------------------------------- ### find_thread_urls() — Search for Reddit Thread URLs Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Searches Reddit for thread URLs either by keyword query or by browsing a subreddit's front page. Returns a data frame with thread metadata and URLs suitable for passing to `get_thread_content()`. When `keywords` is provided, `sort_by` must be one of `"relevance"`, `"comments"`, `"new"`, `"hot"`, `"top"`; without keywords, it must be one of `"hot"`, `"new"`, `"top"`, `"rising"`. The `period` parameter accepts `"hour"`, `"day"`, `"week"`, `"month"`, `"year"`, or `"all"`. ```APIDOC ## find_thread_urls() ### Description Searches Reddit for thread URLs either by keyword query or by browsing a subreddit's front page. Returns a data frame with thread metadata and URLs suitable for passing to `get_thread_content()`. ### Parameters #### Path Parameters None #### Query Parameters - **keywords** (string) - Optional - Keywords to search for. - **subreddit** (string) - Optional - The specific subreddit to search within. - **sort_by** (string) - Required - How to sort the results. Must be one of `"relevance"`, `"comments"`, `"new"`, `"hot"`, `"top"` (if keywords provided), or `"hot"`, `"new"`, `"top"`, `"rising"` (if no keywords). - **period** (string) - Required - The time period for sorting. Accepts `"hour"`, `"day"`, `"week"`, `"month"`, `"year"`, or `"all"`. ### Request Example ```r library(RedditExtractoR) # Search by keyword across all subreddits kittens_urls <- find_thread_urls(keywords = "cute kittens", sort_by = "top", period = "month") # Browse a specific subreddit's top posts of all time cats_top <- find_thread_urls(subreddit = "cats", sort_by = "top", period = "all") # Keyword search restricted to a single subreddit science_news <- find_thread_urls( keywords = "climate change", subreddit = "science", sort_by = "relevance", period = "year" ) ``` ### Response #### Success Response (data.frame) - **date_utc** (string) - The date of the post in UTC. - **timestamp** (numeric) - The Unix timestamp of the post. - **title** (string) - The title of the thread. - **text** (string) - The text content of the post (may be empty). - **subreddit** (string) - The name of the subreddit. - **comments** (numeric) - The number of comments on the thread. - **url** (string) - The URL of the thread. ### Response Example ```r # Example structure of the returned data frame # str(kittens_urls) # 'data.frame': ~100 obs. of 7 variables: # $ date_utc : chr "2023-08-15" "2023-08-10" ... # $ timestamp : num 1692144000 ... # $ title : chr "Meet my new kitten!" ... # $ text : chr "" ... # $ subreddit : chr "aww" "cats" ... # $ comments : num 120 45 ... # $ url : chr "https://www.reddit.com/r/aww/comments/..." ``` ``` -------------------------------- ### Find Reddit Thread URLs in a Subreddit Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Browse a specific subreddit's posts, sorted by 'top' and with the 'all' period. The head function displays the first 3 results, showing title, comments, and URL. ```r # Browse a specific subreddit's top posts of all time cats_top <- find_thread_urls(subreddit = "cats", sort_by = "top", period = "all") head(cats_top[, c("title", "comments", "url")], 3) # title comments url # 1 This went on for over 5 min 12 https://www.reddit.com/r/cats/comments/p4zwkx/... # 2 Found this friendly stray... 121 https://www.reddit.com/r/cats/comments/p3no3t/... # 3 Let's wake up now 7 https://www.reddit.com/r/cats/comments/p21kyf/... ``` -------------------------------- ### Find Top Subreddit Threads Source: https://github.com/ivan-rivera/redditextractor/blob/master/README.md Finds URLs for top posts in a specified subreddit, sorted by 'top'. Alternatively, use keywords to search for threads. ```r top_cats_urls <- find_thread_urls(subreddit="cats", sort_by="top") str(top_cats_urls) ``` ```r find_thread_urls(keywords="cute kittens") ``` -------------------------------- ### Find Reddit Thread URLs with Keyword and Subreddit Filters Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Perform a keyword search within a specific subreddit, sorted by 'relevance' for the 'year' period. The nrow function shows the number of results, which can be up to approximately 999 due to pagination. ```r # Keyword search restricted to a single subreddit science_news <- find_thread_urls( keywords = "climate change", subreddit = "science", sort_by = "relevance", period = "year" ) nrow(science_news) # up to ~999 results (paginated) ``` -------------------------------- ### Use Discovered Subreddit for Thread Collection Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Use the name of the top community found to perform further thread collection, searching for 'hot' posts from the past 'week'. ```r # Use results to drive further thread collection top_community <- ds_subreddits$subreddit[1] top_threads <- find_thread_urls(subreddit = top_community, sort_by = "hot", period = "week") ``` -------------------------------- ### Find Reddit Thread URLs by Keyword Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Search for Reddit thread URLs using keywords. The sort_by parameter must be one of 'relevance', 'comments', 'new', 'hot', or 'top'. The period parameter accepts 'hour', 'day', 'week', 'month', 'year', or 'all'. ```r library(RedditExtractoR) # Search by keyword across all subreddits kittens_urls <- find_thread_urls(keywords = "cute kittens", sort_by = "top", period = "month") str(kittens_urls) # 'data.frame': ~100 obs. of 7 variables: # $ date_utc : chr "2023-08-15" "2023-08-10" ... # $ timestamp : num 1692144000 ... # $ title : chr "Meet my new kitten!" ... # $ text : chr "" ... # $ subreddit : chr "aww" "cats" ... # $ comments : num 120 45 ... # $ url : chr "https://www.reddit.com/r/aww/comments/..." ... ``` -------------------------------- ### get_thread_content() Source: https://context7.com/ivan-rivera/redditextractor/llms.txt Retrieves metadata and comments for a given set of Reddit thread URLs. It returns a named list containing two data frames: one for thread metadata and another for all comments. ```APIDOC ## `get_thread_content()` — Retrieve Thread Metadata and Comments Takes a vector of Reddit thread URLs and returns a named list with two data frames: `$threads` (one row per thread with metadata) and `$comments` (all comments across all threads). Both tables share the `url` column for easy joining. Comment hierarchy is encoded in `comment_id` using dot-separated depth notation (e.g., `"1_2_3"` = 3rd reply to 2nd reply to 1st top-level comment). ### Parameters * **thread_urls** (character vector) - A vector of Reddit thread URLs. ### Returns A named list with two data frames: * **`$threads`**: Contains metadata for each thread (url, author, date, timestamp, title, text, subreddit, score, upvotes, downvotes, up_ratio, total_awards_received, golds, cross_posts, comments). * **`$comments`**: Contains all comments for the threads (url, author, date, timestamp, score, upvotes, downvotes, golds, comment, comment_id). ### Example ```r library(RedditExtractoR) # Step 1: get URLs top_cats <- find_thread_urls(subreddit = "cats", sort_by = "top", period = "month") # Step 2: retrieve content for the top 3 threads content <- get_thread_content(top_cats$url[1:3]) # Inspect thread metadata str(content$threads) # Inspect comments str(content$comments) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.