[R] passing arguments to simple plotting program.

Ulrik Stervbo ulrik.stervbo at gmail.com
Tue May 9 18:40:00 CEST 2017


Hi Gerard,

Quotation marks are used for strings. In you function body you try to use
the strings "indata" and "fig_descrip" (the latter will work but is not
what you want).

In your current function call you pass the variable Figure as the value to
the argument fig_descrip, followed by a lot of other stuff your function
doesn't know what to do with.

Remove the quotation marks around indata and fig_descrip in the function
body, call your function with:

plot_f1(indata=v5, n1=114, n2=119, n3=116, fig_descrip="Figure 2a\nChange
in Composite Score at Visit 5 (Day 31)\nPer Protocol Population")

and you should be fine.

HTH

Ulrik
Gerard Smits <smits.gerard.j at gmail.com> schrieb am Di., 9. Mai 2017, 18:27:

> Hi Ulrik,
>
> If I can trouble you with one more question.
>
> Now trying to send a string to the main= .  I was able to pass the data
> name in data=in_data, but same logic is not working in passion the main
> string.
>
>
> plot_f1 <-function(indata,n1,n2,n3,fig_descrip) {
>   par(oma=c(2,2,2,2))
>   boxplot(formula = d_comp ~ rx_grp,
>           data="indata”,                    # <- worked fine here.
>           main="fig_descrip",
>           ylim=c(-10,5),
>           names=c(paste0("Placebo(N=", n1,  ")"),
>          paste0("Low Dose(N=", n2, ")"),
>  paste0("High Dose(N=", n3,")")),
>           ylab='Change from Baseline')
>   abline(h=c(0), col="lightgray")
> }
>
> plot_f1(indata=v5, n1=114, n2=119, n3=116, fig_descrip=Figure 2a\nChange
> in Composite Score at Visit 5 (Day 31)\nPer Protocol Population)
>
> Error Message: Error: unexpected numeric constant in "plot_f1(indata=v5,
> n1=114, n2=119, n3=116, fig_descrip=Figure 2”
>
> Even this call gives the same error:  plot_f1(indata=v5, n1=114, n2=119,
> n3=116, fig_descrip=Figure)
>
>
> Thanks,
>
> Gerard
>
>
>
>
>
>
> On May 8, 2017, at 11:40 PM, Ulrik Stervbo <ulrik.stervbo at gmail.com>
> wrote:
>
> HI Gerard,
>
> You get the literals because the variables are not implicitly expanded -
> 'Placebo(N=n1)  ' is just a string indicating the N = n1.
>
> What you want is to use paste() or paste0():
> c(paste0("Placebo(N=", n1, ")"), paste0("Low Dose (N=", n2, ")"),
> paste0("High Dose (N=", n3, ")"))
> should do it.
>
> I was taught a long ago that attach() should be avoided to avoid name
> conflicts. Also, it makes it difficult to figure out which data is actually
> being used.
>
> HTH
> Ulrik
>
> On Tue, 9 May 2017 at 06:44 Gerard Smits <smits.gerard.j at gmail.com> wrote:
>
>> Hi All,
>>
>> I thought I’d try to get a function working instead of block copying code
>> and editing. My backorund is more SAS, so using a SAS Macro would be easy,
>> but not so lucky with R functions.
>>
>>
>> R being used on Mac Sierra 10.12.4:
>>
>> R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
>> Copyright (C) 2016 The R Foundation for Statistical Computing
>> Platform: x86_64-apple-darwin13.4.0 (64-bit)
>>
>>
>> resp<-read.csv("//users//gerard//gs//r_work//xyz.csv", header = TRUE)
>>
>> v5  <-subset(resp, subset=visit==5 & pp==1)
>>
>> plot_f1 <-function(n1,n2,n3) {
>>   attach(v8)
>>   par(oma=c(2,2,2,2))
>>   boxplot(formula = d_comp ~ rx_grp,
>>           main="Figure 2\nChange in Composite Score at Visit 5 (Day
>> 31)\nPer Protocol Population",
>>           ylim=c(-10,5),
>>           names=c('Placebo(N=n1)  ',
>>                   'Low Dose(N=n2) ',
>>                   'High Dose(N=n3)'),
>>           ylab='Change from Baseline')
>>   abline(h=c(0), col="lightgray")
>> }
>>
>> plot_f1(n1=114, n2=119, n3=116)
>>
>> The above is a simplified example where I am trying to pass 3 arguments,
>> n1-n3, to be shown in the x-axis tables,  Instead of the numbers, I get the
>> literal n1, n2, n3.
>>
>> Any help appreciated.
>>
>> Thanks,
>>
>> Gerard
>>
>>
>>
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> <http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list