App Pitch for Coursera Project

Reproducible Pitch Presentation

My Shiny app analyses text and outputs a summary analysis.

Warning: if you are a fan of the The Beatles, there’s an Easter egg.

How It Works

The app is very simple to use:

  • The user will input some text.

  • The app will return the number of characters in the text, the number of words, a word frequency table, and more importantly, the number of times a specific word appears.

  • If the word “walrus” appears, there will be an opportunity to sing.

  • The user will get to decide if they feel like singing or not, and depending on their answer, the chorus of a song will appear.

User Interface

After the user types some text, the app will analyse it. If the word “walrus” is in the text, some buttons will appear asking if the user would like to sing.

library(shiny)
library(shinythemes)
ui <- fluidPage(
        theme = shinythemes::shinytheme("journal"),  
        titlePanel(h1("Are there walruses in your text?", align = "center")), 
        fluidRow(
                column(6, offset = 1,  
                       wellPanel(
                               textAreaInput("text_input", "Enter your text:", rows = 5, placeholder = "Type some text here, perhaps with a walrus or two if you like The Beatles...")
                       ),
                       br(),
                       h4("Word Frequency", align = "center"),
                       tableOutput("word_frequency")
                ),
                
                column(4,  
                       h3("Analysis Results", align = "center"),
                       br(),
                       htmlOutput("char_count"),
                       htmlOutput("word_count"),
                       br(),
                       htmlOutput("walrus_count"),
                       br(),
                       uiOutput("sing_button"),
                       br(),
                       uiOutput("lyrics")
                )
        )
)

Server Logic

The server logic renders all the elements described previously using reactive expressions.

server <- function(input, output) {
        analyzeText <- reactive({
                text <- input$text_input
                words <- unlist(strsplit(tolower(gsub("[^[:alnum:] ]", "", text)), "\\s+"))
                words <- words[words != ""]
                char_count <- nchar(text)
                word_count <- length(words)
                word_frequency <- sort(table(words), decreasing = TRUE)
                walrus_count <- sum(words == "walrus")
                
                list(
                        char_count = char_count,
                        word_count = word_count,
                        word_frequency = word_frequency,
                        walrus_count = walrus_count  
                )
        })
        output$char_count <- renderUI({
                HTML(paste("<strong style='color: black;'>Number of characters:</strong>", analyzeText()$char_count))
        })
        output$word_count <- renderUI({
                HTML(paste("<strong style='color: green;'>Number of words:</strong>", analyzeText()$word_count))
        })
        output$word_frequency <- renderTable({
                word_freq <- analyzeText()$word_frequency
                data.frame(
                        Word = names(word_freq),
                        Frequency = as.integer(word_freq),
                        row.names = NULL
                )
        })
        output$walrus_count <- renderUI({
                walrus_count <- analyzeText()$walrus_count
                HTML(paste("<strong style='color: purple;'>Walrus counter:</strong>", walrus_count))
        })
        
        output$sing_button <- renderUI({
                walrus_count <- analyzeText()$walrus_count
                if (walrus_count >= 1) {
                        tagList(
                                h4("Would you like to sing a chorus?", align = "center"),
                                radioButtons("sing", label = NULL, choices = c("Yes", "No thanks, I already sang yesterday."), selected = "No", inline = FALSE)
                        )
                }
        })
        
        output$lyrics <- renderUI({
                if (!is.null(input$sing) && input$sing == "Yes") {
                        HTML("<p style='color: darkred; font-size: 18px; text-align: center;'>I am the eggman/They are the eggmen/I am the walrus/Goo goo g' joob g' goo goo g' joob!</p>")
                }
        })
}

Recap

To conclude, this simple Shiny app performs text analysis by:

  • counting the number of characters overall,

  • counting the number of words (character strings separated by spaces),

  • producing a word frequency table organized in descending order,

  • counts the number of times the word “walrus” appears,

  • on the condition that the word “walrus” is in the text written by the user, some options will appear.