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:
Here is a screenshot of R Shiny Apps:


Container event from container-9161944: oom (out of memory)-- max instance size for the free plan is 1GB. You could check from shiniyapps.io app settings if it's using largest available instance, but if the process takes about 5s on your local system, it probably will not fit into 1GB limit and you'd need to reduce (peak) memory usage. What's the size of the loaded dataset? You are keeping 3 copies of it in your server code. As a 1st step, I'd move bigram generation from Shiny to a locally executed script that storesnewsBigramsas RDS, which then gets deployed and loaded in the app.