File size: 4,609 Bytes
526ed7e
dcdfe4c
 
 
 
 
526ed7e
dcdfe4c
 
 
526ed7e
 
 
dcdfe4c
 
 
 
 
 
 
 
 
 
 
 
526ed7e
 
dcdfe4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526ed7e
dcdfe4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526ed7e
 
dcdfe4c
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
library(shiny)
library(shinyalert)
library(shinythemes)
library(shinycssloaders)
library(shinyjs)
library(httr)
library(bslib)
library(thematic)
library(gtrendsR)
library(plotly)
library(dplyr)
library(ggplot2)

options(spinner.color = "lightblue",
        spinner.color.background = "#ffffff",
        spinner.size = 2)

my_theme <- bs_theme(
    bg = "#fdfefe",
    fg = "blue",
    primary = "red",
    base_font = font_google("PT Sans Caption"),
    "font-size-base" = "0.9rem",
    version = 5,
    "navbar-bg" = "blue"
)

thematic_shiny()

ui <- list(useShinyjs(),navbarPage(windowTitle = "TrendChecker",
                                   title = strong("TrendChecker"),theme = my_theme,
                                   tabPanel(title = strong("Trend Over Time"),icon = icon("chart-line"),
                                            sidebarLayout(
                                                sidebarPanel(width = 3,actionButton("info",strong("About TrendChecker",icon("info"))),hr(),
                                                             hidden(tags$div(id = "about",h5("TrendChecker is a web application that enables users to monitor the search popularity of any subject of interest over time,
                                                                                          and across different countries by calling the Google trend api. Search hit of 100 is the indicator of optimum popularity, while other hits are measured relative to the optimum."))),h4(strong("Controls")),hr(),
                                                             textInput("text",strong("Enter Search Term"),value = "Soccer"),
                                                             checkboxGroupInput("check",strong("Select Country(ies)"),choices = c("USA" = "US","UK" = "GB","Germany" = "DE","Netherlands" = "NL","Nigeria" = "NG","Japan" = "JP"),selected = "US"),
                                                             radioButtons("radio",strong("Choose Trend Source"),choices = c("Web","News","YouTube","Images"),selected = "News"),
                                                             radioButtons("time",strong("Select Time Frame"),choices = c("Last Hour","Last Four Hours","Last Day","Last Seven Days","Past 30 Days","Past 90 Days","Past 12 Months","Last Five Years"),selected = "Last Seven Days"),
                                                             actionButton("run",strong("Run Query"),icon("caret-right"))
                                                             ),
                                                mainPanel(
                                                          withSpinner(plotlyOutput("plot"),type = 8))
                                                          
                                            ))
)) 

# Define server logic required to run query

server <- function(input, output,session) {
  
## APP info button toggle activation
    
observeEvent(input$info,{
    toggle("about")
})    


## Create reactive input switch functionality
    
check_input <- reactive(input$check)
    

radio_input <- reactive(switch(input$radio,
                      "Web" = "web",
                      "News" = "news",
                      "YouTube" = "youtube",
                      "Images" = "images"))

radio_time <- reactive(switch(input$time,
                     "Last Hour" = "now 1-H",
                     "Last Four Hours" = "now 4-H",
                     "Last Day" = "now 1-d",
                     "Last Seven Days" = "now 7-d",
                     "Past 30 Days" = "today 1-m",
                     "Past 90 Days" = "today 3-m",
                     "Past 12 Months" = "today 12-m",
                     "Last Five Years" = "today+5-y"))


    
text_input <- reactive(input$text)




## Write the trend function

trend <- function(){
    
    gt <- gtrends(keyword = c(text_input()),geo = c(check_input()),gprop = radio_input(),
            time = radio_time())
    
    p <- plot(gt)
    
    gp <- ggplotly(p)
    
    return(gp)
}
    
## Convert to reactive function

trend2 <- reactive(trend())

## Create interactive plot

output$plot <- renderPlotly({
    
    withProgress(message = "Fetching data",
                 detail = "This may take a while...",value = 0,{
                     
                     for (i in 1:25){
                         
                         incProgress(1/25)
                         Sys.sleep(0.25)
                     }
                 })    
    
    input$run
    isolate(trend2())
    

})
       
}

# Run the application 
shinyApp(ui = ui, server = server)