Glue Pack and Escaping Special Characters

glue

#1

Hi all -

Does anyone know if the glue package has a way to preserve special characters without escaping them?

For example:
If I run...
var <- "solve\issue"
glue::glue("help me {var}")
It results in...
help me solve\issue

I want the result to be
help me solve\issue

If I run...
sprintf("help me %s", var)
It results in...
help me solve\issue

Another workaround would be to include increase the number of escapes (e.g., from "\" to "\\").

Thanks!


#2
var <- "solve\issue"

should result in an error, so I don't know how you've got the results.

You need to escape "\\" to print "\".


#3

sorry var <- "solve\issue"


#4

A prose description along with some code snippets isn't sufficient for us to understand what you are trying to do, you should always post code in the form of a reprex that:

  1. Builds the input data you are using.
  2. The function you are trying to write, even if it doesn't work.
  3. Usage of the function you are trying to write, even if it doesn't work.
  4. Builds the output data you want the function to produce.

You can learn more about reprex's here:

Right now the is an issue with the version of reprex that is in CRAN so you should download it directly from github.

Until CRAN catches up with the latest version install reprex with

devtools::install_github("tidyverse/reprex")

The reason we ask for a reprex is that it is the easiest and quickest way for someone to understand the issue you are running into and answer it.

Nearly everyone here who is answering questions is doing it on their own time and really appreciate anything you can do to minimize that time.

In any case here are some reprex's that may point out the issue you are having and how to fix it.

# single backslash in front of a the letter "i"
# is not a legal character sequence in R
var <- "solve\issue"
#> Error: '\i' is an unrecognized escape in character string starting ""solve\i"

Created on 2018-03-14 by the reprex package (v0.2.0).

Escaping the backslash makes the character sequence legal.

suppressPackageStartupMessages(library(glue))
# you need to escape the backslash with a backslash
var <- "solve\\issue"
glue("Help me {var}")
#> Help me solve\issue

Created on 2018-03-14 by the reprex package (v0.2.0).

The problem has nothing to do with glue or sprintf, neither can be use with a string like solve\issue because that is not a legal character sequence in R. Your sprintf code should not be working if it is preceded by var = "solve\issue".

var <- "solve\issue"
sprintf("help me %s", var)
#> Error: '\i' is an unrecognized escape in character string starting ""solve\i"

Created on 2018-03-14 by the reprex package (v0.2.0).