R Shiny not deploying correctly on Shiny Apps

46 Views Asked by At

I'm building this Shiny app that perfectly works fine inside R Studio, but once is published on Shiny Apps, it does not work.

The app should predict a second word based on a first one, using bi grams. Inside R Studio, it takes around 5 seconds to predict it. But once deployed on Shiny Apps, it goes 20 secs and then it disconnects from the server.

Here is the code:

library(shiny)
library(NLP)
library(tibble)
library(tidytext)
library(dplyr)
library(stringr)

ui <- fluidPage(

    titlePanel("Word Prediction with n-Grams by Humberto Renteria - Data Science Capstone"),

    sidebarLayout(
        sidebarPanel(
          textInput("name", "Please enter the word to predict"),
          actionButton("do", "Predict!")
        ),

        mainPanel(
          textOutput("distPlot")
        )
    )
)

server <- function(input, output) {
  
  news_text <- readLines(file("en_US.news.txt", open="r"))
  newsLinesDF <- data_frame(line = 1:length(news_text), text = news_text)
  newsBigrams <- newsLinesDF %>% unnest_tokens(bigram, 
                                               text, token = "ngrams", n = 2)
  
  prediction <- eventReactive(input$do, {
    word_to_start_with <- input$name
    
    last_word <- str_extract(word_to_start_with, "\\b\\w+\\b$")
    
    result <- newsBigrams %>%
      filter(str_detect(bigram, paste0("^", last_word, "\\b"))) %>%
      mutate(second_word = str_extract(bigram, "\\b\\w+\\b")) %>%
      arrange(line) %>%
      slice(1) %>%
      pull(bigram)
    
    return(result)
  })
  
  output$distPlot <- renderText({
    prediction()
  })
}

# Run the application 
shinyApp(ui = ui, server = server)


Here is a screenshot of R Studio:

enter image description here

Here is a screenshot of R Shiny Apps:

enter image description here

0

There are 0 best solutions below