Hey!
So i made this function that automaticlly generates a wordcloud out of any wikipedia value that you choose!
It works great when i run it one by one, but for somereason, rvest::html() does'nt coerce inside a function.
I tried multiple manipulations, including using loops instead of function inside a function, but each time the code executed perfectly when i just ran it line by line, but in a function- NHA!
Error in open.connection(x, "rb") : HTTP error 404.
#require: magrittr, rvest, wordcloud2
wikicloud <- function(page="RStudio",
bg_color="white",
word_color=c("#75aadb","#4c4c4c"),
shape="circle",
remove_pattern = c("[.?]"),
remove_words = c("the","off","is","an","by","and","in","a","for","of","on","to")){
# Setting up function's arguments: Name of value in Wikipedia, colors and values to remove
for(i in 1:1){
web_link <- paste(c("https://en.wikipedia.org/wiki/"),page,sep = "")
raw_text=read_html(web_link) %>% html_nodes("p") %>%
html_text()
# Mining page's text
clean.text <- str_to_lower(str_remove_all(
unlist(str_split(raw_text,pattern = " ")),
pattern = remove_pattern))
# Cleanining page's text
words.freq <- data.frame(words=clean.text,freq=1) %>%
group_by(words) %>% summarise(f=sum(freq)) %>%
arrange(desc(f)) %>% filter(!words %in%remove_words)
# Creating a data frame for wordcloud2::
word_col <- sample(word_color,nrow(words.freq),T)
cloud=wordcloud2::wordcloud2(words.freq,
color = word_col,backgroundColor = bg_color,shape=shape)
##
print(cloud)
#rm(list=ls())# remove hashtag to remove enviornment
}
return(words.freq)
}