Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

162 lines
4.8KB

  1. #
  2. # This is the server logic of a Shiny web application. You can run the
  3. # application by clicking 'Run App' above.
  4. #
  5. # Find out more about building applications with Shiny here:
  6. #
  7. # http://shiny.rstudio.com/
  8. #
  9. library(shiny)
  10. library(rvest)
  11. googleme_url <- function(first, last) {
  12. paste0("https://www.google.com/#q=%22", first, "+", last, "%22")
  13. }
  14. googleme <- function(first, last) {
  15. paste0('<a href="', googleme_url(first, last),
  16. '" target="_blank"',
  17. #ifelse(is.null(class), "", paste0('class="',class,'"'),
  18. '>',
  19. first, " ", last,"</a>")
  20. }
  21. # Define server logic required to draw a histogram
  22. shinyServer(function(input, output, session) {
  23. surname.this <- reactive({
  24. surnames.final %>% filter(surname == input$surname)
  25. })
  26. surname.choices <- reactive({
  27. if (input$cluster == 'All') {
  28. surname_choices <- surnames.final$surname
  29. } else if (input$cluster == "Favorites - Ryan") {
  30. load('likes_ryan.RData')
  31. surname_choices <- likes_ryan
  32. } else if (input$cluster == "Favorites - Garrick") {
  33. load('likes_garrick.RData')
  34. surname_choices <- likes_garrick
  35. } else {
  36. surname_choices <- surnames.final %>%
  37. filter(cluster_name == input$cluster) %>% .$surname
  38. }
  39. if (length(surname_choices) < 1) {
  40. surnames.final$surname
  41. } else {
  42. surname_choices
  43. }
  44. })
  45. # ---- Sidebar ----
  46. observe({
  47. updateSelectizeInput(session, 'surname', choices = surname.choices())
  48. })
  49. observeEvent(input$next_name, {
  50. surname_choices <- surname.choices()
  51. chosen <- which(input$surname == surname_choices)
  52. if (chosen == length(surname_choices)) chosen <- 0
  53. updateSelectizeInput(session, 'surname', selected = surname_choices[chosen+1])
  54. })
  55. observeEvent(input$random_name, {
  56. surname_choices <- surname.choices()
  57. chosen <- sample(1:length(surname_choices), 1)
  58. updateSelectizeInput(session, 'surname', selected = surname_choices[chosen])
  59. })
  60. observeEvent(input$like, {
  61. if (input$user == "Ryan") {
  62. load('likes_ryan.RData')
  63. likes_ryan <- union(likes_ryan, input$surname)
  64. save(likes_ryan, file = 'likes_ryan.RData')
  65. } else if (input$user == 'Garrick') {
  66. load('likes_garrick.RData')
  67. likes_garrick <- union(likes_garrick, input$surname)
  68. save(likes_garrick, file = 'likes_garrick.RData')
  69. }
  70. })
  71. # ---- Display ----
  72. output$t_header <- renderText({
  73. paste("Hi, <span class='text-danger'>",
  74. googleme(input$user, input$surname),
  75. "</span>")
  76. })
  77. output$t_othername <- renderText({
  78. us <- c('Garrick', 'Ryan')
  79. baby_text <- ""
  80. verb <- "is"
  81. if (input$baby != "") {
  82. verb <- "are"
  83. baby_text <- paste0(" and little <span class='text-info'>",
  84. googleme(input$baby, input$surname), "</span>")
  85. }
  86. paste0("How ", verb,
  87. " <span class='text-info'>",
  88. googleme(us[which(us != input$user)], input$surname),
  89. "</span>", baby_text, "?")
  90. })
  91. output$t_body <- renderText({
  92. match_score <- round(surname.this()$mean*100, 1)
  93. rnk_surname <- surname.this()$rank
  94. pop_surname <- surname.this()$count
  95. paste0("The last name ",
  96. "<strong class='text-danger'>",
  97. input$surname,
  98. "</strong>",
  99. " scored ",
  100. "<strong class='text-info'>",
  101. match_score,
  102. "%</strong>",
  103. " in the match score,",
  104. " is shared by approximately ",
  105. "<strong class='text-info'>",
  106. formatC(pop_surname, format = 'd', big.mark = ','),
  107. "</strong>",
  108. " people,",
  109. " and is the ",
  110. "<strong class='text-info'>",
  111. formatC(rnk_surname, format = 'd', big.mark = ','),
  112. "th</strong>",
  113. " most popoular name in the US",
  114. ".")
  115. })
  116. output$p_radar <- renderPlot({
  117. if (is.null(input$surname) | input$surname == "") return(NULL)
  118. plot.new()
  119. par(mfrow = c(1, 2), mai = c(0.1, 0.1, 0.3, 0.1))
  120. surname_data <- surname.this()
  121. radarplot(surname_data, TRUE)
  122. radarplot(surname_data, FALSE)
  123. })
  124. output$link_forebears <- renderText({
  125. paste0('Read more on <a href=http://forebears.io/surnames/',
  126. tolower(input$surname), ' target="_blank">forebears.io...</a>')
  127. })
  128. output$forebears_name <- renderText({
  129. url <- paste0('http://forebears.io/surnames/', tolower(input$surname))
  130. def <- read_html(url) %>%
  131. html_nodes(".surDesc p") %>%
  132. html_text() %>%
  133. paste(collapse = "</p><p>")
  134. if(def == "") return(NULL)
  135. paste0("<blockquote class='small'><p>", def, "</p></blockquote>")
  136. })
  137. output$t_surnames <- renderDataTable({
  138. surnames.final %>%
  139. filter(surname %in% surname.choices()) %>%
  140. select(-cluster, -cluster_name, -prop100k, -cum_prop100k)
  141. }, options = list(pageLength = 15))
  142. })