texts <- c(
"C6H12Cl6", # simple formula
"H3Na", # simple formula
"Text with blank", # simple text
"A1B2", # No valid elements but looks like formula
"Fe(NO3)3", # group with index
"SO4^2-", # charged molecule
"Na+", "Cl-", # simple ions
paste0("H", intToUtf8(0x2082), "O"), # unicode-subscript
"Ca(OH)2-" # group with index and charge
)
# Check that all examples can be converted to expression
all(sapply(texts , function(x) { !inherits(try(fml2expr(x), silent=TRUE), "try-error") }))
exprs <- fml2expr(texts)
str(exprs)
# Plot with legend
plot(1:10, 1:10, pch = 16, col = 1:10, xlim = c(0, 11), ylim = c(0, 11))
legend("topright", legend = exprs, col = 1:10, pch = 16, title = "Formeln")
# Careful! text() does not accept an expression vector
text(3:4,3:4,labels=exprs[3:4])
for (i in 5:6) text(i,i,labels=exprs[[i]])
# you can also return a named character vector (names are input, values are modified)
fml2expr(texts, expr = FALSE)
Run the code above in your browser using DataLab