0

Say I have an object that is a multi-line string:

object <- "value &
value"

I would like for the value of the object to be printed in R without “\n” and without quotes. So instead of:

object
#> [1] "value &\nvalue"

I would get:

object
#> value &
#> value

This is similar to the behavior of cat() but I want the object to print this way by default. I don’t want to have to call cat() on it. I guess I need to set a special class?

Thank you so much for your help!

Created on 2022-05-25 by the reprex package (v2.0.1)

Kene David Nwosu
  • 828
  • 6
  • 12
  • 1
    You could create your own special S3 class and then then create a custom print() method for that class. But otherwise you can't change the default print behavior of a simple character value. So you want to create a custom class? Here's one such example: https://stackoverflow.com/questions/10938427/example-needed-change-the-default-print-method-of-an-object – MrFlick May 25 '22 at 13:27
  • 1
    You might be interested in `noquote()`... though sadly it prints `\n` as-is. Otherwise, yes: `class(object) <- "printcat"; print.printcat <- function (x) cat(x); object`. – dash2 May 25 '22 at 15:17
  • Thank you! Do you want to put this as an answer? – Kene David Nwosu May 26 '22 at 09:10

0 Answers0