Decoding a Function in R

Viewed 65

I found this R Function that can be used to make a Social Network of Reddit Users : https://www.rdocumentation.org/packages/RedditExtractoR/versions/2.1.5/topics/user_network

I tried out this function and I was able to plot the network:

# install the older version of the library
devtools::install_version("RedditExtractoR", version = "2.1.5", repos = "http://cran.us.r-project.org")

library(dplyr)

library(RedditExtractoR)

target_urls <- reddit_urls(search_terms="cats", subreddit="Art", cn_threshold=50)

target_df <- target_urls %>% 
filter(num_comments==min(target_urls$num_comments)) %$% 
URL %>% reddit_content # get the contents of a small thread

network_list <- target_df %>% user_network(include_author=FALSE, agg=TRUE) # extract the network

network_list$plot 

enter image description here

Now, I am trying to figure out how this function ("reddit_content") works in the background.

The function "reddit_urls" returns a list of comments containing the term "cats" in the subrreddit "Art":

> head(target_urls)
      date num_comments                                     title subreddit                                                                                 URL
2 26-08-22          109                     Cat, me, pencil, 2022       Art                     http://www.reddit.com/r/Art/comments/wydak5/cat_me_pencil_2022/
5 24-06-22           51 Cats, me, pen on paper plus digital, 2022       Art http://www.reddit.com/r/Art/comments/vjz27e/cats_me_pen_on_paper_plus_digital_2022/
6 27-07-22          177              Cat operator,me,acrylic,2022       Art              http://www.reddit.com/r/Art/comments/w9983c/cat_operatormeacrylic2022/
7 25-07-22          166          Cat Study, Me, Digital Art, 2022       Art          http://www.reddit.com/r/Art/comments/w81ih8/cat_study_me_digital_art_2022/
8 30-06-22          492             Cat, me, pastel pencils, 2022       Art             http://www.reddit.com/r/Art/comments/voc43v/cat_me_pastel_pencils_2022/
9 08-08-22           83                Cat, me, pen and ink, 2022       Art                http://www.reddit.com/r/Art/comments/wiuhqx/cat_me_pen_and_ink_2022/

From here, the "reddit_content" function seems to be able to figure out who is writing the comment and who the comment is being directed to:

     test = target_urls[1,5] %>% reddit_content

head(test)

  id structure post_date comm_date num_comments subreddit upvote_prop post_score        author               user comment_score controversiality                                 comment                 title
1  1         1  26-08-22  26-08-22          109       Art        0.98      14101 ninadrawsalot  Something_Average           146                0           He needs some head pats stat! Cat, me, pencil, 2022
2  2       1_1  26-08-22  27-08-22          109       Art        0.98      14101 ninadrawsalot littlebitsofspider            25                0 *Someone snuggle that cat immediately!* Cat, me, pencil, 2022
3  3         2  26-08-22  26-08-22          109       Art        0.98      14101 ninadrawsalot           BrickGun           119                0        "What am I doing with my lives?" Cat, me, pencil, 2022

And finally, the "network_list" function is able to transform the above data set into a "graph network" format with edges/nodes (this is what is being visualized):

> network_list
$df
# A tibble: 3 x 4
  sender            receiver       count comment                        
  <chr>             <chr>          <dbl> <chr>                          
1 Amazing_SpiderLAN purr_in_ink        1 This so beautiful bud! Congrats
2 BeatlesTypeBeat   macmynameismac     1 Zoom in and you'll see why.    
3 purr_in_ink       elhomerjas         1 Thank you :)       

However, I am trying to figure out how the "reddit_content" function works. I tried using the "getAnywhere" command in R to look at the source code of the "reddit_content" function, but I still can't seem to understand how it works.

As an example:

    1. Suppose I take the first URL that showed up: http://www.reddit.com/r/Art/comments/wydak5/cat_me_pencil_2022/
    1. Then, I take the first comment that shows up on this Reddit post : Something_Average (26 days ago) He needs some head pats stat!
    1. I can now find out the exact link to this comment by using the Reddit Pushshift API :

API Request: https://api.pushshift.io/reddit/comment/search?limit=250&q=*&link_id=wydak5

Reddit Comment Link: https://www.reddit.com/r/Art/comments/wydak5/comment/ilwj4oq/

  1. Next, I can use the "reddit_content" function to get information about this comment:

Below is an example:

 "https://www.reddit.com/r/Art/comments/wydak5/comment/ilwj4oq/" %>% reddit_content # get the contents of a small thread
  |==================================================================================================================================================================================================================| 100%
  id structure post_date comm_date num_comments subreddit upvote_prop post_score        author               user comment_score controversiality                                 comment                 title post_text
1  1         1  26-08-22  26-08-22          109       Art        0.98      14098 ninadrawsalot  Something_Average           143                0           He needs some head pats stat! Cat, me, pencil, 2022          
2  2       1_1  26-08-22  27-08-22          109       Art        0.98      14098 ninadrawsalot littlebitsofspider            25                0 *Someone snuggle that cat immediately!* Cat, me, pencil, 2022          
                                 link    domain                                                                            URL
1 https://i.redd.it/j8rx9llv13k91.jpg i.redd.it https://www.reddit.com/r/Art/comments/wydak5/comment/ilwj4oq/?ref=search_posts
2 https://i.redd.it/j8rx9llv13k91.jpg i.redd.it https://www.reddit.com/r/Art/comments/wydak5/comment/ilwj4oq/?ref=search_posts

But in the end - I still haven't understood the overall process. If I start with this comment here "https://www.reddit.com/r/Art/comments/wydak5/comment/ilwj4oq/" - how can I figure out who wrote this comment and who is the comment being directed at? If I have a list of many Reddit comments - how can I find out who is writing the comment and to whom the comment is being directed to, and thus create a network visualization?

Thank you!

0 Answers
Related