html <- read_html(url)
table <- html |>
html_element("table") |>
html_table()
html$doc
html$node
table <- html$node |>
html_element("table") |>
html_table()
table <- html$doc |>
html_element("table") |>
html_table()
library(rvest)
url <- "https://web.archive.org/web/20220201012049/https://www.imdb.com/chart/top/"
html <- read_html(url)
table <- html |>
html_element("table") |>
html_table()
library(rvest)
url <- "https://domenicovistocco.it/rlab/"
html <- read_html(url)
table <- html |>
html_element("table") |>
html_table()
html
html_element(html)
library(rvest)
url <- "https://rvest.tidyverse.org/articles/starwars.html"
html <- read_html(url)
html$node
rvest::html_attrs(html)
rvest::html_element(html)
rvest::html_elements(html)
rvest::html_attrs(html)
rvest::html_children(html)
url <- "https://domenicovistocco.it/rlab/"
url <- "https://rvest.tidyverse.org/articles/starwars.html"
html <- read_html(url)
html
html_element(html, "h1")
html_element(html, "body")
url <- "https://domenicovistocco.it/rlab/"
html <- read_html(url)
tb_corso <- html_element(html, "table")
tb_corso <- html_element(html, "table") |> html_table()
View(tb_corso)
library(RSelenium)
# Installa i package necessari
install.packages("RSelenium")
library(RSelenium)
library(rvest)
# Avvia un browser Firefox automatizzato
driver <- rsDriver(browser = "firefox", chromever = NULL)
diamonds
help(diamonds)
diamanti <- diamonds
diamanti |>
group_by(clarity) |>
summarise(mean(price))
diamanti |>
group_by(clarity) |>
summarise(price_avg = mean(price))
diamanti |>
ggplot() +
aes(x = carat, y = price) +
geom_point()
diamanti |>
ggplot() +
aes(x = carat, y = price) +
geom_point() +
facet_wrap(facets = vars(clarity))
calcola_tabella <- function(tb_dati, var_gruppo, var_numerica){
out <- tb_dati |>
group_by(var_gruppo) |>
summarise(mean(var_numerica))
return(out)
}
calcola_tabella(diamanti, clarity, price)
calcola_tabella(diamanti, "clarity", "price")
calcola_tabella <- function(tb_dati, var_gruppo, var_numerica){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
out <- tb_dati |>
group_by(var_gruppo) |>
summarise(mean(var_numerica))
return(out)
}
calcola_tabella(diamanti, "clarity", "price")
calcola_tabella <- function(tb_dati, var_gruppo, var_numerica){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
out <- tb_dati |>
group_by({{var_gruppo}}) |>
summarise(mean({{var_numerica}}))
return(out)
}
calcola_tabella(diamanti, "clarity", "price")
calcola_tabella(diamanti, "cut", "price")
calcola_tabella <- function(tb_dati, var_gruppo, var_numerica){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
tb_dati |>
group_by({{var_gruppo}}) |>
summarise({{var_numerica}} := mean({{var_numerica}}))
}
calcola_tabella(diamanti, "clarity", "price")
calcola_tabella(diamanti, "clarity", "x")
tb_dati |>
group_by({{var_gruppo}}) |>
summarise({{var_numerica}}_avg := mean({{var_numerica}}))
calcola_tabella <- function(tb_dati, var_gruppo, var_numerica){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
tb_dati |>
group_by({{var_gruppo}}) |>
summarise("{{var_numerica}}_avg" := mean({{var_numerica}}))
}
calcola_tabella(diamanti, "clarity", "price")
calcola_tabella(diamanti, "clarity", "x")
calcola_tabella(diamanti, "color", "price")
calcola_tabella(diamanti, "color", "depth")
iris
head(iris)
calcola_tabella(iris, "Species", "Petal.Width")
calcola_tabella(iris, Species, Petal.Width)
select(iris, Species)
select(iris, "Species")
calcola_tabella_fun <- function(tb_dati, var_gruppo, var_numerica, fun = mean){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
tb_dati |>
group_by({{var_gruppo}}) |>
summarise("{{var_numerica}}_avg" := fun({{var_numerica}}))
}
calcola_tabella_fun(iris, Species, Petal.Width)
calcola_tabella_fun(iris, Species, Petal.Width, fun = mean)
calcola_tabella_fun(iris, Species, Petal.Width, fun = sd)
calcola_tabella_fun <- function(tb_dati, var_gruppo, var_numerica, fun = mean){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
tb_dati |>
group_by({{var_gruppo}}) |>
summarise("{{var_numerica}}_{{fun}}" := fun({{var_numerica}}))
}
calcola_tabella_fun(iris, Species, Petal.Width)
calcola_tabella_fun(iris, Species, Petal.Width, fun = sd)
crea_scatter <- function(tb_dati, var_x, var_y){
var_x <- ensym(var_x)
var_y <- ensym(var_y)
ggplot(data = tb_dati) +
aes(x = {{var_x}}, y = {{var_y}}) +
geom_point() +
ggthemes::theme_clean()
}
crea_scatter(iris, Sepal.length, Sepal.width)
crea_scatter(iris, Sepal.Length, Sepal.Width)
crea_scatter(iris, Petal.Length, Petal.Width)
crea_scatter(diamanti, depth, price)
crea_scatter(diamanti, carat, price)
crea_scatter_gruppo <- function(tb_dati, var_x, var_y, var_gruppo){
var_x <- ensym(var_x)
var_y <- ensym(var_y)
var_gruppo <- ensym(var_gruppo)
ggplot(data = tb_dati) +
aes(x = {{var_x}}, y = {{var_y}}) +
geom_point() +
facet_wrap(vars({{var_gruppo}}))
ggthemes::theme_clean()
}
crea_scatter(iris, Sepal.Length, Sepal.Width, Species)
crea_scatter_gruppo(iris, Sepal.Length, Sepal.Width, Species)
crea_scatter_gruppo <- function(tb_dati, var_x, var_y, var_gruppo){
var_x <- ensym(var_x)
var_y <- ensym(var_y)
var_gruppo <- ensym(var_gruppo)
ggplot(data = tb_dati) +
aes(x = {{var_x}}, y = {{var_y}}) +
geom_point() +
facet_wrap(vars({{var_gruppo}})) +
ggthemes::theme_clean()
}
crea_scatter_gruppo(iris, Sepal.Length, Sepal.Width, Species)
crea_scatter_gruppo(iris, Petal.Length, Petal.Width)
crea_scatter_gruppo(iris, Petal.Length, Petal.Width, Species)
crea_scatter_gruppo(diamanti, carat, price)
crea_scatter_gruppo(diamanti, carat, price, clarity)
# librerie utilizzate
library(tidyverse)
library(tidytext)
library(wordcloud2)
# lettura del file
promessi_sposi <- read_file("i_promessi_sposi.txt")
# elimino il carattere nascosto \r (carriage return)
promessi_sposi <- str_remove_all(promessi_sposi, "\r")
# elimino le virgolette
promessi_sposi <- str_remove_all(promessi_sposi, "\"")
# elimino i trattini
promessi_sposi <- str_remove_all(promessi_sposi, "-")
# divido la stringa in sfruttando la presenza di almeno 3 \n
capitoli <- str_split(promessi_sposi, "\n{3,}(?=CAPITOLO )")[[1]]
# estraggo i nomi dei capitoli
nomi_capitoli <- str_extract(capitoli, "^[^\n]+")
# e li rimuovo dal testo
capitoli <- str_remove(capitoli, "^[^\n]+\n+")
# costruisco una tabella con due colonne: nomi e contenuto del capitolo
promessi_sposi <- tibble(nomi_capitoli, capitoli)
# creo una tabella dividendo i capitoli per parole
promessi_tidy <- promessi_sposi |>
unnest_tokens("token", capitoli, token = "words")
# calcolo la tabella di frequenza di ciascun capitolo
# (lunghezza del capitolo come numero di parole)
promessi_tidy |>
count(nomi_capitoli) |>
arrange(desc(n)) |>
knitr::kable()
# rappresentazione grafica della lunghezza dei capitoli
promessi_tidy |>
count(nomi_capitoli) |>
mutate(nomi_capitoli = factor(nomi_capitoli)) |>
ggplot() +
aes(y = fct_reorder(nomi_capitoli, n), x = n) +
geom_col() +
theme_tufte() +
labs(x = "Numero di parole",
y = "",
title = "Analisi testuale dei Promessi Sposi",
subtitle = "Alessandro Manzoni",
caption = "Analisi ottenuta con R")
library(ggthemes)
# rappresentazione grafica della lunghezza dei capitoli
promessi_tidy |>
count(nomi_capitoli) |>
mutate(nomi_capitoli = factor(nomi_capitoli)) |>
ggplot() +
aes(y = fct_reorder(nomi_capitoli, n), x = n) +
geom_col() +
theme_tufte() +
labs(x = "Numero di parole",
y = "",
title = "Analisi testuale dei Promessi Sposi",
subtitle = "Alessandro Manzoni",
caption = "Analisi ottenuta con R")
# tabella delle stop words
ita_stopwords <- read_csv(
gzfile("sentiment-lang-italian-master/lexicon/exc.words.txt.gz"),
col_names = "word")
# tabella parole con sentiment positivo
ita_poswords <- read_csv(
gzfile("sentiment-lang-italian-master/lexicon/pos.words.txt.gz"),
col_names = "word")
# tabella parole con sentiment negativo
ita_negwords <- read_csv(
gzfile("sentiment-lang-italian-master/lexicon/neg.words.txt.gz"),
col_names = "word")
# elimino le stopword
promessi_clean <- anti_join(promessi_tidy,
ita_stopwords,
by = c("token" = "word"))
promessi_clean |>
count(token) |>
arrange(desc(n)) |>
slice_head(n = 10)
promessi_clean |>
group_by(nomi_capitoli) |>
count(token) |>
arrange(desc(n)) |>
slice_head(n = 5) |>
View()
promessi_clean |>
filter(nomi_capitoli == "CAPITOLO I") |>
count(token) |>
filter(n > 5) |>
arrange(desc(n)) |>
wordcloud2(shape = "pentagon")
promessi_clean
library(tidyverse)
help(diamonds)
diamanti <- diamonds
diamanti |>
group_by(clarity) |>
summarise(price_avg = mean(price))
diamanti |>
ggplot() +
aes(x = carat, y = price) +
geom_point() +
facet_wrap(facets = vars(clarity))
calcola_tabella <- function(tb_dati, var_gruppo, var_numerica){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
tb_dati |>
group_by({{var_gruppo}}) |>
summarise("{{var_numerica}}_avg" := mean({{var_numerica}}))
}
calcola_tabella(diamanti, "clarity", "price")
calcola_tabella(diamanti, "clarity", "x")
calcola_tabella(diamanti, "cut", "price")
calcola_tabella(diamanti, "color", "depth")
calcola_tabella(iris, "Species", "Petal.Width")
calcola_tabella(iris, Species, Petal.Width)
calcola_tabella_fun <- function(tb_dati, var_gruppo, var_numerica, fun = mean){
var_gruppo <- ensym(var_gruppo)
var_numerica <- ensym(var_numerica)
tb_dati |>
group_by({{var_gruppo}}) |>
summarise("{{var_numerica}}_{{fun}}" := fun({{var_numerica}}))
}
calcola_tabella_fun(iris, Species, Petal.Width)
calcola_tabella_fun(iris, Species, Petal.Width, fun = sd)
crea_scatter <- function(tb_dati, var_x, var_y){
var_x <- ensym(var_x)
var_y <- ensym(var_y)
ggplot(data = tb_dati) +
aes(x = {{var_x}}, y = {{var_y}}) +
geom_point() +
ggthemes::theme_clean()
}
crea_scatter(iris, Sepal.Length, Sepal.Width)
crea_scatter(iris, Petal.Length, Petal.Width)
crea_scatter(diamanti, carat, price)
crea_scatter_gruppo <- function(tb_dati, var_x, var_y, var_gruppo){
var_x <- ensym(var_x)
var_y <- ensym(var_y)
var_gruppo <- ensym(var_gruppo)
ggplot(data = tb_dati) +
aes(x = {{var_x}}, y = {{var_y}}) +
geom_point() +
facet_wrap(vars({{var_gruppo}})) +
ggthemes::theme_clean()
}
crea_scatter_gruppo(iris, Sepal.Length, Sepal.Width, Species)
crea_scatter_gruppo(iris, Petal.Length, Petal.Width, Species)
crea_scatter_gruppo(diamanti, carat, price, clarity)
promessi_bigrams <- promessi_tidy %>%
unnest_tokens(bigram, word, token = "ngrams", n = 2)
promessi_bigrams %>% count(bigram, sort = TRUE)
bigrams_separated <- atti_bigrams %>%
separate(bigram, c("word1", "word2"), sep = " ")
bigrams_filtered <- promessi_bigrams %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word)
bigram_counts <- bigrams_filtered %>%
count(word1, word2, sort = TRUE)
bigram_counts
ttt <- promessi_tidy %>%
group_by(nomi_capitoli) %>%
count(word) %>%
bind_tf_idf(word, nomi_capitoli, n) %>%
ungroup() %>%
group_by(word) %>%
summarise(tf_idf = sum(tf_idf)) %>%
ungroup() %>%
rename(freq = tf_idf) %>%
arrange(desc(freq))
common_words <- function(tbl_chapter_word, num_parole = 10){
tbl_plot <- tbl_chapter_word %>%
count(word) %>%
mutate(word = reorder(word, n))
tbl_plot %>%
arrange(desc(n)) %>%
top_n(num_parole) %>%
ggplot(aes(word, n)) +
geom_col(fill = "darkblue", width = 0.5) +
geom_text(aes(x = word, y = max(n) + 0.1 * max(n), label = n),
position = position_dodge(width = 1),
vjust = -0.5, size = 5, color = "black", fontface = "bold") +
coord_flip() +
theme_wsj() +
labs(title = "Atti degli Apostoli",
subtitle = "Le parole più comuni",
caption = "Fonte: Versione Ufficiale CEI 2008")
}
common_words_cap <- function(tbl_chapter_word, capitolo, num_parole = 10){
tbl_plot <- tbl_chapter_word %>%
group_by(nomi_capitoli) %>%
count(word) %>%
filter(nomi_capitoli == capitolo) %>%
mutate(word = reorder(word, n))
tbl_plot %>%
arrange(desc(n)) %>%
top_n(num_parole) %>%
ggplot(aes(word, n)) +
geom_col(fill = "darkblue", width = 0.5) +
geom_text(aes(x = word, y = max(n) + 0.1 * max(n), label = n),
position = position_dodge(width = 1),
vjust = -0.5, size = 5, color = "black", fontface = "bold") +
coord_flip() +
theme_wsj() +
labs(title = "Atti degli Apostoli",
subtitle = paste0(capitolo, ": le parole più comuni"),
caption = "Fonte: Versione Ufficiale CEI 2008")
}
cloud_words <- function(tbl_chapter_word, shape_cloud = "circle"){
require(wordcloud2)
tbl_plot <- tbl_chapter_word %>%
count(word)
tbl_plot %>%
rename(freq = n) %>%
wordcloud2(size = 1, shape = shape_cloud)
}
cloud_words_cap <- function(tbl_chapter_word, capitolo, shape_cloud = "circle"){
require(tm)
require(wordcloud2)
tbl_plot <- tbl_chapter_word %>%
group_by(nomi_capitoli) %>%
count(word) %>%
filter(nomi_capitoli == capitolo) %>%
ungroup()
tbl_plot %>%
select(-nomi_capitoli) %>%
rename(freq = n) %>%
wordcloud2(size = 1, shape = shape_cloud)
}
promessi_sposi <- read_file("i_promessi_sposi.txt")
promessi_sposi <- str_remove_all(promessi_sposi, "\\r")
promessi_sposi <- str_remove_all(promessi_sposi, "\"")
capitoli <- str_split(promessi_sposi, "\n{3,}(?=CAPITOLO )")[[1]]
nomi_capitoli <- str_extract(capitoli, "^[^\n]+")
capitoli <- str_remove(capitoli, "^[^\n]+\n+")
promessi_sposi <- tibble(nomi_capitoli, capitoli)
promessi_tidy <- promessi_sposi %>%
group_by(nomi_capitoli) %>%
unnest_tokens("word", capitoli) %>%
ungroup()
promessi_sposi %>%
mutate(lunghezza = str_length(capitoli),
nomi_capitoli = factor(nomi_capitoli, levels = nomi_capitoli)) %>%
select(-capitoli) %>%
ggplot(aes(forcats::fct_rev(nomi_capitoli), lunghezza)) +
geom_col(fill = "darkblue", width = 0.5) +
geom_text(aes(x = forcats::fct_rev(nomi_capitoli),
y = max(lunghezza) + 200,
label = lunghezza),
position = position_dodge(width = 1),
vjust = -0.5, size = 5, color = "black", fontface = "bold") +
coord_flip() +
theme_clean() +
labs(title = "Promessi Sposi",
subtitle = "Lunghezza dei capitoli: numero di parole",
caption = "Fonte: Versione Ufficiale",
y = "Lunghezza", x = "")
# tolgo le stop_words -----------------------------------------------------
## @knitr cancella_stop_words
ita_stop_words <- read_csv(gzfile("sentiment-lang-italian-master/lexicon/exc.words.txt.gz"),
col_names = "word")
# ??? aggiungere altre stop_words ???
ita_stop_words <- ita_stop_words %>%
bind_rows(tibble(word = c("nella", "li", "questo", "questa", "allora", "alla",
"come", "infatti", "ciò", "ad", "dunque", "dalla",
"anche", "queste", "quali", "dalle", "là", "poiché",
"delle", "dopo", "dai", "cui", "quando", "disse",
"dagli", "né", "mentre")))
promessi_tidy <- promessi_tidy %>% anti_join(ita_stop_words)
# le paroli più comuni negli atti -----------------------------------------
## @knitr atti_parole_comuni
common_words(promessi_tidy, num_parole = 30)
# nuvola di parola degli atti ---------------------------------------------
## @knitr atti_nuvole_parole
cloud_words(promessi_tidy)
# le parole più comuni per capitolo ---------------------------------------
## @knitr atti_capitoli
common_words_cap(promessi_tidy, "CAPITOLO I", num_parole = 12)
common_words_cap(promessi_tidy, "CAPITOLO II", num_parole = 12)
common_words_cap(promessi_tidy, "CAPITOLO III", num_parole = 12)
# nuvole di parola per capitoli -------------------------------------------
cloud_words_cap(promessi_tidy, "CAPITOLO I")
cloud_words_cap(promessi_tidy, "CAPITOLO II")
cloud_words_cap(promessi_tidy, "CAPITOLO III")
ttt <- promessi_tidy %>%
group_by(nomi_capitoli) %>%
count(word) %>%
bind_tf_idf(word, nomi_capitoli, n) %>%
ungroup() %>%
group_by(word) %>%
summarise(tf_idf = sum(tf_idf)) %>%
ungroup() %>%
rename(freq = tf_idf) %>%
arrange(desc(freq))
wordcloud2(ttt)
promessi_bigrams <- promessi_tidy %>%
unnest_tokens(bigram, word, token = "ngrams", n = 2)
promessi_bigrams %>% count(bigram, sort = TRUE)
bigrams_separated <- atti_bigrams %>%
separate(bigram, c("word1", "word2"), sep = " ")
bigrams_filtered <- promessi_bigrams %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word)
bigram_counts <- bigrams_filtered %>%
count(word1, word2, sort = TRUE)
bigram_counts
bigrams_united <- bigrams_filtered %>%
unite(bigram, word1, word2, sep = " ")
bigrams_united %>%
count(nomi_capitoli, bigram) %>%
bind_tf_idf(bigram, nomi_capitoli, n) %>%
arrange(desc(tf_idf))
bigram_counts
bigram_graph <- bigram_counts %>%
filter(n > 10) %>%
graph_from_data_frame()
bigram_graph
set.seed(2017)
ggraph(bigram_graph, layout = "fr") +
geom_edge_link() +
geom_node_point() +
geom_node_text(aes(label = name), vjust = 1, hjust = 1)
set.seed(2016)
a <- grid::arrow(type = "closed", length = unit(.15, "inches"))
ggraph(bigram_graph, layout = "fr") + geom_edge_link(aes(edge_alpha = n), show.legend = FALSE,
arrow = a, end_cap = circle(.07, 'inches')) + geom_node_point(color = "lightblue", size = 5) +
geom_node_text(aes(label = name), vjust = 1, hjust = 1) + theme_void()
a
bigram_graph
