2

Consider the string "Hello \n World!". It appears that the relevant methods for formatting and printing it are print.default, cat, and if need be, format then print or cat. However, each of these seem to be able to do some things that the others cannot. Is there any one ultimate printing function that gives the maximum amount of control over the formatting/printing of my strings?

For example, I can't see anywhere in print, format, or print.default's documentations that would make them respect my \n and put a line break in "Hello \n World!", as cat does, but I also can't see anyway to make cat keep the quotation marks in "Hello \n World!", as print("Hello \n World!", quote=FALSE) would.

J. Mini
  • 1,868
  • 1
  • 9
  • 38

3 Answers3

1

If we need the quotes as well, wrap it with dQuote within cat

cat( dQuote("Hello \n World!", FALSE))
"Hello 
 World!"

According to ?cat

Character strings are output ‘as is’ (unlike print.default which escapes non-printable characters and backslash — use encodeString if you want to output encoded strings using cat). Other types of R object should be converted (e.g., by as.character or format) before being passed to cat. That includes factors, which are output as integer vectors.


Or we can use message. The advantage is that it can be used as well with RMD files where those messages will be printed on the console rather than on the document

message('"Hello \n World!"')
#"Hello 
# World!"

i.e. as a trial, create a simple RMD file

---
output:
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
library(ggplot2)
```


```{r trial 1, echo = FALSE, message = FALSE}
message("data(mtcars) is created with message")
print("data(mtcars) is created with print")
glue::glue("data(mtcars is created with glue")

```

-output

enter image description here

NOTE: We already specified message = FALSE. So, it would not show up in the document, whereas for debugging, it still prints on the Rmarkdown console output

...
data(mtcars) is created with message
output file: test1.knit.md
...
akrun
  • 874,273
  • 37
  • 540
  • 662
  • That's interesting. My version of R isn't very out of date, but your `?cat` doesn't match mine. It must've got an update. Anyway, am I to take it that the general answer is "the most powerful printing function is `cat`. Do whatever you need to do to modify the string before passing it to `cat`"? – J. Mini Dec 23 '20 at 18:47
  • @J.Mini What is your R version. I used `R version 4.0.3`. All the answers are doing some kind of string interpolation with double quotes i.e. `paste` or `glue` etc. Here, the `dQuote` is also doing the same thing – akrun Dec 23 '20 at 18:49
  • @J.Mini I used `dQuote` because it automatically creates those double quotes without having to create another function wrapper – akrun Dec 23 '20 at 18:50
  • 1
    3.6.3. I'm updating it now. The fact that everyone is using `cat` rather than `print` is mostly why I've made that comment - I can see a pattern forming. – J. Mini Dec 23 '20 at 18:52
  • @J.Mini `print` doesn't evaluate the `\n` – akrun Dec 23 '20 at 18:53
1

This is possible using glue from Tidyverse. glue will respect the \n. And you can print the double quotes by wrapping the string in a single quotes ' or escaping them with \".

library(glue)

# wrap in single quote
glue('"Hello \n World!"')

# escape the double quotes
glue("\"Hello \n World!\"")
David
  • 2,200
  • 1
  • 12
  • 22
1

Just modify the cat

foo = function(...) {
    s = paste0("\"", ..., "\"")
    cat(s)
}
foo("Hello \n World!")
#"Hello 
# World!"
d.b
  • 32,245
  • 6
  • 36
  • 77
  • _Just modify the cat_ - Oh, internet! – J. Mini Dec 23 '20 at 18:43
  • So am I to take it that the general answer is "the most powerful printing function is `cat`, do whatever you need to do to modify the string beforehand"? – J. Mini Dec 23 '20 at 18:46
  • @J.Mini, I think they're all slightly different in terms of what input they accept, what value they return, and such. One may be better suited than the other depending on the need. It seems like you want something similar to `cat`. – d.b Dec 23 '20 at 23:40