3

I need to concatenate two strings within an R object: one is just regular text; the other is italicized. So, I tried a lot of combinations, e.g.

paste0(" This is Regular", italic( This is Italics))

The desired result should be:

This is Regular This is Italics

Any ideia on how to do it?

Thanks!

JPMD
  • 644
  • 1
  • 7
  • 19
  • 3
    There's no `italic` function in base R. `paste0` and other R functions return `character`s. They have no concept of format, such as Italics or bold. Format is a characteristic of output destination. So the way to get Italic text would depend on your output destination. It can't be done in a text file or a .Rda file. It can be done (in different ways) for a PDF, Markdown or HTML file, to give just a few examples. – Limey Jun 21 '20 at 09:34
  • 1
    italic's and other mathimatical annotations are used in plots. E.g. `plot(iris$Species, main = expression(paste("This is Regular ", italic(" This is Italics"))))` will return a simple plot with the title in regular and italics. Search on SO for examples and read the help (`?italic`) with the italic function. – phiver Jun 21 '20 at 09:43
  • Thanks @Limey... So for HTML how can I format text from a paste0 expression? – JPMD Jun 21 '20 at 10:03
  • Thanks @phiver... I know how to do it for plots... but I need a italic in a regular HTML page – JPMD Jun 21 '20 at 10:04

2 Answers2

1

In plot labels, you can use expressions, see mathematical annotation :

 plot(1,xlab=expression("This is regular"~italic("this is italic")))

enter image description here

Waldi
  • 39,242
  • 6
  • 30
  • 78
1

To provide an string for which an HTML parser will recognise the need to render the text in Italics, wrap the text in <i> and </i>. For example: "This is plain text, but <i>this is in Italics</i>.".

However, most HTML processors will assume that you want your text to appear as-is and will escape their input by default. This means that the special meanings of certain characters - including < and > will be "turned off". You need to tell the processor not to do this. How you do that will depend on context. I can't tell you that because you haven't given me context.

Are you for example, writing to a raw HTML file? (You need do nothing.) Are you writing to a Markdown file? If so, how? In plain text or in a rendered chunk? Are you writing a caption to a graphic? (Waldi has suggested a solution.) Etc, etc....

Limey
  • 10,234
  • 2
  • 12
  • 32
  • Thanks!...I using paste0 inside of a chunk... somethink like: ```concl <- paste0("Não existem diferenças significativas entre as médias das variávies ", var1, " e ", var2)``` var1 and var2 are variable names that I would like to get in italics. – JPMD Jun 21 '20 at 10:17
  • And then where and when do you attempt to render your `concl` variable? Please, please provide a [simple, self-contained example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Put simply, give me some code (as little as possible) that enables me to see `concl` being rendered in a way other than you want. – Limey Jun 21 '20 at 10:57
  • ok, My Rmd is: ```{r variable selection, echo = FALSE, results = "hide"} concl <- paste0("Não existem diferenças significativas entre as médias das variáveis ", var1, " e ", var2) ``` 6. A conclusão do teste é: `r concl` This needs to be output to an html file via knit. – JPMD Jun 21 '20 at 12:01