-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment_app_reactive.R
92 lines (62 loc) · 1.91 KB
/
sentiment_app_reactive.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
library(shiny)
library(dplyr)
library(ggplot2)
library(sentimentr)
dat_reviews <- read.csv("dat_reviews.csv")
attach(dat_reviews)
# Define UI for app -----------------------------------------------------------
ui <- fluidPage(
titlePanel("Sentiment Analyzer"),
# tags$style("#text {font-size:20px;
# color:black;
# display:block;
# font-style:bold;}"),
sidebarLayout(
sidebarPanel(
tableOutput("text"),
tableOutput("textplot"),
actionButton("button", "Analyze Text")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Bar Plot", plotOutput("barplot")),
tabPanel("Density", plotOutput("plot"))
# tabPanel("Text Plot", plotOutput("textplot"))
)
)
)
)
#Define server -------------------------------------------------------------
server <- function(input, output, session) {
#Begin sentiment analysis
dfInput <- reactive({dat_reviews %>%
get_sentences() %>%
sentiment() -> dat_reviews_senti
return(dat_reviews_senti)
})
output$plot <- renderPlot({
ggplot(dfInput()) + geom_density(aes(sentiment))
})
output$barplot <- renderPlot({
dfInput()%>%
mutate(sentiment_scores = ifelse(sentiment > 0, "Positive", "Negative")) %>%
count(cohort, sentiment_scores) %>%
ggplot() + geom_col(aes(y = cohort, x = n, fill = sentiment_scores))
})
output$text <- renderTable({
dfInput() %>%
count(ifelse(sentiment > 0, "Positive", "Negative")) %>%
as.data.frame()
})
text_an <- eventReactive(input$button, {
sentiment_by(dfInput()) %>%
get_sentences() %>%
sentiment_by() %>%
highlight()
})
output$textplot <- renderDataTable({
table(text_an())
})
}
#Create a shiny appp object -------------------------------------------------
shinyApp(ui = ui, server = server)