Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indicator native range year #65

Merged
merged 63 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
574f84b
Create indicator_native_range_year.R
SanderDevisscher Jun 30, 2020
d7373b9
Create test_indicator_native_range_year.R
SanderDevisscher Jul 7, 2020
c2ed621
Get the function working outside of reportinggrofwild & INBOtheme
SanderDevisscher Jul 7, 2020
2879cf5
add axis label customisation
SanderDevisscher Jul 7, 2020
2e5f002
add relative parameter
SanderDevisscher Jul 7, 2020
215f434
Update test_indicator_native_range_year.R
SanderDevisscher Jul 7, 2020
614106d
set dynamic hoverover text
SanderDevisscher Jul 7, 2020
323d1a5
Add translation from native_rang to native_continent
SanderDevisscher Aug 5, 2020
7c8ff30
rename function
SanderDevisscher Aug 13, 2020
c4bd355
translate to english
SanderDevisscher Aug 13, 2020
09958da
add data.table & plotly to description
SanderDevisscher Aug 13, 2020
b4f8fca
Add @sanderdevisscher to Authors
SanderDevisscher Aug 13, 2020
6878392
Add reshape 2 & scales to description
SanderDevisscher Aug 13, 2020
2ae86fd
cleanup roxygen part of function
SanderDevisscher Aug 13, 2020
eafc40e
stringify my email
SanderDevisscher Aug 13, 2020
9d0eacc
remove extra quote
SanderDevisscher Aug 13, 2020
b0e064b
rename function correctly
SanderDevisscher Aug 13, 2020
66a8584
add dplyr to import rules
SanderDevisscher Aug 13, 2020
2d280b5
add ggplot - plot to output & rename slots
SanderDevisscher Aug 13, 2020
55678f9
add case_when to import + remove plotly::plotly from import rules
SanderDevisscher Aug 13, 2020
655896b
Update roxygen items & text
SanderDevisscher Aug 13, 2020
84b14ad
remove required packages
SanderDevisscher Aug 13, 2020
405c0c1
Remove width & height parameters from ggplotly
SanderDevisscher Aug 13, 2020
36609c0
rename data slots
SanderDevisscher Aug 13, 2020
731271f
general layouting/indentations
SanderDevisscher Aug 13, 2020
81a4952
add pipe to import rules
SanderDevisscher Aug 13, 2020
bc4671b
Remove "," from import rules
SanderDevisscher Aug 13, 2020
946b1b2
complete export rule
SanderDevisscher Aug 13, 2020
5f247e3
rebuild NAMESPACE
SanderDevisscher Aug 13, 2020
b6305f5
Rewrite link to original code
SanderDevisscher Aug 17, 2020
1f49274
Fix title
SanderDevisscher Sep 7, 2020
42fbd0a
remove case_when for native_continent
SanderDevisscher Sep 7, 2020
29bc260
Correct typo form -> from
damianooldoni Sep 8, 2020
2b8f864
Remove NULL
damianooldoni Sep 8, 2020
824f2c8
Import %>% from dplyr instead of adding magrittr to pkgs
damianooldoni Sep 8, 2020
25fc430
Apply styling via styler pkg
damianooldoni Sep 10, 2020
947e0ea
devtools::document()
damianooldoni Sep 10, 2020
ddbe7bd
Update version number
damianooldoni Sep 10, 2020
11652f8
Remove test new function as it is not a test
damianooldoni Sep 10, 2020
06b25c0
Update fucntions using name_* functions from rgbif to be rgbif 3.x co…
damianooldoni Sep 11, 2020
3766a36
Solve new arised bug with empty df: logicals not set as chars
damianooldoni Sep 11, 2020
5727ec5
Use tibble/as_tibble as they are already exported by trias
damianooldoni Sep 11, 2020
24fc728
Use left_join instead of right_join to return same order of taxa
damianooldoni Sep 14, 2020
f7bc6d1
Solve order columns in output info new synonyms df
damianooldoni Sep 14, 2020
84c27d1
Add checks for unique taxon keys and no NAs
damianooldoni Sep 14, 2020
7f422a1
Make tubbles instead of dfs
damianooldoni Sep 14, 2020
cb57365
Add coumentation for data, years and relative
damianooldoni Sep 14, 2020
4662830
Add first_observed argument
damianooldoni Sep 14, 2020
e78d77a
Remove unused args in function
damianooldoni Sep 14, 2020
3655230
Set minimum requirement version for rgbif
damianooldoni Sep 14, 2020
0bfc5e5
Set tibble instead of data.frame
damianooldoni Sep 14, 2020
3696004
Update documentation devtools::document()
damianooldoni Sep 14, 2020
244cc1c
Remove typos in @param names and add description of first_observed
damianooldoni Sep 14, 2020
ef8266a
Remove a last return arg as it is defunct in rgbif 3
damianooldoni Sep 14, 2020
e5e361d
Set trias::tiblble to ùake tibble in test a recognizable function
damianooldoni Sep 14, 2020
bd09e4f
Put all tests for in/output together, avoid to source file with input…
damianooldoni Sep 14, 2020
47f978f
set dplyr::tibble instead of trias::tibble in test
damianooldoni Sep 14, 2020
b301f01
Solve typo in markdown syntax for link in documentation
damianooldoni Sep 14, 2020
4ce92ef
Apply styler::style_pg()
damianooldoni Sep 14, 2020
e500397
add xlab, ylab and title to static plot
SanderDevisscher Sep 15, 2020
f68ac0a
Remove title
SanderDevisscher Sep 15, 2020
2610334
add 90° angle axis text for static plot
SanderDevisscher Sep 15, 2020
43a5c84
Remove remote installation of rgbif
damianooldoni Sep 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions R/indicator_native_range_year.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#' Create interactive plot for counts per native region and year of introduction
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
#'
#' Based on \code{\link{countYearProvince}} plot from grofwild
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like you use links for more information about code in documentation 👍
But if I compile the documentation via devtools::document() I get:

> devtools::document()
Updating trias documentation
Loading trias
Writing NAMESPACE
Writing NAMESPACE
Writing countYearNativerange.Rd
Warning message:
Can't find help topic 'countYearProvince' in current package

This means the link is broken. R thinks your link is in trias package. Is grofwild a package? Then you can use something like this \code{\link[MASS]{abbey}}? But I don't think it is a package. Then maybe a classic Markdown-style link solves the problem, e.g.:

#' See more about the markdown markup at the
#' [Commonmark web site](http://commonmark.org/help)

This vignette is very helpful: https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html#links

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like you use links for more information about code in documentation 👍

I just copied that part from @eadriaensen => should I include her as contributor as well ?

Is grofwild a package?

reportingGrofwild is a package, yes! However it is not published to cran or any other package repository.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@damianooldoni the link to the reporting-grofwild function has been rewriten but I don't know how to check if its working.
The installation of the modified trias package runs without new errors (expect the error described here #65 (comment)).

#' @param type character, native_range level of interest should be one of
#' \code{c("native_continent", "native_range")}
#' @param xlab character string, label of the x-axis. Defaults to "year".
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
#' @param ylab character string, label of the y-axis. Defaults to "number of
#' alien species".
#' @inheritParams countYearProvince
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the comment above about link to countYearProvince, inheriting parameters doesn't work either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this line, will have to test if an alternative should be found !

#' @return list with:
#' \itemize{
#' \item{'plot': }{plotly object, for a given specie the observed number
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
#' per year and per province is plotted in a stacked bar chart}
#' \item{'data': }{data displayed in the plot, as data.frame with:
#' \itemize{
#' \item{'year': }{year at which the animal was introduced}
#' \item{'nativeRange': }{native range of the introduced animal}
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
#' \item{'aantal': }{counts of animals}
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
#' }
#' }
#' }
#' @import plotly
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
#' @importFrom reshape2 melt
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
#' @importFrom INBOtheme inbo.2015.colours
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed INBOtheme from trias functions as I got some troubles with it. Moreover, you don't use inbo.2015.colours explicitly in the code. To use INBOtheme colors, just load INBOtheme colors before using this function. This is how I do it in Rmd files of indicators repo where I use trias' visualization functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@damianooldoni should I write a .rmd for this function as well ?

#' @export
countYearNativerange <- function(data, jaartallen = NULL,
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
type = c("native_continent", "native_range"),
width = NULL, height = NULL,
x_lab = "year",
y_lab = "alien species",
relative = FALSE) {

require(plotly)
require(data.table)
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved

type <- match.arg(type)

if (is.null(jaartallen))
jaartallen <- sort(unique(data$first_observed))

plotData <- data %>%
mutate(native_continent = case_when(grepl(pattern = "Africa", native_range, ignore.case = TRUE) ~ "Africa",
grepl(pattern = "America", native_range, ignore.case = TRUE) ~ "America",
grepl(pattern = "Asia", native_range, ignore.case = TRUE) ~ "Asia",
grepl(pattern = "Australia", native_range, ignore.case = TRUE) ~ "Oceania",
grepl(pattern = "nesia", native_range, ignore.case = TRUE) ~ "Oceania",
grepl(pattern = "Europe", native_range, ignore.case = TRUE) ~ "Europe",
TRUE ~ as.character(NA)))
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved

plotData$locatie <- switch(type,
native_range = plotData$native_range,
native_continent = plotData$native_continent
)

# Select data
plotData <- plotData[plotData$first_observed %in% jaartallen, c("first_observed", "locatie")]
plotData <- plotData[!is.na(plotData$first_observed) & !is.na(plotData$locatie), ]

# Exclude unused provinces
plotData$locatie <- as.factor(plotData$locatie)
plotData$locatie <- droplevels(plotData$locatie)

# Summarize data per native_range and year
plotData$first_observed <- with(plotData, factor(first_observed, levels =
min(jaartallen):max(jaartallen)))

summaryData <- melt(table(plotData), id.vars = "first_observed")
summaryData <- summaryData %>%
group_by(first_observed) %>%
mutate(total = sum(value),
perc = round((value/total)*100,2))

# Summarize data per year
totalCount <- table(plotData$first_observed)


# For optimal displaying in the plot
summaryData$locatie <- as.factor(summaryData$locatie)
summaryData$locatie <- factor(summaryData$locatie, levels = rev(levels(summaryData$locatie)))
summaryData$first_observed <- as.factor(summaryData$first_observed)



# Create plot

if(relative == TRUE){
position <- "fill"
text <- paste0(summaryData$locatie, "<br>", summaryData$perc, "%")
}else{
position <- "stack"
text <- paste0(summaryData$locatie, "<br>", summaryData$value)
}

pl <- ggplot(data = summaryData, aes(x = first_observed, y = value, fill = locatie, text = text)) +
geom_bar(position = position, stat = "identity")

if(relative == TRUE){
pl <- pl + scale_y_continuous(labels = scales::percent_format())
}
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved

pl <- ggplotly(data = summaryData, pl, width = width, height = height, tooltip ="text") %>%
layout(xaxis = list(title = x_lab, tickangle = "auto"),
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved
yaxis = list(title = y_lab, tickformat = ",d"),
margin = list(b = 80, t = 100),
barmode = ifelse(nlevels(summaryData$first_observed) == 1, "group", "stack"))

# To prevent warnings in UI
pl$elementId <- NULL

# Change variable name
names(summaryData)[names(summaryData) == "value"] <- "aantal"
names(summaryData)[names(summaryData) == "first_observed"] <- "jaar"
names(summaryData)[names(summaryData) == "locatie"] <- "regio van oorsprong"
SanderDevisscher marked this conversation as resolved.
Show resolved Hide resolved

return(list(plot = pl, data = summaryData))

}
7 changes: 7 additions & 0 deletions tests/testthat/test_indicator_native_range_year.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(readr)
data_input_checklist_indicators <- read_delim("https://raw.githubusercontent.com/trias-project/indicators/master/data/interim/data_input_checklist_indicators.tsv",
"\t", escape_double = FALSE, trim_ws = TRUE)

source("./R/indicator_native_range_year.r")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need and don't have to source within a package. Installing a package and loading it makes all functions available for tests as well.

Run devtools::install() and ´library(trias)(the latter command needed the first time) after you changed something in the functions and you have all functions ready to be used. So, in package development,devtools::install()is your best friend,source` your enemy 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use devtools::install() with the script without plotly::layout() i get this error:
Note: possible error in 'layout(xaxis = list(title = x_lab, ': unused arguments (xaxis = list(title = x_lab, tickangle = "auto"), yaxis = list(title = y_lab, tickformat = ",d"), margin = list(b = 80, t = 100), barmode = ifelse(nlevels(summaryData$first_observed) == 1, "group", "stack")) at indicator_native_range_year.R:104

I suspected this was due to R thinking layout from the package graphics should be used, while the importFrom line clearly states layout should come from the plotly package (@importForm plotly ggplotly, layout).
Adding plotly:: before layout solves this problem. Any idea why this is ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somehow rebuilding the NAMESPACE (found out some functions I imported were missing) does not include plotly to the list. Maybe this is the cause??


countYearNativerange(data_input_checklist_indicators, jaartallen = c(1990:2019), type = "native_range", relative = FALSE)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a test. Testing plotly output is practically impossible or quite challenging, but you can test the data slot, and the fact that you get always a list and not something else... Writing tests is something which takes time, I know, but it helps making your code more robust and user friendly. For example, testing the input types, See other tests functions in trias package to understand what I mean. But I don't blame you if you don't want to spend time on the tests, I will do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a test.

image

all jokes aside, I simply used this to test if the function worked.