Search code examples
rshinyshiny-server

How does the shiny package work?


i am new to shiny package i am trying to use an example available in githube: https://github.com/dipanjanS/MyShinyApps/tree/master/twitter-analysis

it keeps giving me errors also i did not understand how the connection between the server side and user side i will explain the example: it is an application that take a word from the user then collect tweets related to that word then caculate the sentment analysis, word cloud and view the collected tweets. first is the user side there will be a text input where the user enters keyword:

 textInput("entity1", "Handle 1: ","#thrilled"),

there will be three tabs in the user interface:

    tabPanel("Sentiment Analysis", plotOutput("sentiboxplot"),
   tabPanel("Word Clouds",h2(textOutput("entity1wc")),plotOutput("entity1wcplot"),h2(textOutput("entity2wc")),plotOutput("entity2wcplot")),
tabPanel("Entity 1 Raw tweets",tableOutput("tableentity1"))

The server side get the inputs from the user side it start by collecting tweets, calculate the sentiment then plot a graph, produce the words cloud, and view the tweets. as commented in the code the excution should start in this sequence:

1- entity1<-reactive({ if(input$actb>=0 ){
withProgress(session, min=1, max=15, expr={
for(i in 1:15) {
setProgress(message = '1- Calculation in progress',
detail = 'This may take a while...',
value=i)
Sys.sleep(0.1)
}
})}
entity1<-TweetFrame(input$entity1, input$maxTweets)}
)

2- entityscores<-reactive({
if(input$actb>=0 ){
withProgress(session, min=1, max=15, expr={
for(i in 1:15) {
setProgress(message = '2- Calculation in progress',
detail = 'This may take a while...',
value=i)
Sys.sleep(0.1)
}
})}
entityscores<-sentimentalanalysis(entity1()$text,entity2()$text,input$entity1,input$entity2)})

3- output$notweets<-renderPrint({
if(input$actb>=0 ){
withProgress(session, min=1, max=15, expr={
for(i in 1:15) {
setProgress(message = '3-Calculation in progress',
detail = 'This may take a while...',
value=i)
Sys.sleep(0.1)
}
})}
numoftweets(entity1(),entity2(),input$entity1,input$entity2)})

4- output$sentiboxplot<-renderPlot({
if(input$actb>=0 ){
withProgress(session, min=1, max=15, expr={
for(i in 1:15) {
setProgress(message = '4-Calculation in progress',
detail = 'This may take a while...',
value=i)
Sys.sleep(0.1)
}
})}
cutoff <- data.frame(yintercept=0, cutoff=factor(0))
sentiboxplot<-ggplot(entityscores(),aes(x=size,y=score))+
facet_grid(entity ~ .)+
geom_point(color = "black",size = 2, alpha = 1/2)+
geom_smooth(method = "loess",se=FALSE,col='red',size=1.5, alpha = 0.7)+
geom_hline(aes(yintercept=yintercept, linetype=cutoff), data=cutoff)+
xlab('Tweet number')+
ylab('Sentiment Score')+
theme_bw()
print(sentiboxplot)})
output$sentiheadtable<-renderTable({tab<-head(entityscores(),4)})

5- output$entity1wcplot<-renderPlot({
if(input$actb>=0 ){
withProgress(session, min=1, max=15, expr={
for(i in 1:15) {
setProgress(message = '5-Calculation in progress',
detail = 'This may take a while...',
value=i)
Sys.sleep(0.1)
}
})}
wordcloudentity(entity1()$text)})
output$tableentity1 <- renderTable({tab<-entity1()[1]})
]
})

but this is not the case i numbered the message that is printed to the user in each step to see which one is working first i found that it is always start at 4. which mean that the pressed tab is calculated first which in my case the sentiment analysis tab. in other words if i pressed on tab 2 which display the words cloud it will start at 5. without considering the first step. can any one tell me why it works like this? Thank you in advance.


Solution

  • I found the answer. Once the app is running it will be in tab sentiment analysis which is step number 4; in the server side it require the tweets + the sentiment scores. So the sequence will be 4, 2, 1.

    When I hit tab 2 "words cloud" it will go in sequence 5 then 1 since the words cloud only need the tweets. And when I tab on tab 3 it will go to 1 than set it is simple but make a difference.