Post

1 follower Follow
0
Avatar

Shiny app refuses to load .Rda

I created an app that predicts the next word given an input text. The app is supposed to load some data.tables, which are stored in a .Rda file (using the "load" function). and then source the code for the main functions from a .R file.

When I run the app (on my local machine), the UI appears but then it throws an error, stating that one of the files (which were supposed to be loaded) is not found. It looks like for some reason the app is skipping the "load" function. Can you help with ideas as to how to resolve this?

Following is the error I get and the code for ui.R, server.R and next_word_pred.R:

This is the error I get:

Listening on http://127.0.0.1:6727
Warning: Error in gsub: object 'unigrams2' not found
Stack trace (innermost first):
44: gsub
43: contractions [next_word_pred.R#17]
42: head
41: eval [next_word_pred.R#27]
40: eval
39: withVisible
38: source
37: server [C:\Users\yoav\Documents\1Personal\Yoav\Data Science\Capstone\App\nextWordTest1/server.R#14]
1: runApp
Error in gsub("^s\\s", "'s ", text) : object 'unigrams2' not found

This is my ui.R code:

# NextWord algorithm

library(shiny)

shinyUI(fluidPage(

# Application title
titlePanel("Let me predict the next word"),

# Sidebar with textr input for Phrase
sidebarLayout(
sidebarPanel(
textInput("phraseIn", "Type your text here:", ""),
submitButton("Predict")),

# Show the prediction for the next word
mainPanel(
h4("The next word could be one of these:"),
"By descending order of probability",
h3(tableOutput("next_word"))
)
)
))

This is my server.R code:

library(shiny)

load(file = "C:/Users/yoav/Documents/1Personal/Yoav/Data Science/Capstone/App/nextWordTest1/Data/ngrams1_6.rda")


# Define server for Next Word App
shinyServer(function(input,output,session) {
source("next_word_pred.R")
prediction <- reactive({
if(input$phraseIn == ""){
return("")
output$next_word <- renderText({as.character(prediction())})
}
else{
return( next_words_pred(input$phraseIn))}})
output$next_word <- renderTable({prediction()})

})

This is the code for next_word_pred.R:


library(data.table)
library(readtext)


num_top <- 5 # designate the number of predicted next words

# Function turns input text to lowercase and transforms it to a dataframe
string_df <- function(str) {
str <- tolower(str)
inSplit <- strsplit(str, split = " ")
inDF <- data.frame(words = unlist(inSplit))
return(inDF)
}

# Function to return contrations, which were removed as punctuation, to the text
contractions <- function(text) {
text <- gsub("^s\\s","\'s ",text)
text <- gsub("^n\\s","\'n ",text)
text <- gsub("^d\\s","\'d ",text)
text <- gsub("^t\\s","\'t ",text)
text <- gsub("^ve\\s","\'ve ",text)
text <- gsub("^ll\\s","\'ll ",text)
text <- gsub("^re\\s","\'re ",text)
return(text)
}


# Function to omit the first word of the text
shorten <- function(in_DF) {
inWords <- in_DF[2]
for (i in 3:length(in_DF))
inWords <- paste(inWords, in_DF[i])
i = i + 1
return (inWords)
}

# Function to find next word matches, using the prediction model
find_matches <- function(inWords) {
inWords <- inWords
inDF <- string_df(inWords)
numWords <- nrow(inDF)
if(numWords == 0){
return("You forgot to type in your text")
}
else {
if(numWords > 5){
inDF$words <- as.character(inDF$words)
inDF <- inDF[1:NROW(inDF) > (numWords-5),]
inWords <- paste(inDF[1], inDF[2], inDF[3], inDF[4], inDF[5] )
find_matches(inWords)
}
else{
# Create the subset data.table
start_inWords <- paste0('^',inWords,' ')
sub_DT <- data.table()

# subset 'n-gram' data table to find the most frequent occurrences
if(numWords==5)
sub_DT <- subset(setgrams2, grepl(start_inWords, setgrams2$Feature))
else
if(numWords==4)
sub_DT <- subset(quintgrams2, grepl(start_inWords, quintgrams2$Feature))
else
if(numWords==3)
sub_DT <- subset(quadgrams3, grepl(start_inWords, quadgrams3$Feature))
else
if(numWords==2)
sub_DT <- subset(trigrams3, grepl(start_inWords, trigrams3$Feature))
else
if(numWords==1)
sub_DT <- subset(bigrams5, grepl(start_inWords, bigrams5$Feature))


if(nrow(sub_DT) > 0) {
# if matches are found, return the num_top Words (without any spaces)
top_words <- head(contractions(gsub(paste0(tolower(inWords), " "),"",sub_DT$Feature)),num_top)
msg <<- sprintf("Next phrase was predicted with %1d-gram data table.",(numWords+1))
return( gsub("[[:space:]]"," ",top_words) )
}
else{
if(numWords > 0) {
inWords <- shorten(inDF$words)
find_matches(inWords)
}
else {
msg <<- paste("Next word not found in n-grams.\nReturning the",num_top,"most frequent words of uni-gram.")
top_uni <- head(contractions(unigrams2$Feature),num_top) # Top 5 Unigrams
return(top_uni)
}
}
}}}

# Function tht takes in the input phrase and returns a 5:2 datatable
# containing the top 5 next word matches found, by descending frequency
next_words_pred <- function(inPhrase) {
top_list <- find_matches( inPhrase)
top_table <- data.frame(Index = 1:5, Next_Word = "")
top_table$Next_Word <- as.character(top_table$Next_Word)
for (i in 1:length(top_list))
top_table$Next_Word[i] <- as.character(top_list[i])
i = i+1
top_table <- top_table[1:length(top_list),]
names(top_table)[1] <- ""
return(top_table)
}

 

Yoav Pridor