14.3 Add some features with Shiny

Build a slider, this cannot be run, it needs to be inside the shiny app/flexdashboard.

minvalue <- floor(min(map$PSev, na.rm = TRUE))
maxvalue <- ceiling(max(map$PSev, na.rm = TRUE))

sliderInput("rangevalues",
  label = "PSev values:",
  min = minvalue, max = maxvalue,
  value = c(minvalue, maxvalue)
)

14.3.1 Add a table

And add a table and wrap it into a div() function with the option for resizing the font-size.

div(renderDT({
  DT::datatable(mapFiltered()@data[, c("ISO3", "NAME", "PSev")],
    rownames = FALSE, 
    options = list(pageLength = 10)
  )
}),style = "font-size:80%")

14.3.2 Make the histogram

To color the bins, we need to know how many bins are in our histogram, in this case we use ggplot_build() function:

### Histogram
library(ggplot2)

gg_b <- ggplot_build(
  ggplot() + geom_histogram(aes(x = map$PSev), binwidth=0.5)
)
gg_b$data 
## [[1]]
##   y count   x  xmin xmax   density ncount ndensity flipped_aes PANEL group ymin
## 1 2     2 0.0 -0.25 0.25 0.2666667    0.4      0.4       FALSE     1    -1    0
## 2 5     5 0.5  0.25 0.75 0.6666667    1.0      1.0       FALSE     1    -1    0
## 3 4     4 1.0  0.75 1.25 0.5333333    0.8      0.8       FALSE     1    -1    0
## 4 2     2 1.5  1.25 1.75 0.2666667    0.4      0.4       FALSE     1    -1    0
## 5 1     1 2.0  1.75 2.25 0.1333333    0.2      0.2       FALSE     1    -1    0
## 6 0     0 2.5  2.25 2.75 0.0000000    0.0      0.0       FALSE     1    -1    0
## 7 0     0 3.0  2.75 3.25 0.0000000    0.0      0.0       FALSE     1    -1    0
## 8 1     1 3.5  3.25 3.75 0.1333333    0.2      0.2       FALSE     1    -1    0
##   ymax colour   fill linewidth linetype alpha
## 1    2     NA grey35       0.5        1    NA
## 2    5     NA grey35       0.5        1    NA
## 3    4     NA grey35       0.5        1    NA
## 4    2     NA grey35       0.5        1    NA
## 5    1     NA grey35       0.5        1    NA
## 6    0     NA grey35       0.5        1    NA
## 7    0     NA grey35       0.5        1    NA
## 8    1     NA grey35       0.5        1    NA
gg_b$plot

nu_bins <- dim(gg_b$data[[1]])[1]
nu_bins
## [1] 8
ggplot() +
  geom_histogram(aes(x = map$PSev),
                 binwidth=.5,
                 fill = rainbow(nu_bins))