write_matrix: Write a matrix to a file without row or column names

View source: R/write_matrix.R

write_matrixR Documentation

Write a matrix to a file without row or column names

Description

The inverse function of read_matrix(), this writes what is intended to be a numeric matrix to a tab-delimited file without row or column names present. It uses readr::write_tsv() to do it efficiently. Intended for outputs such as those of admixture inference approaches.

Usage

write_matrix(file, x, ext = "txt", verbose = TRUE, append = FALSE)

Arguments

file

Output file (whatever is accepted by readr::write_tsv()). If file is missing the expected extension (see below), the function adds it.

x

The matrix to write. Unlike read_matrix(), this is not in fact required to be a matrix or be strictly numeric; anything that coerces to tibble or data.frame is acceptable.

ext

The desired file extension. If NA, no extension is added. Works if file already contains desired extension.

verbose

If TRUE (default), function reports the path of the file being written (after autocompleting the extension).

append

If TRUE, appends rows onto the file. (Default is FALSE).

Value

The output x, coerced into data.frame, invisibly (what readr::write_tsv() returns).

See Also

read_matrix(), the inverse function.

Examples

# to write an existing matrix `x` into file "data.txt", run like this:
# write_matrix( "data", x )
# this also works
# write_matrix( "data.txt", x )

# The following example is more detailed but also more awkward
# because (only for these examples) the package must create the file in a *temporary* location

# create a dummy matrix with the right columns
x <- rbind( 1:3, (0:2)/10, -1:1 )
# a dummy file
file_out <- tempfile('delete-me-example', fileext = '.txt') # will also work without extension
# write the matrix without header
write_matrix( file_out, x )
# delete output when done
file.remove( file_out )


genio documentation built on Jan. 7, 2023, 1:12 a.m.