Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,51 +1,129 @@
|
|
| 1 |
library(shiny)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
library(bslib)
|
|
|
|
|
|
|
|
|
|
| 3 |
library(dplyr)
|
| 4 |
library(ggplot2)
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
hr(), # Add a horizontal rule
|
| 19 |
-
checkboxInput("by_species", "Show species", TRUE),
|
| 20 |
-
checkboxInput("show_margins", "Show marginal plots", TRUE),
|
| 21 |
-
checkboxInput("smooth", "Add smoother"),
|
| 22 |
-
),
|
| 23 |
-
plotOutput("scatter")
|
| 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 |
-
|
|
|
|
|
|
| 1 |
library(shiny)
|
| 2 |
+
library(shinyalert)
|
| 3 |
+
library(shinythemes)
|
| 4 |
+
library(shinycssloaders)
|
| 5 |
+
library(shinyjs)
|
| 6 |
+
library(httr)
|
| 7 |
library(bslib)
|
| 8 |
+
library(thematic)
|
| 9 |
+
library(gtrendsR)
|
| 10 |
+
library(plotly)
|
| 11 |
library(dplyr)
|
| 12 |
library(ggplot2)
|
| 13 |
|
| 14 |
+
options(spinner.color = "lightblue",
|
| 15 |
+
spinner.color.background = "#ffffff",
|
| 16 |
+
spinner.size = 2)
|
| 17 |
+
|
| 18 |
+
my_theme <- bs_theme(
|
| 19 |
+
bg = "#fdfefe",
|
| 20 |
+
fg = "blue",
|
| 21 |
+
primary = "red",
|
| 22 |
+
base_font = font_google("PT Sans Caption"),
|
| 23 |
+
"font-size-base" = "0.9rem",
|
| 24 |
+
version = 5,
|
| 25 |
+
"navbar-bg" = "blue"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
+
thematic_shiny()
|
| 29 |
+
|
| 30 |
+
ui <- list(useShinyjs(),navbarPage(windowTitle = "TrendChecker",
|
| 31 |
+
title = strong("TrendChecker"),theme = my_theme,
|
| 32 |
+
tabPanel(title = strong("Trend Over Time"),icon = icon("chart-line"),
|
| 33 |
+
sidebarLayout(
|
| 34 |
+
sidebarPanel(width = 3,actionButton("info",strong("About TrendChecker",icon("info"))),hr(),
|
| 35 |
+
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,
|
| 36 |
+
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(),
|
| 37 |
+
textInput("text",strong("Enter Search Term"),value = "Soccer"),
|
| 38 |
+
checkboxGroupInput("check",strong("Select Country(ies)"),choices = c("USA" = "US","UK" = "GB","Germany" = "DE","Netherlands" = "NL","Nigeria" = "NG","Japan" = "JP"),selected = "US"),
|
| 39 |
+
radioButtons("radio",strong("Choose Trend Source"),choices = c("Web","News","YouTube","Images"),selected = "News"),
|
| 40 |
+
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"),
|
| 41 |
+
actionButton("run",strong("Run Query"),icon("caret-right"))
|
| 42 |
+
),
|
| 43 |
+
mainPanel(
|
| 44 |
+
withSpinner(plotlyOutput("plot"),type = 8))
|
| 45 |
+
|
| 46 |
+
))
|
| 47 |
+
))
|
| 48 |
+
|
| 49 |
+
# Define server logic required to run query
|
| 50 |
+
|
| 51 |
+
server <- function(input, output,session) {
|
| 52 |
|
| 53 |
+
## APP info button toggle activation
|
| 54 |
+
|
| 55 |
+
observeEvent(input$info,{
|
| 56 |
+
toggle("about")
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
## Create reactive input switch functionality
|
| 61 |
+
|
| 62 |
+
check_input <- reactive(input$check)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
radio_input <- reactive(switch(input$radio,
|
| 66 |
+
"Web" = "web",
|
| 67 |
+
"News" = "news",
|
| 68 |
+
"YouTube" = "youtube",
|
| 69 |
+
"Images" = "images"))
|
| 70 |
+
|
| 71 |
+
radio_time <- reactive(switch(input$time,
|
| 72 |
+
"Last Hour" = "now 1-H",
|
| 73 |
+
"Last Four Hours" = "now 4-H",
|
| 74 |
+
"Last Day" = "now 1-d",
|
| 75 |
+
"Last Seven Days" = "now 7-d",
|
| 76 |
+
"Past 30 Days" = "today 1-m",
|
| 77 |
+
"Past 90 Days" = "today 3-m",
|
| 78 |
+
"Past 12 Months" = "today 12-m",
|
| 79 |
+
"Last Five Years" = "today+5-y"))
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
text_input <- reactive(input$text)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
## Write the trend function
|
| 89 |
+
|
| 90 |
+
trend <- function(){
|
| 91 |
+
|
| 92 |
+
gt <- gtrends(keyword = c(text_input()),geo = c(check_input()),gprop = radio_input(),
|
| 93 |
+
time = radio_time())
|
| 94 |
+
|
| 95 |
+
p <- plot(gt)
|
| 96 |
+
|
| 97 |
+
gp <- ggplotly(p)
|
| 98 |
+
|
| 99 |
+
return(gp)
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
## Convert to reactive function
|
| 103 |
+
|
| 104 |
+
trend2 <- reactive(trend())
|
| 105 |
+
|
| 106 |
+
## Create interactive plot
|
| 107 |
+
|
| 108 |
+
output$plot <- renderPlotly({
|
| 109 |
+
|
| 110 |
+
withProgress(message = "Fetching data",
|
| 111 |
+
detail = "This may take a while...",value = 0,{
|
| 112 |
+
|
| 113 |
+
for (i in 1:25){
|
| 114 |
+
|
| 115 |
+
incProgress(1/25)
|
| 116 |
+
Sys.sleep(0.25)
|
| 117 |
+
}
|
| 118 |
+
})
|
| 119 |
+
|
| 120 |
+
input$run
|
| 121 |
+
isolate(trend2())
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
})
|
| 125 |
+
|
| 126 |
}
|
| 127 |
|
| 128 |
+
# Run the application
|
| 129 |
+
shinyApp(ui = ui, server = server)
|