message("You can now develop reports directly in the inst/templates folder !!!") message("There is a way to generate them without building the package by specifying the template inputpath") # I tried placing the yaml block after the chunks but for some reason it doesn't always generate the pdf and generates a html file instead. # pdf is generated when the yaml block is at the top, library(knitr) opts_knit$set(root.dir="../../..") # file paths are relative to the root of the project directory opts_chunk$set(echo=FALSE) library(tradeflows) library(dplyr) library(ggplot2) library(reshape2)
######### # # ! 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 # Product and reporter information can be extracted back from tfdata # in the template this would have to be the country appearing most in the data frame productcodeinreport <- c(440799) reporterinreport <- "Indonesia" # Test countries: Cameroon, Ghana, Indonesia tfdata <- readdbtbl("raw_flow_yearly") %>% filter(productcode == productcodeinreport & (reporter == reporterinreport | partner == reporterinreport)) %>% collect()
numberoflargecountries <- 27 # Extract unit and check that function arguments reporterinreport and productcodeinreport are consistent with the data frame metadata <- extractmetadata(tfdata) stopifnot(metadata$productcode == productcodeinreport) stopifnot(metadata$reporter == reporterinreport) if(metadata$unit == "Volume in cubic meters") metadata$unit <- "cubic meters" # Calculate discrepancies tfdata <- tfdata %>% addpartnerflow %>% calculatediscrepancies # Select the last year available for which at least half of the countries have # quantity data lastyearreporter <- tfdata %>% filter(reporter == reporterinreport) %>% mutate(naquantity = is.na(quantity)) %>% group_by(year) %>% summarise(naratio = sum(naquantity)/n()) %>% filter(naratio<0.5 & year == max(year)) lastyearreporter <- lastyearreporter$year yearinreport <- lastyearreporter # Display number of years available only if other countries reported # data for more years if (lastyearreporter != max(tfdata$year)){ messageyears = paste("Data for other countries is available until", max(tfdata$year),".") } else { messageyears = "" }
r reporterinreport
reported importing from
r length(unique(tfdata$partner[tfdata$reporter==reporterinreport & tfdata$flow=="Import"]))
countries and exporting to
r length(unique(tfdata$partner[tfdata$reporter==reporterinreport & tfdata$flow=="Export"]))
countries
between r min(tfdata$year)
and r lastyearreporter
.
r messageyears
Trade value is reported in US dollars and quantity in r metadata$unit
.
tfsum <- tfdata %>% mutate(isreporter = ifelse(reporter == reporterinreport, "", "partners")) # Switch import and export for the partners tfsum[tfsum$isreporter=="partners",] <- tfsum[tfsum$isreporter=="partners",] %>% mutate(flow = gsub("Import","aaaaaaa",flow), flow = gsub("Export","Import",flow), flow = gsub("aaaaaaa","Export",flow)) tfsum %>% group_by(year,flow, isreporter) %>% summarise(nrow = n(), value = sum(tradevalue, na.rm=TRUE), quantity = sum(quantity, na.rm=TRUE)) %>% melt(id = c("year","flow","isreporter")) %>% dcast(year + flow ~ isreporter + variable) %>% kable
\newpage
r yearinreport
r as.character(numberoflargecountries)
largest import or export flows reported by r reporterinreport
.
Trade value is reported in US dollars and quantity in r metadata$unit
.
# Select the largest import or export flows reported by the reporter largestreporter <- tfdata %>% filterworldeu28 %>% filter(year == yearinreport & reporter == reporterinreport) %>% arrange(desc(quantity)) %>% head(numberoflargecountries) # Display latest trade flows as a table largestreporter %>% select(year, reporter, partner, flow, flag, tradevalue, quantity, dicrepancy_quantity = discrq) %>% kable # Display these trade flows as a plot largestreporter <- tfdata %>% filterworldeu28 %>% # Keep the same combination of flow / country filter(paste0(flow,partner) %in% paste0(largestreporter$flow, largestreporter$partner)) %>% # reshape table for use in the plot select(year, partner, flow, quantityreporter, quantitypartner) %>% arrange(-quantityreporter) %>% rowwise() %>% mutate(qmin = min(quantityreporter, quantitypartner), qmax = max(quantityreporter, quantitypartner)) %>% # rename(discrepancy = discrq) %>% melt(id = c("year","partner","flow", "qmin","qmax")) ggplot(largestreporter, aes(x = year, y = value, fill = variable)) + geom_bar(position="dodge", stat="identity") + scale_fill_manual(values = c("chocolate3","black"))+ geom_errorbar(aes(ymin = qmin, ymax = qmax, width=0.2, colour="discrepancy")) + scale_colour_manual(name="error bars",values="red") + ylab(metadata$unit) + facet_wrap(flow~partner, scales = "free_y", ncol = 3) + theme_bw() + theme(legend.position= "bottom", legend.box="horizontal") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
\newpage
Relative discrepancy is equal to $$(quantitypartner - quantity)/ (quantity + quantitypartner)$$
## Trade flows as a point cloud tfdata <- tfdata %>% filterworldeu28 %>% addregion ggplot(tfdata, aes(x = quantity, y = reldiscrq, color = regionpartner)) + geom_point() + facet_wrap(~flow, scales="free_x") ggplot(tfdata, aes(x = tradevalue, y = reldiscrv, color = regionpartner)) + geom_point() + facet_wrap(~flow, scales = "free_x")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.