R Shiny loop to display multiple plots

1,639 views
Skip to first unread message

Oumaima Ouali

unread,
Jun 20, 2016, 11:58:14 AM6/20/16
to Shiny - Web Framework for R

I have code that you can run below. I am trying to modify this code:

so that max_plots is dynamically set using input from the UI. The input from the UI is dynamic as well.

Here is my UI.R:



shinyUI(pageWithSidebar(



headerPanel("Domain"),



sidebarPanel(

textInput("n", "News domain", "cloud")

),



mainPanel(

# This is the dynamic UI for the plots

uiOutput("plots")

)

))

and here is my Server.R:

library(shiny)
library(shinySignals)
library(signal)
library(shinyIncubator)
library(ROAuth)
library(twitteR)
library(igraph)
library(stringr)
library(httr)
library(base64enc)


library(plotly)
library(rCharts)
library(knitr)
library(RColorBrewer)
library(dygraphs)
library("dplyr")
library(plyr)
library(sentiment)


library(ggplot2)
library(wordcloud)
library(RColorBrewer)
library(highcharter)
options(httr_oauth_cache=T)
api_key <- "lNmpoVQQ9xbDoQJh5Yvmhuzks"
api_secret <- "a9mSSOig38vSHN1MbU4waxMbzX0hDrAaG5a87k4fKsvbEYp11X"
access_token <- "708639762719096833-mrjnScIpfXPrYrcXCVni0qK9BoefBjG"
access_token_secret <- "WRW0LqnLDhfGhO8QNN7Z27JzQTI6zNB5O680NXnLuvC4L"
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
max_plots <- 5


shinyServer(function(input, output) {

# Insert the right number of plot output objects into the web page
for ( i in 1:10 ){
output$plots <- renderUI({


some_tweets = searchTwitter(input$n, n=1500, lang="en")

# get the text
some_txt = sapply(some_tweets, function(x) x$getText())

# remove retweet entities
some_txt = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "", some_txt)
# remove at people
some_txt = gsub("@\\w+", "", some_txt)
# remove punctuation
some_txt = gsub("[[:punct:]]", "", some_txt)
# remove numbers
some_txt = gsub("[[:digit:]]", "", some_txt)
# remove html links
some_txt = gsub("http\\w+", "", some_txt)
# remove unnecessary spaces
some_txt = gsub("[ \t]{2,}", "", some_txt)
some_txt = gsub("^\\s+|\\s+$", "", some_txt)

# 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="")


output[[plotname]] <- renderPlot({
ggplot(sent_df, aes(x=emotion)) +
geom_bar(aes(y=..count.., fill=emotion)) +
scale_fill_brewer(palette="Dark2") +
labs(x="emotion categories", y="number of tweets")

})
})
}


})



Every time I enter a new area I want a new graph display but I can't to do
Please help months I am a beginner in this field
Reply all
Reply to author
Forward
0 new messages