View source: R/data-management.R
| opendata | R Documentation |
Reads local/remote files and connects to supported research data sources. Existing file and REDCap behavior is retained. Additional connectors support KoboToolbox, Google Forms, Google Sheets, Microsoft Forms/Excel, and MySQL/MariaDB.
opendata(
file = NULL,
api = NULL,
url = NULL,
token = NULL,
vars = NULL,
obs = NULL,
active = FALSE,
labels = c("factor", "labelled", "numeric"),
header = TRUE,
sep = NULL,
sheet = 1,
range = NULL,
skip = 0,
na = c("", "NA"),
encoding = "UTF-8",
check.names = FALSE,
records = NULL,
fields = NULL,
forms = NULL,
events = NULL,
quiet = FALSE,
uid = NULL,
server = NULL,
form = NULL,
access_token = NULL,
question_names = c("id", "title"),
sheet_id = NULL,
drive_id = NULL,
item_id = NULL,
table = NULL,
page_size = 1000L,
host = "localhost",
port = 3306L,
dbname = NULL,
database = NULL,
user = NULL,
password = NULL,
query = NULL,
ssl_ca = NULL,
ssl_cert = NULL,
ssl_key = NULL,
db_timeout = 10,
bigint = "integer64",
...
)
file |
File path or web URL. A bare file name is first resolved in the
current working directory and, when not found there, in R4VN's bundled
|
api |
Optional source name: |
url |
API/project/form/sheet URL when applicable. |
token |
REDCap or KoboToolbox API token. For Google/Microsoft, use
|
vars |
Optional variables to retain. |
obs |
Optional observations to retain. |
active |
Logical; also place a working copy in active memory. |
labels |
How imported value labels are handled: |
header |
Logical; first text/spreadsheet row contains names. |
sep |
Text-file delimiter. Defaults from extension. |
sheet |
Excel/online workbook sheet name or number. |
range |
Excel/online workbook cell range such as |
skip |
Number of rows to skip for local files. |
na |
Strings interpreted as missing. |
encoding |
Text encoding. |
check.names |
Logical; make names syntactically valid. |
records, fields, forms, events |
Optional REDCap filters. |
quiet |
Logical; suppress summary messages. |
uid |
KoboToolbox asset UID. May be inferred from a Kobo project URL. |
server |
KoboToolbox server. Defaults to the Global server and may be
inferred from |
form |
Google Forms form ID. May be inferred from a compatible form URL. |
access_token |
OAuth bearer token for Google or Microsoft APIs. |
question_names |
Google Forms variable naming: stable question |
sheet_id |
Google Sheets spreadsheet ID. May be inferred from |
drive_id, item_id |
Microsoft Graph drive/item identifiers for the Excel workbook containing Microsoft Forms responses. |
table |
Microsoft Excel table name, or MySQL/MariaDB table name. |
page_size |
Number of records requested per API page. |
host |
MySQL/MariaDB server host. |
port |
MySQL/MariaDB TCP port, usually 3306. |
dbname, database |
MySQL/MariaDB database name. |
user, password |
MySQL/MariaDB credentials. A read-only database account is strongly recommended. |
query |
Optional read-only SQL query. Use this instead of |
ssl_ca, ssl_cert, ssl_key |
Optional SSL CA/certificate/key paths for MySQL/MariaDB. |
db_timeout |
Database connection timeout in seconds. |
bigint |
How 64-bit database integers are returned; passed to RMariaDB. |
... |
Additional arguments for the existing format-specific local reader. |
Existing local-file and REDCap behavior is unchanged. When file is a bare
file name that does not exist in the current working directory, opendata()
also looks in the package's bundled extdata directory. This makes the
teaching dataset available simply as opendata("ivf_v3_vi.dta") after
R4VN is installed.
KoboToolbox uses API v2 and Token authentication. Google Forms direct access uses the official Forms API and OAuth. Google Sheets public share links can be read without OAuth when the sheet is accessible to anyone with the link; private sheets can be read with an OAuth access token.
Google Forms and Microsoft Forms response files exported to CSV/XLSX can be
opened directly with opendata(file). api = "googleform" or
api = "msforms" may also be supplied together with file as a friendly
alias; in that case the normal file reader is used.
Microsoft Forms direct response access is handled through its linked Excel workbook via Microsoft Graph. MySQL/MariaDB uses DBI + RMariaDB and closes the database connection automatically before returning.
A data frame.
# Bundled R4VN teaching dataset (Stata format).
if (requireNamespace("haven", quietly = TRUE) ||
requireNamespace("readstata13", quietly = TRUE)) {
ivf <- opendata("ivf_v3_vi.dta", quiet = TRUE)
head(ivf)
}
## Not run:
# Existing REDCap behavior
redcap <- opendata(
api = "redcap",
url = "https://example.org/api/",
token = "REDCAP_TOKEN",
active = TRUE
)
# KoboToolbox
kobo <- opendata(
api = "kobo",
uid = "aBcDeFg123",
token = "KOBO_TOKEN",
active = TRUE
)
# Public Google Sheet using only a share link
gsheet <- opendata(
api = "gsheet",
url = "https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit?gid=0",
active = TRUE
)
# Named tab and range from a public Google Sheet
gsheet2 <- opendata(
api = "gsheet",
url = "https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit",
sheet = "Responses",
range = "A1:H500"
)
# Google Forms / Microsoft Forms: easiest route after exporting responses
gform_export <- opendata("google-form-responses.xlsx")
msform_export <- opendata("microsoft-form-responses.xlsx")
# Direct Google Forms API (OAuth token required)
gform <- opendata(
api = "googleform",
form = "FORM_ID",
access_token = "GOOGLE_OAUTH_ACCESS_TOKEN"
)
# Microsoft Forms linked Excel workbook via Microsoft Graph
msform <- opendata(
api = "msforms",
item_id = "WORKBOOK_ITEM_ID",
access_token = "MS_GRAPH_ACCESS_TOKEN"
)
# MySQL table
mysql_data <- opendata(
api = "mysql",
host = "db.example.org",
dbname = "research",
user = "reader",
password = "PASSWORD",
table = "participants"
)
# MySQL read-only query
mysql_subset <- opendata(
api = "mysql",
host = "db.example.org",
database = "research",
user = "reader",
password = "PASSWORD",
query = "SELECT id, age, sex FROM participants WHERE age >= 18"
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.