message("you can now develop directly in the inst/tempalte dir, see instruction to compile the pdf in the file there")
library(dplyr)
library(tradeflows)
library(knitr)
library(ggplot2)
library(reshape2)
options(digits = 4) #  Number of digits in knitr tables
opts_chunk$set(echo=FALSE, message=FALSE)
######### #
# ! This chunk is used for development only, remove it or change eval=FALSE.
# ! When this file is used as a template, place two  `r` in the title.
######### #
# The data frame tfdata will be passed by the report generating function to the template
# Reporter information can be extracted back from tfdata 
# in the template this would have to be the country appearing most in the data frame
reporterinreport <- "China" 
# Test countries: Cameroon, Ghana, Indonesia

message("Trade values are the same in raw flow and validated flow")
message("The table read will have to be changed to validated_flow to access quantities")

# If I use the readdbtbl function to join the 2 tables, the following error will be returned
# Error: x and y don't share the same src. Set copy = TRUE to copy y into x's source (this may be time consuming).
# Therefore I create the conenction object here so that it can be shared between the two tbl() objects.
setdatabaseconfig(silent=TRUE)
db <- getOption("tradeflowsDB")
DBread <- src_mysql(user=db["user"], host=db["host"],
                    password=db["password"], dbname=db["dbname"])
tfdata <- tbl(DBread, "raw_flow_yearly")

# How do I find unique product codes in SQL?

Country reports

reportercomtrade %>% select(reporter, region) %>% 
    mutate(countryreport = paste0("<a href='../../reports/countries/",reporter,".pdf'>",reporter,".pdf</a>")) %>%
    kable 

\newpage

Links to completeness reports

classificationitto %>% select(product, productcodecomtrade) %>% 
    mutate(completenessreport = paste0("<a href='../../reports/completeness/",productcodecomtrade,".pdf'>",productcodecomtrade,".pdf</a>")) %>%
    kable

\newpage

Links to discrepancy reports

You might want to show a link only if the combination of product and country exists.

Logs

discrepancytablelinks <- function(product_){
    products <- classificationitto %>% 
        select(product, productcodecomtrade) %>%
        filter(product == product_) %>% unique
    countries <- reportercomtrade %>% 
        select(reporter, region)
    expand.grid(productcode = products$productcodecomtrade,
                reporter = countries$reporter) %>%
        mutate(variable = paste0("<a href='../../reports/discrepancies/",productcode,reporter,".pdf'>",productcode,reporter,".pdf</a>")) %>%
        dcast(reporter ~ productcode) %>% 
        kable
}
discrepancytablelinks("LOGS")

Sawnwood

discrepancytablelinks("SAWNWOOD")


EuropeanForestInstitute/tradeflows documentation built on Oct. 7, 2019, 10:57 a.m.