.dict_rhodium_names <- function() {
tibble(
nameRhodium = c(
"month",
"year",
"millions",
"direction",
"millionsPerQuarter",
"stake",
"type",
"investorOwnership",
"mode",
"industry",
"targetKey",
"sourceKey",
"sourceCountry"
),
nameActual = c(
"monthTransaction",
"yearTransaction",
"amountInvestment",
"typeDirection",
"amountInvestmentCumulativeQuarter",
"typeStake",
"typeStrategy",
"typeInvestor",
"typeInvestment",
"sectorInvestment",
"stateInvestment",
"stateInvestor",
"countryInvestor"
)
)
}
# chinese_fdi -------------------------------------------------------------
## https://www.us-china-investment.org/us-china-foreign-direct-investments/getData
#' Rhodium group China-US investment tracker
#'
#' Acquires cross border investment data from Rhodium Group US-China investment tracker
#'
#'
#' @param snake_names if `TRUE` return snake case names
#' @param unformat if `TRUE` unformat currency columns
#' @param return_message if `TRUE` unformat columns
#'
#' @return \code{tibble()}
#' @family china
#' \url{https://www.us-china-investment.org}
#' @export
#'
#' @examples
#' \dontrun{
#' rhodium_chinese_fdi()
#' }
rhodium_chinese_fdi <-
function(snake_names = F,
unformat = F,
return_message = T) {
url = "https://www.us-china-investment.org/us-china-foreign-direct-investments/getData"
json_data <- fromJSON(url, simplifyDataFrame = T, flatten = T)
industries <- json_data$filters$industry
data <- json_data$docs %>% as_tibble()
names(data) <- names(data) %>% str_remove_all("rowParams.")
data <- data %>%
mutate(idRow = 1:n()) %>%
select(idRow, everything())
df_q <- data %>%
select(idRow, quarters)
df_q <- seq_along(1:nrow(df_q)) %>%
future_map_dfr(function(x) {
df_q %>% dplyr::slice(x) %>%
unnest() %>%
mutate_all(as.numeric)
})
df_q <- df_q %>% group_by(idRow) %>% dplyr::slice(1) %>% ungroup()
data <-
data %>% select(-quarters) %>% left_join(df_q, by = "idRow") %>% select(one_of(names(df_q)), everything()) %>%
arrange(desc(year)) %>%
select(-idRow)
dict_names <- .dict_rhodium_names()
actual_names <- names(data) %>%
map_chr(function(name) {
dict_names %>% filter(nameRhodium == name) %>% pull(nameActual)
})
data <- data %>%
setNames(actual_names) %>%
mutate(isGreenfield = typeInvestment == "Greenfield") %>%
mutate_at(c("amountInvestmentCumulativeQuarter", "amountInvestment"),
list(function(x) {
x * 1000000 %>% currency(digits = 0)
}))
data <-
data %>%
mutate(
dateTransaction = glue("{yearTransaction}-{monthTransaction}-01") %>% ymd() %m+% months(1) - 1
) %>%
select(
dateTransaction,
sectorInvestment,
typeDirection,
typeStake,
typeInvestment,
typeInvestor,
countryInvestor,
stateInvestment,
stateInvestment,
amountInvestment,
everything()
)
if (return_message) {
t_count <- nrow(data) %>% formattable::comma(digits = 0)
total_inv <- data$amountInvestment %>% sum()
us_count <-
data %>% filter(countryInvestor == "usa") %>% nrow() %>% comma(digits = 0)
us_amt <-
data %>% filter(countryInvestor == "usa") %>% pull(amountInvestment) %>% sum()
china_count <-
data %>% filter(countryInvestor == "china") %>% nrow() %>% comma(digits = 0)
china_amt <-
data %>% filter(countryInvestor == "china") %>% pull(amountInvestment) %>% sum()
start <- data$dateTransaction %>% min()
end <- data$dateTransaction %>% max()
glue(
"{t_count} Chinese/US investments totaling {total_inv} from {start} to {end}\n\n{us_count} US to China investments totaling {us_amt}\n\n{china_count} China to US investments totaling {china_amt}"
) %>% message()
}
data <- data %>%
munge_data(snake_names = snake_names, unformat = unformat)
data
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.