File size: 4,926 Bytes
526ed7e
dcdfe4c
 
 
 
 
526ed7e
dcdfe4c
 
 
526ed7e
 
 
dcdfe4c
 
 
 
 
 
 
 
 
 
 
 
526ed7e
 
dcdfe4c
 
 
 
a6d3d0b
dcdfe4c
a4f5c3e
dcdfe4c
 
60d30df
a215634
 
 
ef8d58a
dcdfe4c
 
5304b24
f7c6751
 
dcdfe4c
 
 
 
 
 
 
526ed7e
dcdfe4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6d3d0b
dcdfe4c
 
 
3000c76
dcdfe4c
 
 
683e0f0
 
180765c
683e0f0
dcdfe4c
 
 
 
46ca90b
 
 
dcdfe4c
 
 
180765c
dcdfe4c
 
 
 
7c4d949
dcdfe4c
7c4d949
 
dcdfe4c
 
301bbeb
 
46ca90b
dcdfe4c
 
f7c6751
 
 
 
 
 
 
cc7609e
f7c6751
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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"),#firebaseUIContainer(),
                                            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")),
                                                             checkboxGroupInput("check",strong("Select Country(ies)"),choices = c("USA" = "US","UK" = "GB","Germany" = "DE","Netherlands" = "NL","Nigeria" = "NG","Japan" = "JP")),
                                                             radioButtons("radio",strong("Choose Trend Source"),choices = c("Web","News","YouTube","Images")),
                                                             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")),
                                                             actionButton("run",strong("Run Search"),icon("caret-right"))
                                                             ),
                                                mainPanel(
                                                          withSpinner(plotlyOutput("plot", width = "113%", height = "450px"),type = 8),
                                                downloadButton("plot_download","Download",icon = icon("download"))),
                                              
                                                          
                                            ))
)) 

# 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 function

trend <- function(){
    
    gt <- gtrends(keyword = text_input(),geo = check_input(),gprop = radio_input(),
            time = radio_time())
    
    p <- plot(gt)

    gp <- ggplotly(p)
        
    return(gp)
}
    
## Convert to reactive function

trend2 <- eventReactive(input$run,{
  trend()
})

## Create interactive plot

output$plot <- renderPlot({
    
    withProgress(message = "Fetching data",
                 detail = "This may take a while...",value = 0,{
                     
                     for (i in 1:40){
                         
                         incProgress(1/40)
                         Sys.sleep(0.4)
                     }
                 })    

    req(input$run)
    trend2()

})

output$plot_download <- downloadHandler(
  filename = function(){
    paste("graph",".jpg",sep = "")
  },
  content = function(file){
    jpeg(file)
    trend()
    dev.off()
  }
)
       
}

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