Last data update: 2014.03.03

R: Read and write data models for LaF
read_dmR Documentation

Read and write data models for LaF

Description

Using these routines data models can be written and read. These data models can be used to create LaF object without the need to specify all arguments (column names, column types etc.). Opening of files using the data models can be done using laf_open.

Usage

read_dm(modelfile, ...)

write_dm(model, modelfile)

Arguments

modelfile

character containing the filename of the file the model is to be written to/read from.

model

a data model or an object of type laf. See details for more information.

...

additional arguments are added to the data model or, when they are also present in the file are used to overwrite the values specified in the file.

Details

A data model is a list containing information which open routine should be used (e.g. laf_open_csv or laf_open_fwf), and the arguments needed for these routines. Required elements are ‘type’, which can (currently) be ‘csv’, or ‘fwf’, and ‘columns’, which should be a data.frame containing at least the columns ‘name’ and ‘type’, and for fwf ‘width’. These columns correspond to the arguments column_names, column_types and column_widths respectively. Other arguments of the laf_open_* routines can be specified as additional elements of the list.

write_dm can also be used to write a data model that is created from an object of type laf. This is probably one of the easiest ways to create a data model.

The data model is stored in a text file in YAML format which is a format in which data structures can be stored in a readable and editable format.

Value

read_dm returns a data model which can be used by laf_open.

See Also

See detect_dm_csv for a routine which can automatically create a data model from a CSV-file. The data models can be used to open a file using laf_open.

Examples

# 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="tmp.csv", row.names=FALSE, col.names=FALSE, sep=',')

# Create LaF-object
laf <- laf_open_csv("tmp.csv", column_types=column_types)

# Write data model to stdout() (screen)
write_dm(laf, stdout())

# Write data model to file
write_dm(laf, "tmp.yaml")

# Read data model and open file
laf2 <- laf_open(read_dm("tmp.yaml"))

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

# Read data model and open seconde file, demonstrating the use of the optional
# arguments to read_dm
laf2 <- laf_open(read_dm("tmp.yaml", filename="tmp2.csv"))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(LaF)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/LaF/datamodels.Rd_%03d_medium.png", width=480, height=480)
> ### Name: read_dm
> ### Title: Read and write data models for LaF
> ### Aliases: read_dm write_dm
> 
> ### ** Examples
> 
> # 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="tmp.csv", row.names=FALSE, col.names=FALSE, sep=',')
> 
> # Create LaF-object
> laf <- laf_open_csv("tmp.csv", column_types=column_types)
> 
> # Write data model to stdout() (screen)
> write_dm(laf, stdout())
Loading required namespace: yaml
type: csv
filename: tmp.csv
sep: ','
dec: .
skip: 0
trim: no
columns:
- name: V1
  type: integer
- name: V2
  type: integer
- name: V3
  type: double
- name: V4
  type: string

> 
> # Write data model to file
> write_dm(laf, "tmp.yaml")
> 
> # Read data model and open file
> laf2 <- laf_open(read_dm("tmp.yaml"))
> 
> # Write test data to second csv file
> write.table(testdata, file="tmp2.csv", row.names=FALSE, col.names=FALSE, sep=',')
> 
> # Read data model and open seconde file, demonstrating the use of the optional
> # arguments to read_dm
> laf2 <- laf_open(read_dm("tmp.yaml", filename="tmp2.csv"))
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>