Last active
April 29, 2024 00:05
-
-
Save erikgregorywebb/96449eb904b5ff4bd96b67c801ba50fc to your computer and use it in GitHub Desktop.
This file contains 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
# import libraries | |
library(tidyverse) | |
library(scriptuRs) | |
library(tidytext) | |
library(shiny) | |
library(shinythemes) | |
library(rsconnect) | |
# import scriptures | |
scriptures = scriptuRs::lds_scriptures() | |
glimpse(scriptures) | |
# unnest verses to words | |
scripture_words = scriptures %>% unnest_tokens(word, text) | |
glimpse(scripture_words) | |
# define search function | |
scripture_search = function(search_term) { | |
verses = scripture_words %>% | |
filter(word == search_term) %>% | |
distinct(verse_short_title) %>% pull(verse_short_title) | |
return(verses) | |
} | |
# define table preview function | |
scripture_table = function(verses) { | |
table = scriptures %>% | |
filter(verse_short_title %in% verses) %>% | |
select(volume_title, verse_title, text) %>% | |
arrange(volume_title) %>% | |
mutate(num = row_number()) %>% | |
select(num, volume_title, verse_title, text) %>% | |
rename(`No.` = num, Book = volume_title, Verse = verse_title, Text = text) | |
return(table) | |
} | |
# define summary table function | |
summary_table = function(verses) { | |
table = scriptures %>% | |
filter(verse_short_title %in% verses) %>% | |
group_by(volume_title) %>% count() %>% | |
rename(Book = volume_title, `Verse Count` = n) %>% ungroup() | |
total_verses = sum(table$`Verse Count`) | |
table = table %>% add_row(Book = 'Total', `Verse Count` = total_verses) | |
return(table) | |
} | |
# build shiny app | |
ui <- fluidPage(theme = shinytheme("superhero"), | |
tags$h1( | |
tags$style(HTML(" | |
@import url('//fonts.googleapis.com/css?family=Roboto|Cabin:400,700'); | |
h1 { | |
font-weight: 500; | |
line-height: 1.1; | |
color: #ffa200; | |
} | |
")) | |
), | |
h1("Scripture Search"), | |
HTML("<h4> Quickly count and identify words across the <a href='https://www.churchofjesuschrist.org/study/manual/gospel-topics/standard-works?lang=eng'>LDS Standard Works</a></h4>"), | |
HTML("<h6>Powered by <a href = 'https://github.com/andrewheiss/scriptuRs'>scriptuRs</a>, created by @andrewheiss | Shiny app by @erikgregorywebb</h6>"), | |
mainPanel( | |
# input field | |
textInput("user_text", label = "Enter a search term (single, lowercase word)", width = '600px', placeholder = "faith | gospel | sacrifice | savior | sin | rock | love"), | |
# submit button | |
actionButton("submit", label = "Search"), | |
br(), | |
br(), | |
# display summary table | |
textOutput("summary_title"), | |
tableOutput("summary_table"), | |
# display verses table | |
textOutput("detail_title"), | |
tableOutput("verses_table") | |
) | |
) | |
server <- function(input, output) { | |
# reactive expression | |
input_reactive <- eventReactive(input$submit, { | |
input$user_text | |
}) | |
output$summary_table <- renderTable({ | |
verses = scripture_search(input_reactive()) | |
summary_table(verses) | |
}, striped = F, hover = TRUE, bordered = TRUE, spacing = 'xs', align = 'l', width = '300px') | |
output$verses_table <- renderTable({ | |
verses = scripture_search(input_reactive()) | |
scripture_table(verses) | |
}, striped = F, hover = TRUE, bordered = TRUE, spacing = 'xs', align = 'l', width = '800px') | |
output$summary_title <- renderText({paste("Summary for ", "\"", input_reactive(), "\"", sep = '')}) | |
output$detail_title <- renderText({paste("Detail for ", "\"", input_reactive(), "\"", sep = '')}) | |
} | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment