Skip to content

Commit bd83530

Browse files
authored
Merge pull request #68 from ESHackathon/restructure
Restructure as package with ability to save custom app
2 parents bc1fff6 + 2d5c8a2 commit bd83530

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2092
-2044
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$

DESCRIPTION

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Package: eviatlas
2+
Version: 0.4.0
3+
Title: Visualisation and mapping for datasets from systematic reviews and maps
4+
Authors@R: c(
5+
person(
6+
given = "Martin",
7+
family = "Westgate",
8+
role = c("aut", "cre"),
9+
email = "martinjwestgate@gmail.com",
10+
comment = c(ORCID = "0000-0003-0854-2034")),
11+
person(
12+
given = "Andrew",
13+
family = "Feierman",
14+
role = c("aut"),
15+
email = "andrew.feierman@sei.org"))
16+
Description: Run a shiny app for visualising the datasets produced by systematic maps, or produce your own.
17+
Depends: R (>= 3.5.0), shiny
18+
License: GPL-3
19+
BugReports: https://github.com/rmetaverse/eviatlas
20+
LazyData: true
21+
RoxygenNote: 7.1.1
22+
Imports:
23+
dplyr,
24+
tidyr,
25+
readr,
26+
stringr,
27+
magrittr,
28+
ggplot2,
29+
DT,
30+
RColorBrewer,
31+
viridis,
32+
sf,
33+
leaflet,
34+
mapview,
35+
leafem,
36+
shinyBS,
37+
shinyWidgets,
38+
shinydashboard

LICENSE

Lines changed: 0 additions & 674 deletions
This file was deleted.

NAMESPACE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
S3method(GenTimeTrend,default)
4+
S3method(GenTimeTrend,numeric)
5+
export("%<>%")
6+
export("%>%")
7+
export(eviatlas)
8+
export(save_eviatlas_app)
9+
export(shiny_config)
10+
export(shiny_server)
11+
export(shiny_ui)
12+
import(shiny)
13+
importFrom(magrittr,"%<>%")
14+
importFrom(magrittr,"%>%")
15+
importFrom(utils,capture.output)
16+
importFrom(utils,data)
17+
importFrom(utils,read.csv)
18+
importFrom(utils,write.csv)

R/GenHeatMap.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#' Create heatmap from dataset
2+
#'
3+
#' Feed in a dataset and get a heatmap in return
4+
#'
5+
#' @param idata Input dataframe
6+
#' @param selcols Numeric vector of column indices
7+
#' @param axis_txt_lim character limit for label text
8+
#'
9+
#' @return Returns a heatmap object showing number of literature under different categories in user specified \code{selcols}
10+
#' @keywords internal
11+
#' @author Ezgi Tanriver-Ayder and Sanita Dhaubanjar
12+
13+
GenHeatMap <- function(idata, selcols, axis_txt_lim = 60) {
14+
15+
# if df is a shapefile, remove geometry column
16+
if (any(class(idata) == "sf")) {
17+
idata <- sf::st_drop_geometry(idata)
18+
}
19+
20+
# Bind variables locally to function (helps avoid R CMD notes)
21+
listone <- listtwo <- n <- NULL
22+
23+
# Convert columns to factors to allow for categorical classification for both numeric and character data -------
24+
tmp <- as.data.frame(sapply(idata[selcols], function(x) as.factor(x)))
25+
26+
27+
# Plot Heatmap ------
28+
heatmp <- tmp %>%
29+
dplyr::rename(listone = colnames(tmp[1]), listtwo = colnames(tmp[2])) %>%
30+
dplyr::count(listone, listtwo) %>%
31+
tidyr::complete(listone, listtwo, fill = list(n = 0)) %>%
32+
ggplot2::ggplot(ggplot2::aes(x = listone, y = listtwo, fill = n, label = n)) +
33+
ggplot2::geom_tile(alpha = 0.3, color = "grey60") +
34+
ggplot2::geom_text() +
35+
viridis::scale_fill_viridis() +
36+
ggplot2::theme_minimal() +
37+
ggplot2::theme(
38+
axis.text.x = ggplot2::element_text(angle = 45, hjust = 1),
39+
panel.grid = ggplot2::element_blank(),
40+
text = ggplot2::element_text(size = 14),
41+
axis.title = ggplot2::element_text(size = 16),
42+
title = ggplot2::element_text(size = 18)
43+
) +
44+
ggplot2::xlab(paste0(selcols[1])) +
45+
ggplot2::ylab(paste0(selcols[2])) +
46+
ggplot2::labs(fill = "Count") +
47+
# Limit axis text to a certain number of characters, so that long text doesn't ruin the chart display
48+
ggplot2::scale_x_discrete(labels = function(x) substr(x, 1, axis_txt_lim)) +
49+
ggplot2::scale_y_discrete(labels = function(x) substr(x, 1, axis_txt_lim)) +
50+
ggplot2::ggtitle("Study Heatmap", subtitle = paste(selcols[2], "by", selcols[1]))
51+
52+
heatmp
53+
}

R/GenLocationTrend.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#' Create bar plot with distribution of studies over the region from lat/long info
2+
#'
3+
#' Created for: ES Hackathon 2018
4+
#' @param df Input dataframe
5+
#' @param location_column Column with location information (preferably country-level or higher)
6+
#' @param axis_txt_lim character limit for label text
7+
#' @return Returns a bar plot object showing counts of literature in systematic review for each location
8+
#'
9+
#' @author Sanita Dhaubanjar
10+
#'
11+
#' @keywords SystematicReview
12+
#' @keywords internal
13+
14+
GenLocationTrend <- function(df, location_column, axis_txt_lim = 60) {
15+
16+
# if df is a shapefile, remove geometry column
17+
if (any(class(df) == "sf")) {
18+
df <- sf::st_drop_geometry(df)
19+
}
20+
21+
22+
# Bind variables locally to function (helps avoid R CMD notes)
23+
listone <- listtwo <- n <- NULL
24+
25+
# Count per locations --------
26+
location_counts <- as.data.frame(table(df[location_column])) # table() tabulates frequency
27+
colnames(location_counts) <- c(location_column, "n")
28+
29+
# Plot bar chart
30+
locmp <- ggplot2::ggplot(location_counts, ggplot2::aes_string(
31+
x = colnames(location_counts[1]),
32+
y = colnames(location_counts[2]),
33+
label = colnames(location_counts[2])
34+
)) +
35+
ggplot2::geom_bar(alpha = 0.9, stat = "identity", fill = "light green") +
36+
ggplot2::scale_x_discrete(labels = function(x) substr(x, 1, axis_txt_lim)) +
37+
ggplot2::geom_text(ggplot2::aes(), size = 3, nudge_y = 10) +
38+
ggplot2::labs(y = "# Studies") +
39+
ggplot2::ggtitle(paste(location_column, "frequency")) +
40+
ggplot2::theme_bw() +
41+
ggplot2::theme(
42+
axis.line = ggplot2::element_line(colour = "black"),
43+
panel.background = ggplot2::element_blank(),
44+
plot.title = ggplot2::element_text(hjust = .5),
45+
text = ggplot2::element_text(size = 14)
46+
)
47+
48+
# Rotate xaxis label if too many
49+
if (nrow(location_counts) > 15) {
50+
locmp <- locmp + ggplot2::theme(
51+
axis.text.x = ggplot2::element_text(angle = 45, hjust = 0.95, size = 11)
52+
)
53+
}
54+
55+
locmp
56+
}
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
#' Create Histogram from dataset
2+
#' @param idata dataset from eviatlas
3+
#' @param hist_col column used for histogram
4+
#' @param axis_txt_lim character limit for label text in axis
5+
#' @keywords internal
26

3-
GenTimeTrend = function(idata, hist_col, axis_txt_lim = 60){
7+
GenTimeTrend <- function(idata, hist_col, axis_txt_lim = 60) {
48
UseMethod("GenTimeTrend", object = idata[hist_col][[1]])
59
}
610

7-
GenTimeTrend.default <- function(idata, hist_col, axis_txt_lim = 60){
8-
9-
gttmp <- ggplot2::ggplot(idata, aes_string(x = hist_col)) +
11+
#' Create Histogram from dataset - default method
12+
#' @method GenTimeTrend default
13+
#' @export
14+
#' @keywords internal
15+
GenTimeTrend.default <- function(idata, hist_col, axis_txt_lim = 60) {
16+
gttmp <- ggplot2::ggplot(idata, ggplot2::aes_string(x = hist_col)) +
1017
ggplot2::geom_bar(
1118
alpha = 0.9,
1219
stat = "count",
1320
fill = "dodger blue"
1421
) +
1522
ggplot2::labs(y = "Studies") +
16-
ggplot2::scale_x_discrete(name = paste(hist_col), labels = function(x) substr(x, 1, axis_txt_lim)) +
23+
ggplot2::scale_x_discrete(
24+
name = paste(hist_col),
25+
labels = function(x) substr(x, 1, axis_txt_lim)
26+
) +
1727
ggplot2::theme_bw() +
1828
ggplot2::theme(
1929
axis.line = ggplot2::element_line(colour = "black"),
2030
panel.background = ggplot2::element_blank(),
2131
plot.title = ggplot2::element_text(hjust = .5),
2232
text = ggplot2::element_text(size = 13)
2333
)
24-
34+
2535
# Rotate xaxis label if too many categories
26-
if (dplyr::n_distinct(idata[hist_col]) > 15){
36+
if (dplyr::n_distinct(idata[hist_col]) > 15) {
2737
gttmp <- gttmp + ggplot2::theme(
28-
axis.text.x = element_text(angle = 40, hjust = 0.95, size = 12))
38+
axis.text.x = ggplot2::element_text(
39+
angle = 40,
40+
hjust = 0.95,
41+
size = 12
42+
)
43+
)
2944
}
3045
gttmp
3146
}
3247

33-
GenTimeTrend.numeric <- function(idata, hist_col, axis_txt_lim = 60){
34-
35-
ggplot2::ggplot(idata, aes_string(x = hist_col)) +
48+
#' Create Histogram from dataset - numeric column
49+
#' @method GenTimeTrend numeric
50+
#' @export
51+
#' @keywords internal
52+
53+
GenTimeTrend.numeric <- function(idata, hist_col, axis_txt_lim = 60) {
54+
ggplot2::ggplot(idata, ggplot2::aes_string(x = hist_col)) +
3655
ggplot2::geom_bar(
3756
alpha = 0.9,
3857
stat = "count",

R/eviatlas.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' Eviatlas Shiny App
2+
#'
3+
#' @param study_data a dataframe or tibble containing data to be used in an eviatlas shiny app
4+
#' @param ... additional variables to feed into shiny app
5+
#' @export
6+
7+
eviatlas <- function(study_data = NULL, ...) {
8+
shiny_env <- new.env()
9+
attr(shiny_env, "name") <- "eviatlas_shiny_env"
10+
11+
if (!missing(study_data)) {
12+
assign("study_data", study_data, shiny_env)
13+
}
14+
environment(shiny_ui) <- shiny_env
15+
environment(shiny_server) <- shiny_env
16+
environment(shiny_config) <- shiny_env
17+
18+
app <- shiny::shinyApp(
19+
ui = shiny_ui,
20+
server = shiny_server,
21+
onStart = shiny_config
22+
)
23+
24+
environment(app) <- shiny_env
25+
shiny::runApp(app, ...)
26+
}

R/eviatlas_pilotdata.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#' Eviatlas Sample Data
2+
#'
3+
#' A sample systematic map dataset used to show features of Eviatlas and
4+
#' its functions to build a map atlas
5+
#' @keywords internal
6+
"eviatlas_pilotdata"

R/get_coord_cols.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#' functions to determine if columns contain what appear to be latitude or longitude columns
2+
#' @keywords internal
3+
4+
get_latitude_cols <- function(df) {
5+
n_out <- colnames(df)[1]
6+
# waiting for a more elegant interpretation, this is quick and dirty...
7+
if ("Latitude" %in% colnames(df)) {
8+
n_out <- "Latitude"
9+
} else if ("latitude" %in% colnames(df)) {
10+
n_out <- "latitude"
11+
} else if ("lat" %in% colnames(df)) {
12+
n_out <- "lat"
13+
} else if ("Plotted.lat." %in% colnames(df)) {
14+
n_out <- "Plotted.lat."
15+
} else if (length(agrep(pattern = "latitude", x = colnames(df)))) {
16+
n_out <- agrep(pattern = "latitude", x = colnames(df), value = T)[1]
17+
}
18+
n_out
19+
}
20+
21+
get_longitude_cols <- function(df) {
22+
n_out <- colnames(df)[1]
23+
# waiting for a more elegant interpretation, this is quick and dirty...
24+
if ("Latitude" %in% colnames(df)) {
25+
n_out <- "Longitude"
26+
} else if ("longitude" %in% colnames(df)) {
27+
n_out <- "longitude"
28+
} else if ("long" %in% colnames(df)) {
29+
n_out <- "long"
30+
} else if ("lng" %in% colnames(df)) {
31+
n_out <- "lng"
32+
} else if ("Plotted.long." %in% colnames(df)) {
33+
n_out <- "Plotted.long."
34+
} else if (length(agrep(pattern = "longitude", x = colnames(df)))) {
35+
n_out <- agrep(pattern = "longitude", x = colnames(df), value = T)[1]
36+
}
37+
n_out
38+
}

0 commit comments

Comments
 (0)