Ifeanyi commited on
Commit
dcdfe4c
·
1 Parent(s): 526ed7e

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +119 -41
app.R CHANGED
@@ -1,51 +1,129 @@
1
  library(shiny)
 
 
 
 
 
2
  library(bslib)
 
 
 
3
  library(dplyr)
4
  library(ggplot2)
5
 
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
-
10
- ui <- page_fillable(theme = bs_theme(bootswatch = "minty"),
11
- layout_sidebar(fillable = TRUE,
12
- sidebar(
13
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
14
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
15
- checkboxGroupInput("species", "Filter by species",
16
- choices = unique(df$Species), selected = unique(df$Species)
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
- server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
31
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- output$scatter <- renderPlot({
34
- p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) + list(
35
- theme(legend.position = "bottom"),
36
- if (input$by_species) aes(color=Species),
37
- geom_point(),
38
- if (input$smooth) geom_smooth()
39
- )
40
-
41
- if (input$show_margins) {
42
- margin_type <- if (input$by_species) "density" else "histogram"
43
- p <- p |> ggExtra::ggMarginal(type = margin_type, margins = "both",
44
- size = 8, groupColour = input$by_species, groupFill = input$by_species)
45
- }
46
-
47
- p
48
- }, res = 100)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  }
50
 
51
- shinyApp(ui, server)
 
 
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)