-2

I'm trying to write complex R code that depends on my data. For example, suppose x = c(1,3,17), but critically I don't know how many elements x has ahead of time. All I know is that x is a vector of integers. I want to use x to create a code block like this:

a = fcn(complex_stuff,
   thing(abc = xyz, 1, zyx),
   thing(abc = xyz, 3, zyx),
   thing(abc = xyz, 17, zyx),
   more_complex_stuff
 )
user43953
  • 109
  • 11
  • Your question is unclear to me. Can you add some clarifications? For example, input and output of the function that you would like to have. – slava-kohut Nov 22 '19 at 01:48
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 22 '19 at 05:04
  • If knowing the length of a vector is useful towards carrying out the "complex stuff," then ```length(x)``` – ThetaFC Nov 22 '19 at 07:24
  • @slava-kohut To clarify I want to write code that creates the three middle "thing" liines from the vector x and then inserts them into the function. I did not specify the nature of "fcn" because it's a general issue (in my case the fcn is synth() if that helps). I considered a reprex but did not think it would that useful here, a sketched-out solution will suffice. I agree length(x) will be part of the solution. I think assign() and paste() in a loop may also be involved. – user43953 Nov 22 '19 at 16:47

1 Answers1

0

I agree my question here is ill-stated, but in case anyone winds up here, I solved this in two steps:

Step 1: make the text I want to insert into the function.

insert_me <- paste("thing(abc = xyz", x, "zyx)", sep=",", collapse=",")

Step 2: insert the text into the function. The key is to use eval(parse()) in a particular way, see the comments and answer to my question here: How to insert text into an R function?

user43953
  • 109
  • 11