1

I wrote a function as:

gener1 <- function(du){
  nth <- paste0(paste0("dum", 1:du, " * ", "X", 1:du), collapse = " + ")
  return(nth)
  }

it returns a sequence as:

"dum1 * X1 + dum2 * X2 + dum3 * X3" 

Now I want to use this sequence in a next function. Simply I can just copy that and paste:

S = quote(dum1 * X1 + dum2 * X2 + dum3 * X3)

Result:

dum1 * X1 + dum2 * X2 + dum3 * X3

It works. I wonder if it is possible to automatize that I do not need to use a "copy paste approach".

The final product I want to achieve is to use S in the following situation:

fo <- substitute(BH100 ~ S * ((1 - exp(-b2*TIME))/(1-exp(-b2*100)))^b3, list(S = S))

nls(fo, data = dane, start = list(b2 = 0.01, b3 = 1.1, X1 = 20, X2 = 20, X3 = 20))

Here is an example of the data structure I have

year    T   BH100   TIME    dum1    dum2    dum3    dum4
1987    25  12.6    25  1   0   0   0
1990    28  14.9    28  1   0   0   0
1994    32  18.8    32  1   0   0   0
1983    21  13.4    21  0   1   0   0
1986    24  16.1    24  0   1   0   0
1990    28  19.6    28  0   1   0   0
1998    36  26.7    36  0   1   0   0
2002    40  27.8    40  0   1   0   0
1994    32  17.2    32  0   0   1   0
1998    36  19.4    36  0   0   1   0
2002    40  23.5    40  0   0   1   0
2008    46  26.3    46  0   0   1   0
2013    51  28.7    51  0   0   1   0
1985    23  14.6    23  0   0   0   1
1989    27  18.5    27  0   0   0   1
1990    28  19.2    28  0   0   0   1
Mateusz1981
  • 1,817
  • 17
  • 33
  • 4
    Not much clear. You can use it in the "next function". Maybe you should provide a little more context and describe what you are trying to achieve. – nicola Jun 09 '15 at 09:45
  • Maybe `S <- noquote(gener1(3)); cat(S)` ? [Duplicate/Relevant post](http://stackoverflow.com/questions/13449233/r-how-to-send-a-text-string-containing-double-quotes-to-function) – zx8754 Jun 09 '15 at 09:50
  • @zx8754 Just `cat(gener1(3))` is enough – nicola Jun 09 '15 at 09:52
  • @nicola assigning to variable with `cat` wouldn't work. `S <- cat(gener1(3))` – zx8754 Jun 09 '15 at 09:54
  • If you want to print on screen, `cat` is enough. If you want to have a variable, `S<-gener1(3)` suffices, without using `noquote`. – nicola Jun 09 '15 at 09:58
  • @nicola 'noquote' as well as 'cat' do not work. After using _substitute_ I get the value in the _""_ in the _fo_ function which makes _nls_ not able to perform – Mateusz1981 Jun 09 '15 at 10:12
  • @Mateusz1981 Try `eval(parse(text=S))` where `S<-gener1(3)` for instance. – nicola Jun 09 '15 at 10:16
  • @nicola does not work either. – Mateusz1981 Jun 09 '15 at 10:24

1 Answers1

0

I'm not sure I understand your question fully, but I think you're looking for the eval function. Your gener1 function right now is generating a string: "dum1 * X1 + dum2 * X2 + dum3 * X3". You can evaluate this string using eval with parse:

expression = gener1(3)
> expression
[1] "dum1 * X1 + dum2 * X2 + dum3 * X3"
evaluated_expression = eval(parse(text=expression))
> evaluated_expression
[1] 68

So, for your example, you can generate an expression string using your gener1 function, and then use the evaluated output in your fo variable:

S = eval(parse(text = gener1(3)))
fo <- substitute(BH100 ~ S * ((1 - exp(-b2*TIME))/(1-exp(-b2*100)))^b3, list(S = S))
nls(fo, data = dane, start = list(b2 = 0.01, b3 = 1.1, X1 = 20, X2 = 20, X3 = 20))
Steve
  • 2,401
  • 3
  • 24
  • 28
  • I did `ex = gener1(19)` than I did `eval(parse(text = ex))` error pop up `Error in eval(expr, envir, enclos) : object 'dum1' not found` – Mateusz1981 Jun 10 '15 at 06:34
  • can you provide a minimal example of your code and data so that I know what variables are defined in your environment? from your question, it seemed like you already had these variables "dum1," "dum2," etc defined in your environment. – Steve Jun 10 '15 at 15:00
  • I added a bit of data I work with. Yes, the variables _dum1 ect_ are defined – Mateusz1981 Jun 12 '15 at 08:08
  • @Mateusz1981 OK -- in your `gener1` function you reference variables `dum1`, `dum2`, `dum3`, as well as `X1`, `X2`, and `X3`. If you're trying to refer to the column names in your data frame, you'll need to change the `dum1` etc. in your function to `your-data-frame$dum1` etc. I still don't see where `X1` etc are defined, though -- are those variables defined in your environment? – Steve Jun 22 '15 at 17:18
  • `X1` is not defined (it is not in the `data.frame` as variable).it is a coefficient to be calculated by the `nls` by multipliction of `dum1 * X1` – Mateusz1981 Jun 23 '15 at 07:27