I'm experiencing a strange issue where the plot loads fine for the first query, but then begins to load the plot from the previous query. (I.e., a user inputs "go to" and the plot doesn't change. The user then inputs "an incredible" and the plot changes to the plot for "go to". Next, the user inputs for "what do" and the plot changes to the plot for "an incredible").
The app works totally fine on my local machine (R version 3.1.0 (2014-04-10), Platform: x86_64-apple-darwin10.8.0 (64-bit), shinyapps_0.3.63, shiny_0.10.2.1). I never see the issue that's occurring on the shinyapps server.
I thought it was probably related to lexical scoping, and I tried a bunch of different fixes (e.g, removing variables from memory after the plot is drawn or not making them global in the first place, etc.) but nothing worked.
library(shiny)
library(shinyapps)
library(RMySQL)
algo <- function(ngram){
word.ct <- strsplit(ngram, split=" ")
trigram <- function(x){
match <- regexpr("([a-zA-Z0-9[:punct:]]+[[:space:]]*){3}$", x)
tri <- regmatches(x, match)
if (grepl("([a-zA-Z0-9[:punct:]]+[[:space:]]+){3}$", tri) == FALSE){
tri <- paste(tri, "")
}
tri <- gsub("'", "\\'", tri, fixed=TRUE)
return(tri)
}
bigram <- function(x){
match <- regexpr("([a-zA-Z0-9[:punct:]]+[[:space:]]*){2}$", x)
bi <- regmatches(x, match)
if (grepl("([a-zA-Z0-9[:punct:]]+[[:space:]]+){2}$", bi) == FALSE){
bi <- paste(bi, "")
}
bi <- gsub("'", "\\'", bi, fixed=TRUE)
return(bi)
}
unigram <- function(x){
match <- regexpr("([a-zA-Z0-9[:punct:]]+[[:space:]]*){1}$", x)
uni <- regmatches(x, match)
if (grepl("([a-zA-Z0-9[:punct:]]+[[:space:]]+){1}$", uni) == FALSE){
uni <- paste(uni, "")
}
uni <- gsub("'", "\\'", uni, fixed=TRUE)
return(uni)
}
unigram2 <<- function(x){
match <- regexpr("([a-zA-Z0-9[:punct:]]+[[:space:]]*){1}$", x)
uni <- regmatches(x, match)
# if (grepl("([a-zA-Z0-9[:punct:]]+[[:space:]]+){1}$", uni) == FALSE){
# uni <- paste(uni, "")
# }
# uni <- gsub("'", "\\'", uni, fixed=TRUE)
return(uni)
}
tri.pred <- function(z){
con <- dbConnect(MySQL(),
user=,
password=,
host=,
dbname=
)
expr <- paste("^", bigram(z), sep="")
stmt <- paste("SELECT tri, freq FROM trigrams WHERE (tri REGEXP BINARY'", expr, "');", sep="")
t.values <<- dbGetQuery(conn = con, statement = stmt)
#print("trigrams loaded")
dbDisconnect(con)
if (identical(t.values[1,1], NULL)){
#print("backing off to bigrams")
wordz <<- c("is", "to", "of")
bi.pred(z)
} else {
word <- unigram2(t.values[1,1])
wordz <<- c(unigram2(t.values[2,1]), unigram2(t.values[3,1]), unigram2(t.values[4,1]))
return(word)
}
}
bi.pred <- function(w){
con <- dbConnect(MySQL(),
user=,
password=,
host=,
dbname=
)
expr <- paste("^", unigram(w), sep="")
stmt <- paste("SELECT bi, freq FROM bigrams WHERE (bi REGEXP BINARY '", expr, "');", sep="")
b.values <<- dbGetQuery(conn = con, statement = stmt)
# print("bigrams loaded")
dbDisconnect(con)
if(identical(b.values[1,1], NULL)){
## rm(bi.comb.3)
wordz <<- c("is", "to", "of")
return("the")
} else {
word <- unigram2(b.values[1,1])
wordz <<- c(unigram2(b.values[2,1]), unigram2(b.values[3,1]), unigram2(t.values[4,1]))
return(word)
}
}
if (length(unlist(word.ct)) >= 2){
tri.pred(ngram)
} else {
bi.pred(ngram)
}
}
shinyServer(
function(input, output) {
output$word <- renderPrint({
input$Submit
if (input$Submit == 0)
return(print("Waiting for input..."))
ngram <- isolate(input$text)
return(algo(ngram))
})
output$other <- renderPrint({
input$Submit
if (input$Submit == 0)
return(print("Waiting for input..."))
return(wordz)
})
output$hist <- renderPlot({
if (input$Submit == 0){
return(print("Waiting for input..."))
} else {
if (wordz == c("is", "to", "of")){
bp <- NULL
} else if (exists("t.values")){
if (length(t.values[,2]) > 20){
bp <- barplot(t.values[1:20,2]/sum(t.values[,2]), main="Highest probability words", xaxt="n", ylab="Probability", col="cornflowerblue")
axis(1, labels=lapply(t.values[1:20,1], unigram2), at=bp, las=2)
} else {
bp <- barplot(t.values[,2]/sum(t.values[,2]), main="Highest probability words", xaxt="n", ylab="Probability", col="cornflowerblue")
axis(1, labels=lapply(t.values[,1], unigram2), at=bp, las=2)
}
} else {
if (length(b.values[,2] > 20)){
bp <- barplot(b.values[1:20,2]/sum(b.values[,2]), main="Highest probability words", xaxt="n", ylab="Probability", col="cornflowerblue")
axis(1, labels=lapply(b.values[1:20,1], unigram2), at=bp, las=2)
} else {
bp <- barplot(b.values[,2]/sum(b.values[,2]), main="Highest probability words", xaxt="n", ylab="Probability", col="cornflowerblue")
axis(1, labels=lapply(b.values[,1], unigram2), at=bp, las=2)
}
}
}
})
}
)