私はあなたの疑問を解決する可能性が高いです(それはブログの投稿がSOのものよりも多いからです)。しかし、おそらくこのQ & Aつまずく他の人に有用であり得るの下にあり、いくつかの事柄:
#' Convert currencies using Google Finance calculator
#'
#' @note from, to and qty lengths must meet the following conditions
#'
#' - from, to and qty must all be length 1, *OR*
#' - from, to and qty must all be the _same_ length, *OR*
#' - from can be any length, to can be length 1 and qty can be length 1 or the length of from
#'
#' @param from,to character vectors of currencies to convert from and to.
#' @param qty numeric vector of "from" quantities to convert to "to"
currency_converter <- function(from, to = "USD", qty = 1) {
# require() only has "real" overhead the first time but we need to
# ensure these dependencies are loaded and this isn't in a package
require(httr, quietly=TRUE, warn.conflicts = FALSE)
require(xml2, quietly=TRUE, warn.conflicts = FALSE)
require(rvest, quietly=TRUE, warn.conflicts = FALSE)
require(purrr, quietly=TRUE, warn.conflicts = FALSE)
require(dplyr, quietly=TRUE, warn.conflicts = FALSE)
# Local environment variables we'll use liberally in the nested function
meta <- valid_froms <- valid_tos <- NULL
# Performs a single conversion
.currency_converter <- function(.from, .to, .qty) {
Sys.sleep(5) # just b/c *you* want something for free doesn't give you the right to abuse resources
if (is.na(.qty)) return(NULL)
# Prime "meta" & validators with an initial call to the calculator
# we'll keep updating "meta" in the calculator call until all
# conversions are done
if (is.null(meta)) {
prime <- GET("https://finance.google.com/finance/converter")
stop_for_status(prime) # shld only happen if you abuse the API or lose network
prime <- content(prime, as="parsed")
meta <<- html_attr(html_node(prime, "input[name='meta']"), "value")
# Get valid from/to & store for later use
valid_froms <<- html_attr(html_nodes(prime, "select[name='from'] > option"), "value")
valid_tos <<- html_attr(html_nodes(prime, "select[name='to'] > option"), "value")
}
# Make sure the currencies are valid but don't abort if not, just
# return a data frame with NA for the converted value
if (!(.from %in% valid_froms)) return(data_frame(from = .from, from_qty = .qty, to = .to, to_qty = NA_real_))
if (!(.to %in% valid_tos)) return(data_frame(from = .from, from_qty = .qty, to = .to, to_qty = NA_real_))
# Now make the API call
httr::GET(
url = "https://finance.google.com/finance/converter",
query = list(a = .qty, from = .from, to = .to, meta = meta)
) -> res
stop_for_status(res) # shld only happen if you abuse the API or lose network
res <- content(res, as="parsed")
res <- html_text(html_node(res, "div#currency_converter_result > span"))
res <- as.numeric(sub(" .*$", "", res))
data_frame(from = .from, from_qty = .qty, to = .to, to_qty = res)
}
# Ensure valid params
if (length(from) > 1) {
if (length(to) > 1) {
if (length(to) != length(from)) {
stop("`from` and `to` must either be the same length or `to` must be a single currency", call.=FALSE)
}
} else {
to <- rep(to, length(from))
}
if (length(qty) > 1) {
if (length(qty) != length(from)) {
stop("`from`, `to` and `qty` must either be the same length or `qty` must be a single currency", call.=FALSE)
}
} else {
qty <- rep(qty, length(from))
}
}
pmap_dfr(
data_frame(
.from = trimws(toupper(from)),
.to = trimws(toupper(to)),
.qty = as.numeric(qty)
),
.currency_converter
)
}
は、それが動作するかどうか見てみましょう。ここでは
currency_converter("SOS")
## # A tibble: 1 x 4
## from from_qty to to_qty
## <chr> <dbl> <chr> <dbl>
## 1 SOS 1 USD 0.0017
currency_converter(c("EUR", "GBP", "JPY", "CHF"))
## # A tibble: 4 x 4
## from from_qty to to_qty
## <chr> <dbl> <chr> <dbl>
## 1 EUR 1 USD 1.1570
## 2 GBP 1 USD 1.3138
## 3 JPY 1 USD 0.0087
## 4 CHF 1 USD 0.9992
currency_converter(
c("EUR", "GBP", "JPY", "CHF"),
rev(c("EUR", "GBP", "JPY", "CHF"))
)
## # A tibble: 4 x 4
## from from_qty to to_qty
## <chr> <dbl> <chr> <dbl>
## 1 EUR 1 CHF 1.1580
## 2 GBP 1 JPY 150.2182
## 3 JPY 1 GBP 0.0067
## 4 CHF 1 EUR 0.8634
set.seed(8675309)
currency_converter(c("EUR", "GBP", "JPY", "CHF"), "ZAR", sample(20, 4))
## # A tibble: 4 x 4
## from from_qty to to_qty
## <chr> <dbl> <chr> <dbl>
## 1 EUR 4 ZAR 65.6464
## 2 GBP 10 ZAR 186.5290
## 3 JPY 14 ZAR 1.7388
## 4 CHF 18 ZAR 255.2202
currency_converter(
c("EUR", "GBP", "JPY", "ZZZ"),
rev(c("EUR", "ZZZ", "JPY", "CHF")),
sample(20, 4)
)
## # A tibble: 4 x 4
## from from_qty to to_qty
## <chr> <dbl> <chr> <dbl>
## 1 EUR 6 CHF 6.948
## 2 GBP 13 JPY 1952.600
## 3 JPY 18 ZZZ NA
## 4 ZZZ 15 EUR NA
は、私が使用している有用な技術でありますExcelのスプレッドシートhttps://office-watch.com/2016/excel-stock-prices-from-google-finance/でyahoo株価を置き換えてください。継続的に更新されるGoogleスプレッドシートの使い方は通貨でもうまくいくかもしれませんが、Excelと同じようにRに簡単にインポートできます。 –
私はfinservタイプが常にExcelを実行している可能性が高いと認識していますが、それはしばしば危険に苦しむ不要な依存性です。 – hrbrmstr