# R 言語ドライバのインストール この記事では、[RPresto](https://rpubs.com/chezou/TD-from-RPresto-RTD) パッケージを使用して、[R 言語](http://www.r-project.org/) で Treasure Data を使用する方法を説明します。 * [RPresto パッケージのインストール](#install-the-rpresto-package) * [ローカルエンドポイント](#your-local-endpoint) * [クエリの発行](#issuing-queries) # RPresto パッケージのインストール 1. R コンソールを開きます。 2. 次に、[RPresto](https://rpubs.com/chezou/TD-from-RPresto-RTD) と [RTD](https://github.com/treasure-data/RTD/) パッケージを次のようにインストールします: ```r install.packages(c("RPresto", "devtools", "dplyr", "dbplyr", "ggplot2")) devtools::install_github("treasure-data/RTD") devtools::install_github("crowding/msgpack-r") ``` R 言語に RODBC lib を使用する場合は、代わりに RJDBC lib を使用してください。RJDBC を使用すると、汎用の Trino/Presto JDBC ドライバを設定できます。R 言語から Treasure Data Trino にアクセスできるようになります。 # ローカルエンドポイント 以下のエンドポイントを使用してこの機能にアクセスできます。RPresto の場合は Presto JDBC を、RTD では API が使用される可能性があります。[Treasure Data サイトとエンドポイント](https://api-docs.treasuredata.com/en/sdk/unity-sdk/quickstart/#endpoint)の詳細をご覧ください。 | **サイト** | **EU** | **Korea (ベータ版)** | **US** | **Tokyo** | | --- | --- | --- | --- | --- | | **Presto JDBC/ODBC** | [api-presto.eu01.treasuredata.com](http://api-presto.eu01.treasuredata.com) | [api-presto.ap02.treasuredata.com](http://api-presto.ap02.treasuredata.com) | [api-presto.treasuredata.com](http://api-presto.treasuredata.com) | [api-presto.treasuredata.co.jp](http://api-presto.treasuredata.co.jp) | | **API** | [api.eu01.treasuredata.com](http://api.eu01.treasuredata.com) | [api.ap02.treasuredata.com](http://api.ap02.treasuredata.com) | [api.treasuredata.com](http://api.treasuredata.com) | [api.treasuredata.co.jp](http://api.treasuredata.co.jp) | # クエリの発行 以下の例でクエリを実行できます。`'test'` データベースに `'flights'` テーブルがあると仮定して、TD API キーの環境変数 <`TD_API_KEY`> を設定する必要があります。 別の地域を使用するには、`host` を目的の地域に置き換えます。 * [dplyr](https://dplyr.tidyverse.org/) パッケージの使用 * [DBI](https://db.rstudio.com/dbi/) パッケージの使用 ## 例 1 (RPresto と dplyr を使用): ```r library(RPresto) library(dplyr) db <- src_presto( host="https://api-presto.treasuredata.com", port=443, user=Sys.getenv("TD_API_KEY"), schema='test', catalog='td-presto' ) flights_tbl <- tbl(db, 'flights') # filter by departure delay and show result flights_tbl %>% filter(dep_delay == 2) ``` ## 例 2 (RPresto と DBI を使用): ```r library(DBI) con <- dbConnect( RPresto::Presto(), host="https://api-presto.treasuredata.com", port=443, user=Sys.getenv("TD_API_KEY"), schema='test', catalog='td-presto' ) # write your query with dbGetQuery function flights_preview <- dbGetQuery(con, 'SELECT year, month, day, dep_time, dep_delay, carrier, flight from flights limit 10') # show query result flights_preview ``` [サンプルノートブック](https://rpubs.com/chezou/TD-from-RPresto-RTD) をご覧ください。