Re: [R] ggplot inside of the function

2019-11-25 Thread Eric Berger
Hi Mayooran,
If you define the following function

f <- function(m,v) { sprintf("Norm(mu=%.1f, var=%.1f)",m,v) }

Then you can modify the setting of Prob as follows

 Prob <- plyr::rename(Prob_df,
c("p_1"=f(mu1,var1),"p_2"=f(mu2,var2),"p_3"=f(mu3,var3)))

The lesson here is that wherever you set a variable to a string, as in
"p_1"="Norm(mu=mu1,var=var1)",
you can create the string dynamically using the function sprintf(),
which returns a string.

Best,
Eric

On Mon, Nov 25, 2019 at 3:54 AM Thevaraja, Mayooran
 wrote:
>
> Hello Folks
>I am working some R package development. When I was using 
> ggplot inside of the function, I need to get the output graph's legend must 
> be corresponding to the input parameters numerical value (need to be 
> automatically changed when we input different parameters).  Therefore anyone 
> has any ideas? Here, I have given below a simple example. I need to get my 
> output graph's legend should be Norm(mu=0.2, var=0.8),...
>
>
> example <- function(mu1,mu2,mu3,var1,var2,var3){
>   x <- seq(-3,3, by=0.01)
>   p_1 <- dnorm(x,mu1,var1)
>   p_2 <- dnorm(x,mu2,var2)
>   p_3 <- dnorm(x,mu3,var3)
>   Prob_df <- data.frame(x,p_1,p_2,p_3)
>   Prob <- plyr::rename(Prob_df, c("p_1"="Norm(mu=mu1, 
> var=var1)","p_2"="Norm(mu=mu2, var=var2)","p_3"="Norm(mu=mu3, var=var3)"))
>   melten.Prob <- reshape2::melt(Prob, id = "x", variable.name = "Methods", 
> value.name = "Prob")
> ggplot2::ggplot(melten.Prob)+
> ggplot2::geom_point(ggplot2::aes(x = x, y = Prob, group= Methods, colour 
> = Methods ))+
> ggplot2::geom_line(ggplot2::aes(x = x, y = Prob, group= Methods, colour = 
> Methods ))
>
> }
>
> mu1 <- 0.2
> mu2 <- 0.5
> mu3 <- 1
> var1 <- 0.8
> var2 <- 1.2
> var3 <- 2.1
> example(mu1,mu2,mu3,var1,var2,var3)
>
>
> Thanks
> Mayooran
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


[R] [r] How to obtain the correlation coefficients between two variables in the R object

2019-11-25 Thread Subhamitra Patra
Dear R-users,

I am estimating the DCC-model by using the following code and successfully
making the individual plots indicating the dynamic correlation between the
two variables stored in the R data frame (namely dcc.fit in my code). My
code is

Dat = Date2[,2:13, drop = FALSE]
library(rmgarch)
library(rugarch)
xspec = ugarchspec(mean.model = list(armaOrder = c(1, 1)), variance.model =
list(garchOrder = c(1,1), model = 'eGARCH'), distribution.model = 'norm')
uspec = multispec(replicate(12, xspec)) # Define a function using xspec
dcc.garch11spec = dccspec(uspec = uspec, dccOrder = c(1, 1), distribution =
'mvnorm')
dcc.fit = dccfit(dcc.garch11spec, data = Dat)
r1=rcor(dcc.fit, type="R")
print(r1)

Here, I need to obtain the DCC relationship between the two variables. I
successfully make a separate plot for the correlation between the two
variables with the following code.
plot(rcor(dcc.fit, type="R")['A1','A2',], type='l')
plot(rcor(dcc.fit, type="R")['B1','B2',], type='l')
plot(rcor(dcc.fit, type="R")['C1','C2',], type='l')
plot(rcor(dcc.fit, type="R")['D1','D2',], type='l')
plot(rcor(dcc.fit, type="R")['E1','E2',], type='l')
plot(rcor(dcc.fit, type="R")['F1','F2',], type='l')

Here, I request your help on two things which I am unable to do.

1. I need to arrange all the above individual plots (counted as 6) on one
page.
2. I need to obtain the correlation coefficients between the particular two
variables (i.e. A1 versus A2, B1 versus B2, ...so on) throughout the
entire period.

For your convenience, I have attached my sample data.

I definitely expect a positive help from the experts as the suggestions
from the expert through this R forum has always solved my previous problem.

Thank you.

-- 
*Best Regards,*
*Subhamitra Patra*
*Phd. Research Scholar*
*Department of Humanities and Social Sciences*
*Indian Institute of Technology, Kharagpur*
*INDIA*

[image: Mailtrack]

Sender
notified by
Mailtrack

11/25/19,
04:00:39 PM
__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] [r] How to obtain the correlation coefficients between two variables in the R object

2019-11-25 Thread Michael Dewey

To get multiple plots on the same physical page try
?layout
ot look through the various option under
?par

I do not know enough about DCC models to answer how you get correlations.

Michael

On 25/11/2019 10:34, Subhamitra Patra wrote:

Dear R-users,

I am estimating the DCC-model by using the following code and successfully
making the individual plots indicating the dynamic correlation between the
two variables stored in the R data frame (namely dcc.fit in my code). My
code is

Dat = Date2[,2:13, drop = FALSE]
library(rmgarch)
library(rugarch)
xspec = ugarchspec(mean.model = list(armaOrder = c(1, 1)), variance.model =
list(garchOrder = c(1,1), model = 'eGARCH'), distribution.model = 'norm')
uspec = multispec(replicate(12, xspec)) # Define a function using xspec
dcc.garch11spec = dccspec(uspec = uspec, dccOrder = c(1, 1), distribution =
'mvnorm')
dcc.fit = dccfit(dcc.garch11spec, data = Dat)
r1=rcor(dcc.fit, type="R")
print(r1)

Here, I need to obtain the DCC relationship between the two variables. I
successfully make a separate plot for the correlation between the two
variables with the following code.
plot(rcor(dcc.fit, type="R")['A1','A2',], type='l')
plot(rcor(dcc.fit, type="R")['B1','B2',], type='l')
plot(rcor(dcc.fit, type="R")['C1','C2',], type='l')
plot(rcor(dcc.fit, type="R")['D1','D2',], type='l')
plot(rcor(dcc.fit, type="R")['E1','E2',], type='l')
plot(rcor(dcc.fit, type="R")['F1','F2',], type='l')

Here, I request your help on two things which I am unable to do.

1. I need to arrange all the above individual plots (counted as 6) on one
page.
2. I need to obtain the correlation coefficients between the particular two
variables (i.e. A1 versus A2, B1 versus B2, ...so on) throughout the
entire period.

For your convenience, I have attached my sample data.

I definitely expect a positive help from the experts as the suggestions
from the expert through this R forum has always solved my previous problem.

Thank you.



--
Michael
http://www.dewey.myzen.co.uk/home.html

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] [r] How to obtain the correlation coefficients between two variables in the R object

2019-11-25 Thread Subhamitra Patra
Thank you for your suggestion. Actually, the correlation is coming in the
DCC model through my above code which I am able to plot it. Is there any
solution that I can extract data from a particular plot. Actually, I am
able to obtain the correlation matrix, but I need the correlation
coefficient series indicating the correlation coefficients between the two
variables.



[image: Mailtrack]

Sender
notified by
Mailtrack

11/25/19,
07:12:49 PM

On Mon, Nov 25, 2019 at 7:10 PM Michael Dewey 
wrote:

> To get multiple plots on the same physical page try
> ?layout
> ot look through the various option under
> ?par
>
> I do not know enough about DCC models to answer how you get correlations.
>
> Michael
>
> On 25/11/2019 10:34, Subhamitra Patra wrote:
> > Dear R-users,
> >
> > I am estimating the DCC-model by using the following code and
> successfully
> > making the individual plots indicating the dynamic correlation between
> the
> > two variables stored in the R data frame (namely dcc.fit in my code). My
> > code is
> >
> > Dat = Date2[,2:13, drop = FALSE]
> > library(rmgarch)
> > library(rugarch)
> > xspec = ugarchspec(mean.model = list(armaOrder = c(1, 1)),
> variance.model =
> > list(garchOrder = c(1,1), model = 'eGARCH'), distribution.model = 'norm')
> > uspec = multispec(replicate(12, xspec)) # Define a function using xspec
> > dcc.garch11spec = dccspec(uspec = uspec, dccOrder = c(1, 1),
> distribution =
> > 'mvnorm')
> > dcc.fit = dccfit(dcc.garch11spec, data = Dat)
> > r1=rcor(dcc.fit, type="R")
> > print(r1)
> >
> > Here, I need to obtain the DCC relationship between the two variables. I
> > successfully make a separate plot for the correlation between the two
> > variables with the following code.
> > plot(rcor(dcc.fit, type="R")['A1','A2',], type='l')
> > plot(rcor(dcc.fit, type="R")['B1','B2',], type='l')
> > plot(rcor(dcc.fit, type="R")['C1','C2',], type='l')
> > plot(rcor(dcc.fit, type="R")['D1','D2',], type='l')
> > plot(rcor(dcc.fit, type="R")['E1','E2',], type='l')
> > plot(rcor(dcc.fit, type="R")['F1','F2',], type='l')
> >
> > Here, I request your help on two things which I am unable to do.
> >
> > 1. I need to arrange all the above individual plots (counted as 6) on one
> > page.
> > 2. I need to obtain the correlation coefficients between the particular
> two
> > variables (i.e. A1 versus A2, B1 versus B2, ...so on) throughout the
> > entire period.
> >
> > For your convenience, I have attached my sample data.
> >
> > I definitely expect a positive help from the experts as the suggestions
> > from the expert through this R forum has always solved my previous
> problem.
> >
> > Thank you.
> >
>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>


-- 
*Best Regards,*
*Subhamitra Patra*
*Phd. Research Scholar*
*Department of Humanities and Social Sciences*
*Indian Institute of Technology, Kharagpur*
*INDIA*

[[alternative HTML version deleted]]

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


[R] Why don't the comments appear in the function?

2019-11-25 Thread bickis
I have made a list in which each element is a function.   If I print
individual elements of the list, then the function code is shown along
with embedded comments.  However, if I print the list or sublist, then the
function code is shown without comments.   Why (and how) are the comments
hidden?

>flist[[3]]
function(y,brackets,rates){
# Calculates before-tax income required to realized value y

ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
x<-(y+ints)/(1-rates)
x[sum(x>brackets)]
}


>flist[3]
$btv
function (y, brackets, rates)
{
ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
1)])) - brackets * rates
x <- (y + ints)/(1 - rates)
x[sum(x > brackets)]
}

I am running R 3.3.2 on Mac OS X  10.10.5

Mik Bickis

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


[R] NRC lexicon usage sentiment analysis

2019-11-25 Thread Mehdi Dadkhah
Dear Colleagues,
I hope you are doing well!
I did sentiment analysis on two datasets. As these datasets do not have
same size, to compare emotions in two datasets, i calculated percentages.
In my work, i calculated total occurrence for each emotion for two datasets
separately.  To calculate percentage of each emotion. i used below equation:

Percentage of an emotion=(total occurrence of an emotion in a dataset/Total
occurrence of all emotions in a dataset)*100

when i calculated percentages for each emotion, in each dataset to
calculate difference in percentages of an emotion, i used below equation:

difference in percentages of an emotion= percentages of an emotion in
dataset A  - percentages of an emotion in dataset B
Is my practice  true?
Could you please answer my question?
Thank you very much!
With best regards,


-- 
*Mehdi Dadkhah*

[[alternative HTML version deleted]]

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] [r] How to obtain the correlation coefficients between two variables in the R object

2019-11-25 Thread PIKAL Petr
Hi

Without knowing much about functions you use, instead of plotting you could
save results of rcor(dcc.fit, type="R") to

something <- rcor(dcc.fit, type="R")

and use

str(something) to inspect the structure.

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Subhamitra
> Patra
> Sent: Monday, November 25, 2019 2:46 PM
> To: Michael Dewey ; r-help mailing list  h...@r-project.org>
> Subject: Re: [R] [r] How to obtain the correlation coefficients between
two
> variables in the R object
> 
> Thank you for your suggestion. Actually, the correlation is coming in the
> DCC model through my above code which I am able to plot it. Is there any
> solution that I can extract data from a particular plot. Actually, I am
> able to obtain the correlation matrix, but I need the correlation
> coefficient series indicating the correlation coefficients between the two
> variables.
> 
> 
> 
> [image: Mailtrack]
>  mpaign=signaturevirality5&>
> Sender
> notified by
> Mailtrack
>  mpaign=signaturevirality5&>
> 11/25/19,
> 07:12:49 PM
> 
> On Mon, Nov 25, 2019 at 7:10 PM Michael Dewey
> 
> wrote:
> 
> > To get multiple plots on the same physical page try
> > ?layout
> > ot look through the various option under
> > ?par
> >
> > I do not know enough about DCC models to answer how you get
> correlations.
> >
> > Michael
> >
> > On 25/11/2019 10:34, Subhamitra Patra wrote:
> > > Dear R-users,
> > >
> > > I am estimating the DCC-model by using the following code and
> > successfully
> > > making the individual plots indicating the dynamic correlation between
> > the
> > > two variables stored in the R data frame (namely dcc.fit in my code).
My
> > > code is
> > >
> > > Dat = Date2[,2:13, drop = FALSE]
> > > library(rmgarch)
> > > library(rugarch)
> > > xspec = ugarchspec(mean.model = list(armaOrder = c(1, 1)),
> > variance.model =
> > > list(garchOrder = c(1,1), model = 'eGARCH'), distribution.model =
'norm')
> > > uspec = multispec(replicate(12, xspec)) # Define a function using
xspec
> > > dcc.garch11spec = dccspec(uspec = uspec, dccOrder = c(1, 1),
> > distribution =
> > > 'mvnorm')
> > > dcc.fit = dccfit(dcc.garch11spec, data = Dat)
> > > r1=rcor(dcc.fit, type="R")
> > > print(r1)
> > >
> > > Here, I need to obtain the DCC relationship between the two variables.
I
> > > successfully make a separate plot for the correlation between the two
> > > variables with the following code.
> > > plot(rcor(dcc.fit, type="R")['A1','A2',], type='l')
> > > plot(rcor(dcc.fit, type="R")['B1','B2',], type='l')
> > > plot(rcor(dcc.fit, type="R")['C1','C2',], type='l')
> > > plot(rcor(dcc.fit, type="R")['D1','D2',], type='l')
> > > plot(rcor(dcc.fit, type="R")['E1','E2',], type='l')
> > > plot(rcor(dcc.fit, type="R")['F1','F2',], type='l')
> > >
> > > Here, I request your help on two things which I am unable to do.
> > >
> > > 1. I need to arrange all the above individual plots (counted as 6) on
one
> > > page.
> > > 2. I need to obtain the correlation coefficients between the
particular
> > two
> > > variables (i.e. A1 versus A2, B1 versus B2, ...so on) throughout
the
> > > entire period.
> > >
> > > For your convenience, I have attached my sample data.
> > >
> > > I definitely expect a positive help from the experts as the
suggestions
> > > from the expert through this R forum has always solved my previous
> > problem.
> > >
> > > Thank you.
> > >
> >
> > --
> > Michael
> > http://www.dewey.myzen.co.uk/home.html
> >
> 
> 
> --
> *Best Regards,*
> *Subhamitra Patra*
> *Phd. Research Scholar*
> *Department of Humanities and Social Sciences*
> *Indian Institute of Technology, Kharagpur*
> *INDIA*
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.
__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Why don't the comments appear in the function?

2019-11-25 Thread Bert Gunter
I do not see this behavior.

f <- function(x){
   ## a comment
   2
}

g <- list(a =2, fun =f)

> g
$a
[1] 2

$fun
function(x){
   ## a comment
   2
}

> g[[2]]
function(x){
   ## a comment
   2
}
> g[2]
$fun
function(x){
   ## a comment
   2
}

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Nov 25, 2019 at 7:03 AM  wrote:

> I have made a list in which each element is a function.   If I print
> individual elements of the list, then the function code is shown along
> with embedded comments.  However, if I print the list or sublist, then the
> function code is shown without comments.   Why (and how) are the comments
> hidden?
>
> >flist[[3]]
> function(y,brackets,rates){
> # Calculates before-tax income required to realized value y
>
> ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
> x<-(y+ints)/(1-rates)
> x[sum(x>brackets)]
> }
>
>
> >flist[3]
> $btv
> function (y, brackets, rates)
> {
> ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
> 1)])) - brackets * rates
> x <- (y + ints)/(1 - rates)
> x[sum(x > brackets)]
> }
>
> I am running R 3.3.2 on Mac OS X  10.10.5
>
> Mik Bickis
>
> __
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Why don't the comments appear in the function?

2019-11-25 Thread Bert Gunter
Dunno.

> R.Version()
$platform
[1] "x86_64-apple-darwin15.6.0"

$arch
[1] "x86_64"

$os
[1] "darwin15.6.0"

$system
[1] "x86_64, darwin15.6.0"

$status
[1] ""

$major
[1] "3"

$minor
[1] "6.1"

$year
[1] "2019"

$month
[1] "07"

$day
[1] "05"

$`svn rev`
[1] "76782"

$language
[1] "R"

$version.string
[1] "R version 3.6.1 (2019-07-05)"

$nickname
[1] "Action of the Toes"

You should upgrade to the latest version (which I have not done yet) if you
haven't already done so.

Additional notes:
I saw the same results at the command line from R -- vanilla that I got
from the RStudio console(what I showed you). Have you done this? This
should indicate whether it might have something to do with your
environment, etc..

However, executing the file with Rscript stripped the comments from both
g[[2]] and g[2].

Cheers,
Bert



On Mon, Nov 25, 2019 at 9:40 AM  wrote:

> Different version of R perhaps?   Or is there something in my environment
> or "preferences" that is different?
>
> Here's what I get with your example.
>
> >  f <- function(x){
> + ## a comment
> + 2
> +  }
> > g <- list(a =2, fun =f)
> > g
> $a
> [1] 2
>
> $fun
> function (x)
> {
> 2
> }
>
> > g[[2]]
> function(x){
> ## a comment
> 2
>  }
> > g[1]
> $a
> [1] 2
>
>
>
>
> Mik
>
>
> > I do not see this behavior.
> >
> > f <- function(x){
> >## a comment
> >2
> > }
> >
> > g <- list(a =2, fun =f)
> >
> >> g
> > $a
> > [1] 2
> >
> > $fun
> > function(x){
> >## a comment
> >2
> > }
> >
> >> g[[2]]
> > function(x){
> >## a comment
> >2
> > }
> >> g[2]
> > $fun
> > function(x){
> >## a comment
> >2
> > }
> >
> > Cheers,
> > Bert
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along
> and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Mon, Nov 25, 2019 at 7:03 AM
> > mailto:bic...@math.usask.ca>> wrote:
> > I have made a list in which each element is a function.   If I print
> > individual elements of the list, then the function code is shown along
> > with embedded comments.  However, if I print the list or sublist, then
> the
> > function code is shown without comments.   Why (and how) are the comments
> > hidden?
> >
> >>flist[[3]]
> > function(y,brackets,rates){
> > # Calculates before-tax income required to realized value y
> >
>  ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
> > x<-(y+ints)/(1-rates)
> > x[sum(x>brackets)]
> > }
> >
> >
> >>flist[3]
> > $btv
> > function (y, brackets, rates)
> > {
> > ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
> > 1)])) - brackets * rates
> > x <- (y + ints)/(1 - rates)
> > x[sum(x > brackets)]
> > }
> >
> > I am running R 3.3.2 on Mac OS X  10.10.5
> >
> > Mik Bickis
> >
> > __
> > R-help@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
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>

[[alternative HTML version deleted]]

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] giving priority to stats package

2019-11-25 Thread Greg Snow
You could use the `pos` arg to place the newly loaded package(s) on
the search path after the stats package.  That would give priority for
any functions in the stats package over the newly loaded package (but
also give priority for any other packages earlier on the search path).

On Sat, Nov 23, 2019 at 6:25 AM Gabor Grothendieck
 wrote:
>
> library and require have new args in 3.6 giving additional control
> over conflicts.  This seems very useful but I was wondering if there
> were some, preferabley simple, way to give existing loaded packages
> priority without knowing the actual conflicts in advance.  For example
>
> library(dplyr, exclude = c("filter", "lag"))
>
> works to avoid masking those names in stats that would otherwise be
> masked by that package but I had to know in advance that filter and
> lag were the conflicting names.
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> __
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Why don't the comments appear in the function?

2019-11-25 Thread Duncan Murdoch

On 24/11/2019 6:06 p.m., bic...@math.usask.ca wrote:

I have made a list in which each element is a function.   If I print
individual elements of the list, then the function code is shown along
with embedded comments.  However, if I print the list or sublist, then the
function code is shown without comments.   Why (and how) are the comments
hidden?


Hi Mik.  I see the same behaviour as Bert, not what you see.

Generally the comments will be shown if the "srcref" attribute is 
attached to the function, and it is a valid one.  That shouldn't change 
between looking at flist[[3]] and flist[3].  If it's not there or not 
valid, you'll see a deparsed version of the function; that's what your 
flist[3]$btv looks like.


It's possible this is R 3.3.2-specific; that's a relatively old version 
now, but I don't know of any change that would cause this.  Can you show 
us the code that you used to create flist, or enough of it to show this 
behaviour?  Showing us (or me privately) dput(flist) might be enough to 
see what's going on.


Duncan Murdoch




flist[[3]]

function(y,brackets,rates){
# Calculates before-tax income required to realized value y

ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
x<-(y+ints)/(1-rates)
x[sum(x>brackets)]
}



flist[3]

$btv
function (y, brackets, rates)
{
 ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
 1)])) - brackets * rates
 x <- (y + ints)/(1 - rates)
 x[sum(x > brackets)]
}

I am running R 3.3.2 on Mac OS X  10.10.5

Mik Bickis

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.



__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Why don't the comments appear in the function?

2019-11-25 Thread Neal Fultz
Hi Mik,

Echoing Bert and Duncan's suggestions, please see also ?srcref and
?getSrcref
and consider upgrading to a currently supported version of R.

I'd also call out the "keep.source" installation option as important if
your functions
are inside a package - there is some discussion around this at
https://github.com/DeclareDesign/DesignLibrary/issues/50 and linked issues
from
the last time I encountered this weird corner of R.

If anyone finds a better solution (or for the related problem of preserving
whitespace),
I would be interested as well.

Very respectfully,

Neal Fultz

On Mon, Nov 25, 2019 at 10:49 AM Duncan Murdoch 
wrote:

> On 24/11/2019 6:06 p.m., bic...@math.usask.ca wrote:
> > I have made a list in which each element is a function.   If I print
> > individual elements of the list, then the function code is shown along
> > with embedded comments.  However, if I print the list or sublist, then
> the
> > function code is shown without comments.   Why (and how) are the comments
> > hidden?
>
> Hi Mik.  I see the same behaviour as Bert, not what you see.
>
> Generally the comments will be shown if the "srcref" attribute is
> attached to the function, and it is a valid one.  That shouldn't change
> between looking at flist[[3]] and flist[3].  If it's not there or not
> valid, you'll see a deparsed version of the function; that's what your
> flist[3]$btv looks like.
>
> It's possible this is R 3.3.2-specific; that's a relatively old version
> now, but I don't know of any change that would cause this.  Can you show
> us the code that you used to create flist, or enough of it to show this
> behaviour?  Showing us (or me privately) dput(flist) might be enough to
> see what's going on.
>
> Duncan Murdoch
>
> >
> >> flist[[3]]
> > function(y,brackets,rates){
> >   # Calculates before-tax income required to realized value y
> >
>  ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
> >   x<-(y+ints)/(1-rates)
> >   x[sum(x>brackets)]
> > }
> >
> >
> >> flist[3]
> > $btv
> > function (y, brackets, rates)
> > {
> >  ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
> >  1)])) - brackets * rates
> >  x <- (y + ints)/(1 - rates)
> >  x[sum(x > brackets)]
> > }
> >
> > I am running R 3.3.2 on Mac OS X  10.10.5
> >
> > Mik Bickis
> >
> > __
> > R-help@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
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> __
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Why don't the comments appear in the function?

2019-11-25 Thread bickis
Different version of R perhaps?   Or is there something in my environment
or "preferences" that is different?

Here's what I get with your example.

>  f <- function(x){
+ ## a comment
+ 2
+  }
> g <- list(a =2, fun =f)
> g
$a
[1] 2

$fun
function (x)
{
2
}

> g[[2]]
function(x){
## a comment
2
 }
> g[1]
$a
[1] 2




Mik


> I do not see this behavior.
>
> f <- function(x){
>## a comment
>2
> }
>
> g <- list(a =2, fun =f)
>
>> g
> $a
> [1] 2
>
> $fun
> function(x){
>## a comment
>2
> }
>
>> g[[2]]
> function(x){
>## a comment
>2
> }
>> g[2]
> $fun
> function(x){
>## a comment
>2
> }
>
> Cheers,
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Mon, Nov 25, 2019 at 7:03 AM
> mailto:bic...@math.usask.ca>> wrote:
> I have made a list in which each element is a function.   If I print
> individual elements of the list, then the function code is shown along
> with embedded comments.  However, if I print the list or sublist, then the
> function code is shown without comments.   Why (and how) are the comments
> hidden?
>
>>flist[[3]]
> function(y,brackets,rates){
> # Calculates before-tax income required to realized value y
> 
> ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates
> x<-(y+ints)/(1-rates)
> x[sum(x>brackets)]
> }
>
>
>>flist[3]
> $btv
> function (y, brackets, rates)
> {
> ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
> 1)])) - brackets * rates
> x <- (y + ints)/(1 - rates)
> x[sum(x > brackets)]
> }
>
> I am running R 3.3.2 on Mac OS X  10.10.5
>
> Mik Bickis
>
> __
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Why don't the comments appear in the function?

2019-11-25 Thread Duncan Murdoch

On 25/11/2019 2:27 p.m., Neal Fultz wrote:

Hi Mik,

Echoing Bert and Duncan's suggestions, please see also ?srcref and
?getSrcref
and consider upgrading to a currently supported version of R.

I'd also call out the "keep.source" installation option as important if
your functions
are inside a package - there is some discussion around this at
https://github.com/DeclareDesign/DesignLibrary/issues/50 and linked issues
from
the last time I encountered this weird corner of R.

If anyone finds a better solution (or for the related problem of preserving
whitespace),
I would be interested as well.


Preserving comments and whitespace are definitely the same issue.  I'm 
not sure what solution you're trying to improve, but if you want source 
kept in packages, run


Sys.setenv(R_KEEP_PKG_SOURCE="yes")

before doing the install using install.packages(), or use the command 
line option with R CMD INSTALL.


If you want the source kept for your own function regardless of the 
user's wishes, I think you'll have to do some weird things in your 
source files.  For example, if pkg::fn is defined in the file 
pkg/R/fn.R, you could put this into your zzz.R file (any name is fine, 
as long as it collates after fn.R):


 sys.source("R/fn.R", keep.source = TRUE, envir = getNamespace("pkg"))

This will replace fn with a new copy that has source references.  It 
assumes that the namespace has been created at this point in 
installation; I think that's true, but there may be cases where it's not 
true, in which case you'll need to use something like environment(fn) as 
the envir argument.


Duncan Murdoch



Very respectfully,

Neal Fultz

On Mon, Nov 25, 2019 at 10:49 AM Duncan Murdoch 
wrote:


On 24/11/2019 6:06 p.m., bic...@math.usask.ca wrote:

I have made a list in which each element is a function.   If I print
individual elements of the list, then the function code is shown along
with embedded comments.  However, if I print the list or sublist, then

the

function code is shown without comments.   Why (and how) are the comments
hidden?


Hi Mik.  I see the same behaviour as Bert, not what you see.

Generally the comments will be shown if the "srcref" attribute is
attached to the function, and it is a valid one.  That shouldn't change
between looking at flist[[3]] and flist[3].  If it's not there or not
valid, you'll see a deparsed version of the function; that's what your
flist[3]$btv looks like.

It's possible this is R 3.3.2-specific; that's a relatively old version
now, but I don't know of any change that would cause this.  Can you show
us the code that you used to create flist, or enough of it to show this
behaviour?  Showing us (or me privately) dput(flist) might be enough to
see what's going on.

Duncan Murdoch




flist[[3]]

function(y,brackets,rates){
   # Calculates before-tax income required to realized value y


  ints<-c(0,cumsum(diff(brackets)*rates[1:(length(rates)-1)]))-brackets*rates

   x<-(y+ints)/(1-rates)
   x[sum(x>brackets)]
}



flist[3]

$btv
function (y, brackets, rates)
{
  ints <- c(0, cumsum(diff(brackets) * rates[1:(length(rates) -
  1)])) - brackets * rates
  x <- (y + ints)/(1 - rates)
  x[sum(x > brackets)]
}

I am running R 3.3.2 on Mac OS X  10.10.5

Mik Bickis

__
R-help@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

and provide commented, minimal, self-contained, reproducible code.



__
R-help@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
and provide commented, minimal, self-contained, reproducible code.



[[alternative HTML version deleted]]

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.



__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] ggplot inside of the function

2019-11-25 Thread Thevaraja, Mayooran
Great it works. Thank you so much Eric.

Cheers,

Mayooran

-Original Message-
From: Eric Berger  
Sent: Monday, 25 November 2019 11:22 PM
To: Thevaraja, Mayooran 
Cc: r-help 
Subject: Re: [R] ggplot inside of the function

Hi Mayooran,
If you define the following function

f <- function(m,v) { sprintf("Norm(mu=%.1f, var=%.1f)",m,v) }

Then you can modify the setting of Prob as follows

 Prob <- plyr::rename(Prob_df,
c("p_1"=f(mu1,var1),"p_2"=f(mu2,var2),"p_3"=f(mu3,var3)))

The lesson here is that wherever you set a variable to a string, as in 
"p_1"="Norm(mu=mu1,var=var1)", you can create the string dynamically using the 
function sprintf(), which returns a string.

Best,
Eric

On Mon, Nov 25, 2019 at 3:54 AM Thevaraja, Mayooran  
wrote:
>
> Hello Folks
>I am working some R package development. When I was using 
> ggplot inside of the function, I need to get the output graph's legend must 
> be corresponding to the input parameters numerical value (need to be 
> automatically changed when we input different parameters).  Therefore anyone 
> has any ideas? Here, I have given below a simple example. I need to get my 
> output graph's legend should be Norm(mu=0.2, var=0.8),...
>
>
> example <- function(mu1,mu2,mu3,var1,var2,var3){
>   x <- seq(-3,3, by=0.01)
>   p_1 <- dnorm(x,mu1,var1)
>   p_2 <- dnorm(x,mu2,var2)
>   p_3 <- dnorm(x,mu3,var3)
>   Prob_df <- data.frame(x,p_1,p_2,p_3)
>   Prob <- plyr::rename(Prob_df, c("p_1"="Norm(mu=mu1, 
> var=var1)","p_2"="Norm(mu=mu2, var=var2)","p_3"="Norm(mu=mu3, var=var3)"))
>   melten.Prob <- reshape2::melt(Prob, id = "x", variable.name = 
> "Methods", value.name = "Prob") ggplot2::ggplot(melten.Prob)+
> ggplot2::geom_point(ggplot2::aes(x = x, y = Prob, group= Methods, colour 
> = Methods ))+
> ggplot2::geom_line(ggplot2::aes(x = x, y = Prob, group= Methods, 
> colour = Methods ))
>
> }
>
> mu1 <- 0.2
> mu2 <- 0.5
> mu3 <- 1
> var1 <- 0.8
> var2 <- 1.2
> var3 <- 2.1
> example(mu1,mu2,mu3,var1,var2,var3)
>
>
> Thanks
> Mayooran
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.
__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] giving priority to stats package

2019-11-25 Thread Gabor Grothendieck
Goold idea.  This seems to work.

  library(dplyr, pos = grep("package:stats", search()) + 1)

On Mon, Nov 25, 2019 at 1:27 PM Greg Snow <538...@gmail.com> wrote:
>
> You could use the `pos` arg to place the newly loaded package(s) on
> the search path after the stats package.  That would give priority for
> any functions in the stats package over the newly loaded package (but
> also give priority for any other packages earlier on the search path).
>
> On Sat, Nov 23, 2019 at 6:25 AM Gabor Grothendieck
>  wrote:
> >
> > library and require have new args in 3.6 giving additional control
> > over conflicts.  This seems very useful but I was wondering if there
> > were some, preferabley simple, way to give existing loaded packages
> > priority without knowing the actual conflicts in advance.  For example
> >
> > library(dplyr, exclude = c("filter", "lag"))
> >
> > works to avoid masking those names in stats that would otherwise be
> > masked by that package but I had to know in advance that filter and
> > lag were the conflicting names.
> >
> > --
> > Statistics & Software Consulting
> > GKX Group, GKX Associates Inc.
> > tel: 1-877-GKX-GROUP
> > email: ggrothendieck at gmail.com
> >
> > __
> > R-help@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
> > and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> 538...@gmail.com



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.