Hi all!
You all helped me out with my previous question so I was hoping to ask something similar.
I currently have an app that 'generates' recommendations for music, games, books, and tv. However, the books portion just does not seem to work.
Originally, I thought it might have been that the rvest
scraping was too taxing but I don't think that is the issue.
I can run the app locally as many times as I wish, but once deployed I keep seeing that there was an error.
Any help would be greatly appreciated.
My reprex is below!
library(shiny)
library(tidyverse)
library(stringr)
library(stringi)
library(rvest)
#> Loading required package: xml2
#>
#> Attaching package: 'rvest'
#> The following object is masked from 'package:purrr':
#>
#> pluck
#> The following object is masked from 'package:readr':
#>
#> guess_encoding
library(purrr)
library(collapsibleTree)
library(shinycssloaders)
library(dplyr)
library(forcats)
search_this <- function(x){
# Fake a search query
x <-
ifelse(
str_detect(x, " "),
paste0("https://www.goodreads.com/search?q=",
str_replace_all(x, " ", "+")),
paste0("https://www.goodreads.com/search?q=",
x)
)
x <-
x %>%
read_html() %>%
html_nodes("table a") %>%
html_attr("href") %>%
.[1]
# get link to book title
x <- paste0("https://www.goodreads.com",x)
x
}
##################FUNCTION NEW#####################
get_gp_link <- function(url){
# get recommendation links
gp_rec_links <-
url %>%
read_html() %>%
html_nodes(".cover a") %>%
html_attr("href")
}
################FUNCTION NEW#####################
get_p_rec_name <- function(urls){
# get names of recommendations
book_rec_names <-
urls %>%
read_html() %>%
html_nodes(".cover img") %>%
html_attr("alt")
book_parent_title <-
# get titles of parent books
urls %>%
read_html() %>%
html_nodes("#bookTitle") %>%
html_text(trim = T)
data.frame(book_parent_title,
book_rec_names)
}
ui <-
fluidPage(
titlePanel("GoodReads: Readers Also Liked",
windowTitle = "BookRecs"),
p("A tool for you to find your next book!"),
tags$a(href="https://www.goodreads.com/","All reccomendations sourced from GoodReads."),
br(),
hr(),
sidebarLayout(
sidebarPanel(
textInput(
inputId = "book_title",
label = "Name of Book",
placeholder = NULL,
width = "325px"
),
actionButton("search_book_title",
"Search Book Title",
icon = icon("book")),
br(),
br(),
em("Note: This will take around 1 minute to process."),
),
mainPanel(
collapsibleTreeOutput("book_text")
),
position = c("left", "right")
)
)
server <- function(input, output) {
######################Book Recommendations#################
x_book_title <- eventReactive(input$search_book_title, {
input$book_title
})
# Create function for text to render
output$book_text <- renderCollapsibleTree({
# Replace " " with "+"
gr_link <-
search_this(x_book_title())
book_name <-
gr_link %>%
read_html() %>%
html_nodes("#bookTitle") %>%
html_text(trim=T)
#
new_links <- get_gp_link(gr_link)
book_tree <- map_df(new_links,get_p_rec_name)
#Create Tree
collapsibleTree(
book_tree,
hierarchy = c("book_parent_title",
"book_rec_names"),
root = paste0(book_name)
)
})
}
shinyApp(ui, server)
#>
#> Listening on http://127.0.0.1:8403
Created on 2020-08-14 by the reprex package (v0.3.0)