str_view

The function stringr::str_view() allows you to view/print the “raw contents” of a string.

Tidyverse reference page

View a character scalar

Normal R method to view a string.

"tga"
[1] "tga"

Tidyverse method to view string with stringr::str_view().

stringr::str_view("tga")
[1] │ tga

The differences of str_view() to the normal method are:

  • The inclusion of | between the index ([1]) and the string (tga).
  • The quotes around the string are not included.

View a character vector

Create a character vector.

vec <- c("rose", "thistle", "leek", "shamrock")

Print vector through normal R method.

vec
[1] "rose"     "thistle"  "leek"     "shamrock"

View vector with str_view().

vec |> stringr::str_view()
[1] │ rose
[2] │ thistle
[3] │ leek
[4] │ shamrock

Notice that each vector element is printed on a new line with str_view().

Special characters

There are may special characters that can be used in strings. str_view() is especially useful in how it presents these.

White spaces

The three main white spaces in R are:

  • " ": A space.
  • "\t": A tab.
  • "\n": A new line.

Create a vector demonstrating these three.

#Create a vector of strings
vec <- c("gataac", "gat aac", "gat\taac", "gat\naac")

Print this vector with the normal R method.

vec
[1] "gataac"   "gat aac"  "gat\taac" "gat\naac"

View with str_view().

stringr::str_view(vec)
[1] │ gataac
[2] │ gat aac
[3] │ gat{\t}aac
[4] │ gat
    │ aac

In this case notice:

  • The tab character (\t) is within curly braces ({}), represented as {\t}.
  • The space is shown as a space and the newline creates a new line, causing the fourth element to be split over 2 lines.

Special characters

To include double quotes within strings denoted by double quotes we need to “escape” the double quotes. We can carry this out with \.

Create a string containing a double quote. Note, we can include single quotes (') normally as we are not using them to denote the string.

sentence <- "A double quote is: \"."

Printing the string normally with R shows the escape character (\)

sentence
[1] "A double quote is: \"."

Viewing the string with str_view() does not show the escape character. Instead it aims to display the text how a human would read it, rather than a computer. This is very useful to ensure any escape characters in the string work as intended.

stringr::str_view(sentence)
[1] │ A double quote is: ".

To add to the complexity you can escape escape characters with an escape character.

#Create vector
vec <- c("A double quote is: \"", "A single quote is: '", "An escape character is: \\")
#View vector
vec |> stringr::str_view()
[1] │ A double quote is: "
[2] │ A single quote is: '
[3] │ An escape character is: \

Unicode

Unicode can be used to include other various special symbols. Examples include:

  • π: Th pi symbol represented by \u00b5
  • €: The euro symbol represented by \u20ac
  • 😎: Smiling face with sunglasses represented by \U0001f60e
#Create vector
vec <- c("\u00b5", "\u20ac", "\U0001f60e")

Normal R will print out symbol.

vec
[1] "µ"  "€"  "😎"

str_view() will also print out the symbol.

vec |> stringr::str_view()
[1] │ µ
[2] │ €
[3] │ 😎

For more unicode characters please see: Wikipedia page of List of Unicode characters. For these you need to change U+ to u and you can use lower case letters instead of upper case.

Missing values

Missing values are represented as NA in R.

c("a", NA, "c") |>
    stringr::str_view()
[1] │ a
[2] │ NA
[3] │ c

When you run the above code you will notice that the NA is coloured with a different colour than the string "NA".