What is the filename() function in R?

Overview

The filename() function offers an S3 classA class which has no formal definition. to show the file names that are interconvertible with character.

Syntax


filename(x,
path= NULL,
tag= NULL,
ext= NULL,
date= NULL,
time= NULL,
subdir= TRUE)

Parameters

  • x: This shows the file name.
  • path: This is the path of the file. Its default value is NULL.
  • tags: These are the tags regarding the file name. Its default value is NULL.
  • ext: This is the file extension. Its default value is NULL.
  • date: This is the date stamp. Its default value is NULL.
  • time: This is the time stamp. Its default value is NULL.
  • subdir: This is used to append the date or time subdirectory to the path. Its default value is TRUE.

Return value

It constructs and returns the as.filename object.

Example

library(filenamer)
# by default the date stamped subdirectory is appended in filename function.
fln1 <- filename("data", tag="qc", ext="txt")
print(as.character(fln1))
# disabling date stamped subdirectory inclusion
fln2 <- filename("data", tag="qc", date=NA, time=NA, subdir=FALSE)
print(as.character(fln2))
# creating a new filename using an existing filename.
fln3 <- filename(fln1)
print(as.character(fln3))

Explanation

  • Line 3: It constructs the filename object and the date stamped subdirectory is appended by default in the file name.
  • Line 7: Here, we disable the date stamped subdirectory inclusion.
  • Lines 11–12: We create a new filename object using the existing filename and print it as a character sequence on the console.

Note: In lines 4, 8, and 12, the as.character() method converts a numeric object into a character data type or string data type.

Free Resources