laf_open: Create a connection to a file using a data model.

Description Usage Arguments Details Value See Also Examples

View source: R/meta.R

Description

Uses a data model to create a connection to a file. The data model contains all the information needed to open the file (column types, column widths, etc.).

Usage

1
laf_open(model, ...)

Arguments

model

a data model, such as one returned by read_dm or detect_dm_csv.

...

additional arguments can be used to overwrite the values specified by the data model. These are listed in the argument documentation for laf_open_csv and laf_open_fwf, e.g. see ignore_failed_conversion.

Details

Depending on the field ‘type’ laf_open uses laf_open_csv and laf_open_fwf to open the file. The data model should contain all information needed by these routines to open the file.

Value

Object of type laf. Values can be extracted from this object using indexing, and methods such as read_lines, next_block.

See Also

See read_dm and detect_dm_csv for ways of creating data models.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Create some temporary files
tmpcsv  <- tempfile(fileext="csv")
tmp2csv <- tempfile(fileext="csv")
tmpyaml <- tempfile(fileext="yaml")

# Generate test data
ntest <- 10
column_types <- c("integer", "integer", "double", "string")
testdata <- data.frame(
    a = 1:ntest,
    b = sample(1:2, ntest, replace=TRUE),
    c = round(runif(ntest), 13),
    d = sample(c("jan", "pier", "tjores", "corneel"), ntest, replace=TRUE)
    )
# Write test data to csv file
write.table(testdata, file=tmpcsv, row.names=FALSE, col.names=FALSE, sep=',')

# Create LaF-object
laf <- laf_open_csv(tmpcsv, column_types=column_types)

# Write data model to file
write_dm(laf, tmpyaml)

# Read data model and open file
laf <- laf_open(read_dm(tmpyaml))

# Write test data to second csv file
write.table(testdata, file=tmp2csv, row.names=FALSE, col.names=FALSE, sep=',')

# Read data model and open second file, demonstrating the use of the optional
# arguments to laf_open
laf2 <- laf_open(read_dm(tmpyaml), filename=tmp2csv)

# Cleanup
file.remove(tmpcsv)
file.remove(tmp2csv)
file.remove(tmpyaml)

LaF documentation built on March 26, 2020, 6:59 p.m.