This vignette demonstrates functions in the files
range.
library(shrt)
It will be handy to work with a small table
d1 = mtrx(letters[1:8], col.names=LETTERS[1:4], row.names=c("R1", "R2")) d1
wtht
is a wrapper for write.table
which by default has row.names=FALSE
,
col.names=TRUE
, quote=FALSE
, and sep='\t'
. The basic usage save a table
into a specified file
wtht(d1, file="test.1.tsv")
wtht
also provides a way to record row names in a named column. In this mode,
the function creates a temporary data structure with a new column, and save
this new structure to disk.
wtht(d1, file="test.2.tsv", rowid.column="rowid")
Refer to the next section to see the result from these commands.
rtht
is a wrapper for read.table
which by default sets header=TRUE
,
sep='\t'
, and stringsAsFactors=FALSE
.
We can use this function to see the files saved above. We can read the first table.
d2 = rtht("test.1.tsv") d2
Note that row names are absent in this object - they are not recorded in the file.
The second file we created was meant to contain row names in a new column. We can verify this by reading the file with the same syntax as above
d3 = rtht("test.2.tsv") d3
Note here that the table contains a column rowid
that did not appear in the
original dataset. We can optionally eliminate this column whilst reading the
data.
d4 = rtht("test.2.tsv", rowid.column="rowid") d4
This result is now almost equivalent to the orignal data. (Note d1
was a
matrix, but reading a table from disk always returns a data frame.)
unlink("test.1.tsv") unlink("test.2.tsv")
load1
retrieves data from a file on disk in a similar way to load
. However,
load1
is specialized to loading single objects from a file. This is handy in
situations such as demonstrated below.
Let's save our matrix into an Rda file
save(d1, file="test.Rda")
The best way to check that the data is really saved would be to load it into a new R session. Here, we can achieve a similar effect by removing the variable from the environment and then loading it from the file.
rm(d1)
d1
As expected, this returned an error. But we can now load the data from the saved file.
load("test.Rda") d1
A 'feature' of the standard load
function is that data is associated with
the same object names as at the time of save
. In this case, the matrix
appears in the object called d1
.
However, this behavior is not always desirable. If we already have an object
d1
in the environment, loading from file would over-write our data.
d1 = "Important" d1 load("test.Rda") d1
Thus, we can loose important information through loading of external data. This
is where load1
can be useful
d1 = "Important" d1 dfile = load1("test.Rda") d1 dfile
Thus, we keep all objects in our environment intact and assign the new data into a variable of our choice.
unlink("test.Rda")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.