Learn R Programming

civis (version 1.6.1)

read_civis: Read a table or file from the Civis Platform as a data frame

Description

read_civis loads a table from Redshift as a data frame if given a "schema.table" or sql("query") as the first argument, or loads a file from Amazon S3 (the files endpoint) if a file id is given.

A default database can be set using options(civis.default_db = "my_database"). If there is only one database available, this database will automatically be used as the default.

Usage

read_civis(x, ...)

# S3 method for numeric read_civis(x, using = readRDS, verbose = FALSE, ...)

# S3 method for character read_civis(x, database = NULL, ...)

# S3 method for sql read_civis(x, database = NULL, using = utils::read.csv, job_name = NULL, hidden = TRUE, verbose = FALSE, ...)

Arguments

x

"schema.table", sql("query"), or a file id.

...

arguments passed to using.

using

function, Function to convert the file to a data frame or to unserialize. the file (e.g. read.csv or readRDS).

verbose

bool, Set to TRUE to print intermediate progress indicators.

database

string, Name of database where data frame is to be uploaded. If no database is specified, uses options(civis.default_db).

job_name

string, Name of the job (default: "Civis Export Via R Client").

hidden

bool, Whether the job is hidden.

Methods (by class)

  • numeric: Return a file as a data frame

  • character: Return all columns from a table as a data frame.

  • sql: Return a SQL query as a data frame.

Details

For read_civis.sql, queries must be READ ONLY. To execute arbitrary queries, use query_civis.

By default, read_civis.numeric assumes the file has been serialized using saveRDS, as in write_civis_file and uses using = readRDS by default. For reading an uncompressed text or csv from the files endpoint, set using = read.csv for example.

See Also

Other io: download_civis, query_civis_file, query_civis, write_civis_file, write_civis

Examples

Run this code
# NOT RUN {
# Read all columns in a single table
df <- read_civis("schema.my_table", database = "my_database")

# Read data from a SQL select statement (READ ONLY)
query <- sql("SELECT * FROM table JOIN other_table USING id WHERE var1 < 23")
df <- read_civis(query, database = "my_database")

# Read an R object from the files endpoint.
id <- write_civis_file(df)
df <- read_civis(id)

# Read a text file or csv from the files endpoint.
id <- write_civis_file("my_csv.csv")
df <- read_civis(id, using = read.csv)

# Gracefully handle when read_civis.sql returns no rows
query <- sql("SELECT * FROM table WHERE 1 = 2")
mean_x <- tryCatch({
  df <- read_civis(query, database = "my_database")
  mean(df$x)
}, empty_result_error = function(e) {
   NA
})
# }

Run the code above in your browser using DataLab