I have several functions as strings which contain a lot of numeric vectors in the form of
c(1,2,3)
, with three fixed values each (3D-coordinates). See test_string
below as a small example. I can create a working function test_fun
using eval
and parse
, but there is a problem:
I need these vectors to be recognized as one input, i.e. as double[3]
and not as language
with the parts 'c' (symbol), 1 (double[1]), 2 (double[1]) and 3 (double[1]). Check this code to see what I mean:
test_string <- "function(x) \n c(1,2,3)*x"
test_fun <- eval(parse(text = test_string))
test_fun(2)
#[1] 2 4 6 <- it's working
View(list(test_fun)) # see 'type' column
str(body(test_fun)[[2]])
# language c(1, 2, 3) <- desired output here: num [1:3] 1 2 3
str(body(test_fun)[[2]][[1]])
# symbol c
Is there an easy solution that works on the full string? I would be very happy to learn about this! If necessary I could also change the code in the function which creates these function strings when the substrings are concatenated with paste("function(x) \n ","c(1,2,3)","*x",sep = "")
.
Edit: I did a mistake in the 'View' and 'desired output' line. It is now correct.