Alexander Alexander - 1 month ago 4
R Question

How to use sprintf to show string "\t"?

I have an algorithm to identify the separator used in in a .txt file. I want to output the separator found using

cat()
. I am using
%s
with
sprintf()
to no avail.

current.sep = "\t"

cat(sprintf("Found separator : %s. \n", current.sep))
## Found separator : .

cat(sprintf("Found separator : %s. \n", "current.sep"))
## Found separator : current.sep.

## I want:
## Found separator : \t.

Answer
print_separator <- function(separator) {

    expr <- deparse(paste0("Found separator : ", separator, "."))
    cat(substr(expr, 2, nchar(expr) - 1))

}

print_separator(current.sep)
## Found separator : \t.