I want to add markup to (Urdu language) text that is written right to left. I am trying to use gsub for the purpose but everything I have tried so far does not produce the desired output
text <- "یہ جملہ ایک مثال کے لیے استعمال کیا جا رہا ہے"
pattern <- "کیا جا"
replaceWith <- paste0("<somemark>", pattern, "</somemark>")
gsub(pattern, replaceWith, text)
gsub returns the following
یہ جملہ ایک مثال کے لیے استعمال <somemark>کیا جا</somemark> رہا ہے
desired output .
How can I acheive the desired output?
Note: I could not even properly typeset the desired output in my post, I had to rely on an image instead.
Update: Although mysub
function below correctly concatenates the strings(in console), I continue to face the problem of incorrect order of text in shiny app.
mysub <- function(text, pattern){
beforePattern <- substr(text, 1, regexpr(pattern, text)[1]-1)
afterPattern <- substr(text, regexpr(pattern,text)[1] + nchar(pattern), nchar(text))
result <- paste(afterPattern, replaceWith, beforePattern)
result
}
I gave it a try . I did take the liberty of hard coding the args instead of reading from session, though.
Server:
output$mysub <- function(){ # (text=NULL, pattern=NULL)
text <- "یہ جملہ ایک مثال کے لیے استعمال کیا جا رہا ہے"
pattern <- "کیا جا"
Encoding(text) <- "UTF-8"
Encoding(pattern) <- "UTF-8"
print(text)
beforePattern <- substr(text, 1, regexpr(pattern, text)[1]-1)
afterPattern <- substr(text, regexpr(pattern,text)[1] + nchar(pattern), nchar(text))
replaceWith <- paste0("<somemark>", pattern, "</somemark>")
result <- paste(afterPattern, replaceWith, beforePattern)
# result <- paste( beforePattern, replaceWith, afterPattern)
# Encoding(result) <- "UTF-8"
print(length(result))
print(result)
return(result)
}
# ui.R:
h2( textOutput("mysub") )