Улучшение функции Barplot Regex и метки - PullRequest
0 голосов
/ 15 мая 2019

Можете ли вы помочь мне (i) привести в порядок код, чтобы 2 указанные строки регулярных выражений можно было сделать в одной, и (ii) заголовки можно расположить над столбиками (или, возможно, под углом), чтобы все они соответствовали.

##  Web Scraping DUP
##  to plot a barchart of poem reads

## Download the webpage

NoPoemsRead <- function(x){
  poems <- readLines(x)
  ## Extract out the title lines

  poem_title <- poems[grep("<h2", poems)[-1]]

  ## Extract out the number of reads lines

  poem_reads <- poems[grep("reads</small", poems)]

  ## De-clutter work space

  rm(poems)

  ## tidy up the data
  poem_reads <- unlist(lapply(poem_reads, function(x) as.numeric(gsub("[^0-9]+","",x))))

  ## the two lines below could be done in one
  poem_title <- unlist(lapply(poem_title, function(x) gsub("\t\t\t\t\t\t\t\t\t\t<.*?>", "", x)))
  poem_title <- unlist(lapply(poem_title, function(x) gsub("<.*?>", "", x)))

  names(poem_reads) <- poem_title

  barplot(poem_reads, las = 2,cex.names = 0.75, main = "The Number of Poems Read",
          ylab="Number of Reads", col = "skyblue")
}

NoPoemsRead("https://deepundergroundpoetry.com/poems-by/AnonymousBystander/")

1 Ответ

1 голос
/ 15 мая 2019

Это было бы немного чище, если бы использовалась обработка XML:

library(magrittr)
library(xml2)

u <- "https://deepundergroundpoetry.com/poems-by/AnonymousBystander/"
doc <- read_html(u)

titles <- doc %>%
  xml_find_all(".//h2") %>%
  xml_text %>%
  .[-1]

numbers <- doc %>% 
  xml_find_all(".//small") %>%
  xml_text %>%
  grep("reads.*", ., value = TRUE) %>%
  sub("reads.*", "", .) %>%
  as.numeric

barplot(numbers, names.arg = titles, las = 2, cex.names = 0.75, 
  main = "The Number of Poems Read", ylab="Number of Reads", col = "skyblue")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...