Skip to content

Commit

Permalink
Merge pull request #22 from agrobioinfoservices/devel
Browse files Browse the repository at this point in the history
merge-new-report
  • Loading branch information
kauedesousa authored Aug 8, 2020
2 parents 72f86e0 + 791feaf commit e6944e0
Show file tree
Hide file tree
Showing 34 changed files with 7,999 additions and 5,536 deletions.
193 changes: 67 additions & 126 deletions ClimMob.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# # ................................................................
# # ................................................................
# # Main script to call for the analysis and rendering the reports
# # in ClimMob v3
# # Main script to call for the analysis and rendering ClimMob reports
# # ................................................................
# # ................................................................

# tag <- "techapp"
# tag <- "CR2020"
# args <- c(paste0("dev/data/",tag,"/data.json"), paste0("dev/data/",tag,"/info.json"),
# paste0("dev/output/",tag,"/"), "TRUE","en","html",
# "farmer", "variety", getwd())
# # source("dev/run_climmobv2.R")
# paste0("dev/output/",tag,"/"), "TRUE","en","docx",
# "participant", "option", getwd(), NA)

# get the arguments from server's call
args <- commandArgs(trailingOnly = TRUE)
Expand All @@ -22,150 +20,93 @@ extension <- args[6] # report file format it can be "docx", "pdf", and "html"
ranker <- args[7] # how the system will refer to participants/farmers
option <- args[8] # how the system will refer to tested items
fullpath <- args[9] # this is backward path

# ................................................................
# ................................................................
## Packages ####
library("ClimMobTools")
library("gosset")
library("PlackettLuce")
library("partykit")
library("qvcalc")
library("psychotools")
library("jsonlite")
library("multcompView")
library("knitr")
library("rmarkdown")
library("pls")
library("gtools")
library("ggplot2")
library("igraph")
library("mapview")
library("ggrepel")
library("ggparty")
library("patchwork")

source(paste0(fullpath, "/R/functions.R"))

reference <- args[10] # the reference item for the analysis
if (isTRUE(is.na(reference))) {
reference <- 1
}

# ................................................................
# ................................................................
# Read data ####
# Read data with selected traits and explanatory variables to be analysed
pars <- jsonlite::fromJSON(infoname)
pars <- ClimMobTools:::.decode_pars(pars)

cmdata <- jsonlite::fromJSON(outputname)
class(cmdata) <- union("CM_list", class(cmdata))
cmdata <- as.data.frame(cmdata, tidynames = FALSE, pivot.wider = TRUE)

# ................................................................
# ................................................................
# Dataset parameters ####
Option <- ClimMobTools:::.title_case(option)

# the project name
projname <- which(grepl("project_name", names(cmdata)))
projname <- cmdata[1, projname]

# variables to produce split of results into multiple groups.
expvar <- pars$expl$vars
expvar_full <- pars$expl$name

# number of rankers
nranker <- nrow(cmdata)

itemnames <- cmdata[, grepl("package_item", names(cmdata))]

# Number of items each participant evaluates
ncomp <- ncol(itemnames)

# Name of items tested
items <- unique(sort(unlist(itemnames)))

# Number o items tested
nitems <- length(items)

# Number of characteristics (traits) avaluated
ntrait <- nrow(pars$chars)

# Colnames where items are placed within cmdata
itemnames <- names(itemnames)

# number of questions
nquest <- pars$chars$n_quest[1]

# define which function should be called to build the rankings
if (ncomp == 3) {
rankwith <- "rank_tricot"
}

if (ncomp > 3) {
rankwith <- "rank_numeric"
pars <- tryCatch({
pars <- jsonlite::fromJSON(infoname)
pars <- ClimMobTools:::.decode_pars(pars)
},error = function(e) {
e$message <-
paste(
"Unable to read the json file with the",
"parameters required to perform the analysis."
)
}
)

# Read as json and add the class "CM_list" so it can be passed to as.data.frame
# method from ClimMobTools
cmdata <- tryCatch({
cmdata <- jsonlite::fromJSON(outputname)
class(cmdata) <- union("CM_list", class(cmdata))
cmdata <- as.data.frame(cmdata, tidynames = FALSE, pivot.wider = TRUE)
},error = function(e) {
e$message <-
paste(
"Unable to read the json file with the",
"ClimMob data."
)
}
)

# ................................................................
# ................................................................
# Statistic parameters ####
# Set maximum proportion of missing data allowed in a characteristic evaluation
# before it is excluded.
missper <- 0.5

# Set minimum proportion of valid observations in explanatory variables
missexp <- 0.8

# Set minimum split size for tree models.
minsplit <- ceiling(nrow(cmdata) * 0.1)
if (minsplit < 10) {minsplit <- 10}
# Set alpha
sig_level <- 0.1

# method for adjustments for confidence intervals and setting widths for comparison.
# Defaults to B-H (Benjamini an Hochberg). Any of the methods from p.adjust will work here
# though: "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none"
ci_adjust <- "BH"

# confidence interval level for comparison plots with error bars. 84% to give an
# approximate 5% significance level for comparisons of non-overlapping confidence
# intervals (e.g. https://www.ncbi.nlm.nih.gov/pmc/articles/PMC524673/)
# should probably allow alternatives to be 0.9, 0.95 or 0.99
ci_level <- 0.84

# resolution of display items
dpi <- 250
out_width <- "100%"


# participant report params
# Participant report parameters ####
info_table_items <- c() #info.table.items <- c("variety 1", "variety 2", "variety 3")
info_table_info <- c() #info.table.info <- c("plant it early", "good in high altitude", "")
info_table_typeinfo <- "" #info.table.typeinfo <- "expert advice"

# ................................................................
# ................................................................
# Run analysis ####
dir.create(pathname, showWarnings = FALSE, recursive = TRUE)
tryCatch({
dir.create(pathname, showWarnings = FALSE, recursive = TRUE)
})

#cmdata$evaluation_ASS0dfe2b4467a3_char_agecompare_neg <- NA
#cmdata$evaluation_ASS0dfe2b4467a3_char_overallperf_neg <- NA

source(paste0(fullpath, "/R/analysis_climmob.R"))

# ................................................................
# ................................................................
# Write outputs ####
rankers <- ClimMobTools:::.pluralize(ranker)
options <- ClimMobTools:::.pluralize(option)
nothertraits <- ntrait - 1

#determine format based on extensions
output_format <- ifelse(extension == "docx","word_document",
paste0(extension,"_document"))

# produce main report if output type is "summary" or "both"
rmarkdown::render(paste0(fullpath, "/report/", language, "/mainreport/mainreport.Rmd"),
output_dir = pathname,
output_format = output_format,
output_file = paste0(projname,"_report",".",extension))
# produce the reports
if (isTRUE(done)) {

# the main report
done <- tryCatch({
rmarkdown::render(paste0(fullpath, "/report/", language, "/mainreport/mainreport.Rmd"),
output_dir = pathname,
output_format = output_format,
output_file = paste0(projname, "_climmob_main_report", ".", extension))
}, error = function(e) {
return(FALSE)
}
)

# farmer reports if output type is "farmer" or "both"
# if (isTRUE(infosheets)) {
# source("Farmer Reports/farmerreport.R")
# }
}

# #produce farmer reports if output type is "farmer" or "both"
# if (infosheets) {
# source("Farmer Reports/farmerreport.R")
# }
# if there was any error in the analysis, produce a error report
if (isFALSE(done)) {
rmarkdown::render(paste0(fullpath, "/report/", language, "/mainreport/mainreport_failed.Rmd"),
output_dir = pathname,
output_format = output_format,
output_file = paste0(projname, "_climmob_main_report", ".", extension))
}

17 changes: 17 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
ClimMob-analysis (2020-08-08)
=========================

### Improvements
* Analitical process was moved to a single script "R/analysis_climmob.R"
* Dynamic statements for the report based on the in-putted data and results from analysis. This will also make less complicated to translate the report to other languages
* Code is wrapped in a `tryCatch()` to prevent crashes when a non-expected behaviour in the data occurs. When any error occurs during the analysis the script will return a "failed report" with a message asking the user to contact ClimMob supporting service.
* The process now produce the reports for projects with only one characteristic, the 'overall_performance', which is mandatory. If overall_performance is missing the script will return the "failed report".
* Rankings can be build with more than 3 items per participant using `do.call()`, which will build the rankings with `rank_tricot()` when 3 items or with `rank_numeric()` when 4+ items. Projects with 4 or more items (to be tested by each participant) can now be enabled in ClimMob.net

### Changes in behaviour
* New argument (arg[10]) is added for the reference item (from a discussion with IITA and Cornell). The argument can be `NULL` or `NA` (which will take the first item as reference) or a character string selected by the user which should match with one of the items tested in the project.
* Map is build with package `leaflet`
* New packages added `leaflet` and `multcompView`
* Coordinates in the report map (if any) are clustered in groups with a resolution of 0.05, to comply with the participant's privacy.


ClimMob-analysis (2020-06-04)
=========================
### Improvents
Expand Down
Loading

0 comments on commit e6944e0

Please sign in to comment.