# define "tolower error handling" function try.error = function(x) { # create missing value y = NA # tryCatch error try_error = tryCatch(tolower(x), error=function(e) e) # if not an error if (!inherits(try_error, "error")) y = tolower(x) # result return(y) } # lower case using try.error with sapply some_txt = sapply(some_txt, try.error)
# remove NAs in some_txt some_txt = some_txt[!is.na(some_txt)] names(some_txt) = NULL # classify emotion class_emo = classify_emotion(some_txt, algorithm="bayes", prior=1.0) # get emotion best fit emotion = class_emo[,7] # substitute NA's by "unknown" emotion[is.na(emotion)] = "unknown"
# classify polarity class_pol = classify_polarity(some_txt, algorithm="bayes") # get polarity best fit polarity = class_pol[,4] # data frame with results sent_df = data.frame(text=some_txt, emotion=emotion, polarity=polarity, stringsAsFactors=FALSE)
# sort data frame sent_df = within(sent_df, emotion <- factor(emotion, levels=names(sort(table(emotion), decreasing=TRUE)))) #plot distribution of emotions
plotname <- paste("plot", i, sep="") plotOutput(plotname, height = 280, width = 250) }) # i++ } # Convert the list to a tagList - this is necessary for the list of items # to display properly.
# Call renderPlot for each one. Plots are only actually generated when they # are visible on the web page. for (i in 1:max_plots) { # Need local so that each item gets its own number. Without it, the value # of i in the renderPlot() will be the same across all instances, because # of when the expression is evaluated. local({ my_i <- i plotname <- paste("plot", my_i, sep="")