I am trying to write a function for non R users for report writing in R markdown. The function calls unicode for macron characters.
CODE:
library(stringr)
library(Unicode)
library(htmltools)
library(cat)
mac <- function(x){
if (x == "a") {
result <- "\u101"
} else if (x == "A") {
result <- "\u100"
} else if (x == "e") {
result <- "\u113"
} else if (x == "E") {
result <- "\u112"
} else if (x == "i") {
result <- "\u12b"
} else if (x == "I") {
result <- "\u12a"
} else if (x == "o") {
result <- "\u14d"
} else if (x == "O") {
result <- "\u14c"
} else if (x == "u") {
result <- "\u16b"
} else if (x == "U") {
result <- "\u16a"
} else (print("Entry not recognised"))
result = paste0(result, sep = "")
return(result)
# return(p(paste0(result, sep = "")))
}
I have tried:
# gsub("[\r\n]", "", result)
# str_replace_all(x, "[\r\n]" , "")
Without any success - I realise this is because there are no spaces around the output of the function to remove.
As an example, I want this:
p('Something',mac("a"),'nd something with a macron')
To read:
Something ānd something with a macron.
You're getting multiple lines because you're passing a list to p()
.
If you wrap the text in paste0
the output should all be on one line.
Input:
p(paste0('Something ',mac("a"),'nd something with a macron'))
Output:
<p>Something ānd something with a macron</p>
Which displays as:
Something ānd something with a macron
This can be wrapped in a single function:
p <- function(...) htmltools::p(paste0(...))
If you anticipate users trying to pass lists to p() then you could add something to handle those exceptions.
Full code with example use:
library(stringr)
library(Unicode)
library(htmltools)
library(cat)
mac <- function(x){
if (x == "a") {
result <- "\u101"
} else if (x == "A") {
result <- "\u100"
} else if (x == "e") {
result <- "\u113"
} else if (x == "E") {
result <- "\u112"
} else if (x == "i") {
result <- "\u12b"
} else if (x == "I") {
result <- "\u12a"
} else if (x == "o") {
result <- "\u14d"
} else if (x == "O") {
result <- "\u14c"
} else if (x == "u") {
result <- "\u16b"
} else if (x == "U") {
result <- "\u16a"
} else (print("Entry not recognised"))
result = paste0(result, sep = "")
return(result)
# return(p(paste0(result, sep = "")))
}
# wrap input in paste0() to create a string then pass to p()
p <- function(...) htmltools::p(paste0(...))
# example use
p('Something ',mac("a"),'nd something with a macron')