# R Language Driver Install This article shows how to use Treasure Data with the [R language](http://www.r-project.org/) by using the [RPresto](https://rpubs.com/chezou/TD-from-RPresto-RTD) package. * [Install the RPresto Package](#install-the-rpresto-package) * [Your Local Endpoint](#your-local-endpoint) * [Issuing Queries](#issuing-queries) # Install the RPresto Package 1. Open the R Console. 2. Next, install the [RPresto](https://rpubs.com/chezou/TD-from-RPresto-RTD) and [RTD](https://github.com/treasure-data/RTD/) Package as shown: ```r install.packages(c("RPresto", "devtools", "dplyr", "dbplyr", "ggplot2")) devtools::install_github("treasure-data/RTD") devtools::install_github("crowding/msgpack-r") ``` In cases where RODBC lib is used for R Language, use RJDBC lib instead. RJDBC allows you to configure a generic Trino/Presto JDBC Driver. You'll be able to access Treasure Data Trino from R Language. # Your Local Endpoint You can use the endpoints below to access this feature. You can point to Presto JDBC for RPresto and API might be used by RTD. Learn more about [Treasure Data Sites and Endpoints](https://api-docs.treasuredata.com/en/sdk/unity-sdk/quickstart/#endpoint). | **Site** | **EU** | **Korea (in beta)** | **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) | # Issuing Queries You can query with the following examples. Assuming there is a `‘flights’` table in `‘test’` database, you then need to set an environment variable <`TD_API_KEY`> for your TD API key. To use a different region, replace `host` with the desired region. * Using [dplyr](https://dplyr.tidyverse.org/) package * Using [DBI](https://db.rstudio.com/dbi/) package ## Example 1 (using RPresto and 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) ``` ## Example 2 (using RPresto and 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 ``` View [an example notebook](https://rpubs.com/chezou/TD-from-RPresto-RTD).