Last active
January 14, 2019 13:38
-
-
Save JohnCoene/e0714798284157d145cc4b17fbad6e71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(DT) | |
| library(rvest) | |
| library(dplyr) | |
| library(shiny) | |
| library(purrr) | |
| library(tidytext) | |
| library(echarts4r) | |
| response <- httr::GET( | |
| "https://api.weforum.org/v1/articles?page%5Bnumber%5D=2&page%5Bsize%5D=100" | |
| ) | |
| content <- httr::content(response) | |
| titles <- content$data %>% | |
| map("attributes") %>% | |
| map("title") %>% | |
| unlist() | |
| link <- content$data %>% | |
| map("attributes") %>% | |
| map("website_endpoint") %>% | |
| unlist() | |
| data <- data.frame( | |
| title = titles, | |
| link = link | |
| ) | |
| ui <- fluidPage( | |
| theme = shinythemes::shinytheme("paper"), | |
| div( | |
| class = "container", | |
| h3("WEF Programming test"), | |
| p("Select an article to see a wordcloud of its content"), | |
| hr(), | |
| fluidRow( | |
| column( | |
| 3, DTOutput("table") | |
| ), | |
| column( | |
| 9, echarts4rOutput("cloud", height = "80vh") | |
| ) | |
| ) | |
| ) | |
| ) | |
| server <- function(input, output, session) { | |
| output$table <- renderDT({ | |
| data %>% | |
| mutate( | |
| title = paste0("<a href='", link, "' target='_blank'>", title, "</a>") | |
| ) %>% | |
| select(title) %>% | |
| datatable(options = list(dom = "p"), escape = FALSE, selection = "single") | |
| }) | |
| output$cloud <- renderEcharts4r({ | |
| req(input$table_rows_selected) | |
| data %>% | |
| slice(input$table_rows_selected) %>% | |
| pull(link) %>% | |
| as.character() %>% | |
| read_html() %>% | |
| html_nodes(".article-body") %>% | |
| html_text() %>% | |
| gsub("\n", "", .) %>% | |
| tibble( | |
| text = . | |
| ) %>% | |
| unnest_tokens(word, text) %>% | |
| anti_join(stop_words, by = "word") %>% | |
| count(word, sort = T) %>% | |
| slice(1:150) %>% | |
| e_color_range(n, color, colors = rev(c("#bf444c", "#d88273", "#f6efa6"))) %>% | |
| as.data.frame() %>% | |
| e_charts() %>% | |
| e_cloud(word, n, color) | |
| }) | |
| } | |
| shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment