Re: [R] srt --- slope text with function?
On 2/11/06, Patricia J. Hawkins <[EMAIL PROTECTED]> wrote:
> > "iw" == ivo welch <[EMAIL PROTECTED]> writes:
>
> iw> * SUGGESTION: can we please offer the "?:" operator ala C in
> iw> addition to ifelse()? This would make R code prettier.
>
> R:
> if (condition) xxx else yyy
> if (condition) xxx else if (yyy) zzz else qqq
>
> C:
> condition ? xxx : yyy;
> condition ? xxx : yyy ? zzz : qqq;
>
> If this is a beauty contest, or a readability contest, I'll take R!
>
> ?: is pleasant in C because it avoids so much of the C syntactic
> sugar, of which R is largely free.
>
> As for ifelse(), for a trivial example, try running:
> ifelse(array(rep(c(1,0), 50), dim=c(10,10)), "00", "99")
Maybe this is not the point but in this particular example
we could write:
matrix(c("00", "99"), 10, 10)
>
> iw> Similarly, perl has a nice construct, which would be lovely, but
> iw> which may not jive with the
> iw> R syntax:
> iw> condition or die("the condition", condition, "has failed");
> iw> condition and cat("my condition", condition, "is met");
>
>if (!condition) stop("the condition", condition, "has failed")
>if (condition) cat("my condition", condition, "is met")
>
Try this:
stopifnot(condition)
> --
> Patricia J. Hawkins
> Hawkins Internet Applications
> www.hawkinsia.com
>
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
> "iw" == ivo welch <[EMAIL PROTECTED]> writes:
iw> * SUGGESTION: can we please offer the "?:" operator ala C in
iw> addition to ifelse()? This would make R code prettier.
R:
if (condition) xxx else yyy
if (condition) xxx else if (yyy) zzz else qqq
C:
condition ? xxx : yyy;
condition ? xxx : yyy ? zzz : qqq;
If this is a beauty contest, or a readability contest, I'll take R!
?: is pleasant in C because it avoids so much of the C syntactic
sugar, of which R is largely free.
As for ifelse(), for a trivial example, try running:
ifelse(array(rep(c(1,0), 50), dim=c(10,10)), "00", "99")
iw> Similarly, perl has a nice construct, which would be lovely, but
iw> which may not jive with the
iw> R syntax:
iw> condition or die("the condition", condition, "has failed");
iw> condition and cat("my condition", condition, "is met");
if (!condition) stop("the condition", condition, "has failed")
if (condition) cat("my condition", condition, "is met")
--
Patricia J. Hawkins
Hawkins Internet Applications
www.hawkinsia.com
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
Thank you, as always.
May I disagree with you and offer a suggestion?
In the best of worlds, a function should have multiple attributes. In
addition to the function name and its argument list, two mandatory
attributes should be a filename and lineno. If the function is
interactively created, perhaps we can call the filename "-" and the
lineno a count that could come from the history(). Yes, it will
*NOT* be perfect, but it would be a big improvement.
If functions can have attributes, in addition to keeping the filename
and lineno, it would be great if it could have an immediate
association with a short documentation message? This could be a neat
documentation crutch, useful, e.g., in ls.str() or a describe(). the
best syntax that I can think of just stinks, but maybe a standard
argument does the job and should be encouraged.
xyz <- function(args, doc="xyz returns 0") { return(0); }
I do not think the following syntax would work stylewise:
xyz <- function "xyz returns 0" (args) { return(0); }
but it would be nicer in noting the difference between a normal
argument and a short function descriptor.
regards,
/iaw
by traceback(), do you mean the gdb() like traceback of the R
internals? This would not be too helpful to most users. I would be
more interested in my R call stack, not my underlying C call stack.
[and thanks again for all the other info].
regards,
/iaw
On 2/6/06, Prof Brian Ripley <[EMAIL PROTECTED]> wrote:
> 'Actually', R does what you ask it to: see options(error). But before the
> program stops, there is no error to report. In interactive use I see no
> problem in typing traceback() or using recover() (see below), but for
> batch use we are looking at alternatives. For example, in R 2.3.0
> (modern) Unix users will get a traceback after a segfault.
>
> > There's going to be a new section on debugging in the R 2.3.0 "Writing R
> > Extensions" manual (written by Brian Ripley). You can see it now if you
> > build R-devel (or download a binary build from CRAN; I put Windows
> > builds there approximately daily). I also put together a
> > Windows-oriented debugging page at
> > http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/.
>
> filename+lineno makes no sense: R functions are not (in the main) from
> files. They can be autogenerated (and often are). And I can 'fix' them.
>
> As for the call stack, try options(error=recover) and 'where': see the
> chapter Duncan pointed you to.
>
> >> * is there a way to print all my user defined functions? I have an
> >> init file, in which I am defining all sorts of useful utility
> >> functions, and I would like to print what I have defined (for memory)
> >> upon a read of this init file? that is, something that has
> >> functionality like
> >>note.all.local.definitions.now.in.vector( all.local.functions )
> >>a <- function() { }
> >>b <- function() { }
> >>cat( all.local.functions ); # should print 'a' and 'b'.
> >
> > ls.str() includes an option to select only functions. You could also
> > write your own filter for ls() output.
> >>
> >> * is there a string variable that gives me the name of the current
> >> function I am in?
> >
> > I don't think so, but sys.call() gets close. Watch out though: the
> > name you see will be the name used by the caller, which may not be the
> > name used when the function was written. For example, in a function
> > called from apply(), you'll see FUN, not the original name. Objects
> > don't know their own names in general, because they keep getting passed
> > around.
>
> In the debugging context the command 'where' tells you the sequence of
> calls (which can be more helpful).
>
> More generally, functions need not even have names (Bill Venables calls
> them 'anonymous functions'), as in
>
> r <- sapply(nms,
> function(n) if (exists(n, envir = envir, mode = mode))n
> else as.character(NA))
>
> from ls.str.
>
>
> --
> Brian D. Ripley, [EMAIL PROTECTED]
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UKFax: +44 1865 272595
>
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
On Sun, 5 Feb 2006, Duncan Murdoch wrote:
> On 2/5/2006 4:28 PM, ivo welch wrote:
>> Dear R Wizards: To bore everyone to death, below is an improved and
>> hopefully final version of my native.slope() function. (Thanks, Jim.)
>> In case you are not asleep yet reading yet another post of mine, may
>> I ask the experts some programming questions?
>>
>> [I just saw yesterday's threat---I think it would be very, very, very nice
>> if
>> R would remember from what files with what linenumbers code came from.
>> thus ignore below questions that mention this feature. ALAS, I understand
>> that this cannot be done generally. But could not function definitions at
>> least have a component that remembers this?]
>>
>> * Is there a way that I can print an R functional backtrace? Instead
>> of passing my subroutine name to my function "assert()" below, it
>> would be nice if I could just print the whole call tree [preferably
>> with file names+line numbers] when I want my program to gracefully
>> bonk out. (PS: I think that this is also what R should print when any
>> user program bonks out, not just the stop message.)
>> Actually, I just learned about traceback(), which functions nicely
>> AFTER the program stops, not before. But it gives no traceback
>> earlier---and it does not give filename+lineno.
'Actually', R does what you ask it to: see options(error). But before the
program stops, there is no error to report. In interactive use I see no
problem in typing traceback() or using recover() (see below), but for
batch use we are looking at alternatives. For example, in R 2.3.0
(modern) Unix users will get a traceback after a segfault.
> There's going to be a new section on debugging in the R 2.3.0 "Writing R
> Extensions" manual (written by Brian Ripley). You can see it now if you
> build R-devel (or download a binary build from CRAN; I put Windows
> builds there approximately daily). I also put together a
> Windows-oriented debugging page at
> http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/.
filename+lineno makes no sense: R functions are not (in the main) from
files. They can be autogenerated (and often are). And I can 'fix' them.
As for the call stack, try options(error=recover) and 'where': see the
chapter Duncan pointed you to.
>> * is there a way to print all my user defined functions? I have an
>> init file, in which I am defining all sorts of useful utility
>> functions, and I would like to print what I have defined (for memory)
>> upon a read of this init file? that is, something that has
>> functionality like
>>note.all.local.definitions.now.in.vector( all.local.functions )
>>a <- function() { }
>>b <- function() { }
>>cat( all.local.functions ); # should print 'a' and 'b'.
>
> ls.str() includes an option to select only functions. You could also
> write your own filter for ls() output.
>>
>> * is there a string variable that gives me the name of the current
>> function I am in?
>
> I don't think so, but sys.call() gets close. Watch out though: the
> name you see will be the name used by the caller, which may not be the
> name used when the function was written. For example, in a function
> called from apply(), you'll see FUN, not the original name. Objects
> don't know their own names in general, because they keep getting passed
> around.
In the debugging context the command 'where' tells you the sequence of
calls (which can be more helpful).
More generally, functions need not even have names (Bill Venables calls
them 'anonymous functions'), as in
r <- sapply(nms,
function(n) if (exists(n, envir = envir, mode = mode))n
else as.character(NA))
from ls.str.
--
Brian D. Ripley, [EMAIL PROTECTED]
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax: +44 1865 272595
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function? (list user defined functions)
On Sun, 5 Feb 2006, Pierre Kleiber wrote:
> I'll respond to one of your multitude of queries below. Suggest that you
> confine your postings to a single issue so that the subject heading is more
> meaningful.
>
> Cheers, Pierre
>
>
> ivo welch offered the following remark on 02/05/06 11:28...
> [...]
>>
>> * is there a way to print all my user defined functions? I have an
>> init file, in which I am defining all sorts of useful utility
>> functions, and I would like to print what I have defined (for memory)
>> upon a read of this init file? that is, something that has
>> functionality like
>>note.all.local.definitions.now.in.vector( all.local.functions )
>>a <- function() { }
>>b <- function() { }
>>cat( all.local.functions ); # should print 'a' and 'b'.
>
> The following lists the functions defined in a (default current) environment:
>
> "lsf" <- function (pos = 1)
> {
> junk <- ls(pos, all.names = TRUE)
> junk[sapply(junk, function(x) is.function(eval(as.symbol(x]
> }
I am sorry, it does not in general (since eval is not looking in that
environment). See ls.str (in utils) for a more elegant way to do this via
exists(mode = "function")
--
Brian D. Ripley, [EMAIL PROTECTED]
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax: +44 1865 272595
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
On 2/5/2006 4:28 PM, ivo welch wrote:
> Dear R Wizards: To bore everyone to death, below is an improved and
> hopefully final version of my native.slope() function. (Thanks, Jim.)
> In case you are not asleep yet reading yet another post of mine, may
> I ask the experts some programming questions?
>
> [I just saw yesterday's threat---I think it would be very, very, very nice
> if
> R would remember from what files with what linenumbers code came from.
> thus ignore below questions that mention this feature. ALAS, I understand
> that this cannot be done generally. But could not function definitions at
> least have a component that remembers this?]
>
> * Is there a way that I can print an R functional backtrace? Instead
> of passing my subroutine name to my function "assert()" below, it
> would be nice if I could just print the whole call tree [preferably
> with file names+line numbers] when I want my program to gracefully
> bonk out. (PS: I think that this is also what R should print when any
> user program bonks out, not just the stop message.) Actually, I just
> learned about traceback(), which functions nicely AFTER the program
> stops, not before. But it gives no traceback earlier---and it does
> not give filename+lineno.
There's going to be a new section on debugging in the R 2.3.0 "Writing R
Extensions" manual (written by Brian Ripley). You can see it now if you
build R-devel (or download a binary build from CRAN; I put Windows
builds there approximately daily). I also put together a
Windows-oriented debugging page at
http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/.
>
> * is there a way to print all my user defined functions? I have an
> init file, in which I am defining all sorts of useful utility
> functions, and I would like to print what I have defined (for memory)
> upon a read of this init file? that is, something that has
> functionality like
>note.all.local.definitions.now.in.vector( all.local.functions )
>a <- function() { }
>b <- function() { }
>cat( all.local.functions ); # should print 'a' and 'b'.
ls.str() includes an option to select only functions. You could also
write your own filter for ls() output.
>
> * is there a string variable that gives me the name of the current
> function I am in?
I don't think so, but sys.call() gets close. Watch out though: the
name you see will be the name used by the caller, which may not be the
name used when the function was written. For example, in a function
called from apply(), you'll see FUN, not the original name. Objects
don't know their own names in general, because they keep getting passed
around.
> * SUGGESTION: can we please offer the "?:" operator ala C in addition
> to ifelse()? This would make R code prettier. Similarly, perl has a
> nice construct, which would be lovely, but which may not jive with the
> R syntax:
> condition or die("the condition", condition, "has failed");
> condition and cat("my condition", condition, "is met");
> I believe "or" and "and" are not defined, so this may be possible...
The ? operator is already used as a prefix and infix operator (for the
help system), so this seems unlikely.
> * has it now become possible to embed sample data sets in R code? (I
> asked this a while back, but there were nly kludges, no standard
> "pretty" solutions.)
I don't understand the question. dump() produces R code that can
reconstruct a dataset; is that what you want?
>
> * SUGGESTION: The ?"function" docs would be terrific if they had a
> small example at the end that showed how to return multiple arguments,
> and then pick them up. I believe this is best done through lists, but
> I am not sure.
I'd guess most people learn about functions from the Intro to R manual
rather than the "function" man page, but this is a good suggestion.
Duncan Murdoch
>
> I find myself programming more and more in R, so I am beginning to see
> it as my standard language, rather than as a statistical program.
>
> Regards,
>
> /iaw
>
>
>
> native.slope computes a suitable srt from a function around
> a point on a function. This is useful until text() gets
> an srt parameter that is relative to the coordinate system.
> (Ideally, R would be able to slope along a function.)
>
>
> native.slope <- function( x, y, where.i, debug =0) {
>
> assert <- function( condition, routine, ... ) {
> if (condition) return(NULL);
> cat(paste(routine,...));
> stop("THIS IS A FATAL ERROR!\n");
> }
>
> subname= "native.slope"; # until I discover how to print a complete
> backtrace, this is it.
>
> assert( length(x) == length(y), subname,
>"Sorry, but x and y must have equal dimensions, not ", length(x), "
> and ", length(y), "\n");
>
> ## try to take a symmetric field around the point to
Re: [R] srt --- slope text with function? (list user defined functions)
I'll respond to one of your multitude of queries below. Suggest that you
confine your postings to a single issue so that the subject heading is more
meaningful.
Cheers, Pierre
ivo welch offered the following remark on 02/05/06 11:28...
[...]
>
> * is there a way to print all my user defined functions? I have an
> init file, in which I am defining all sorts of useful utility
> functions, and I would like to print what I have defined (for memory)
> upon a read of this init file? that is, something that has
> functionality like
>note.all.local.definitions.now.in.vector( all.local.functions )
>a <- function() { }
>b <- function() { }
>cat( all.local.functions ); # should print 'a' and 'b'.
The following lists the functions defined in a (default current) environment:
"lsf" <- function (pos = 1)
{
junk <- ls(pos, all.names = TRUE)
junk[sapply(junk, function(x) is.function(eval(as.symbol(x]
}
>
[...]
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
-
Pierre Kleiber, Ph.D Email: [EMAIL PROTECTED]
Pacific Island Fisheries Science Center Tel: 808 983-5399
NOAA Fisheries
2570 Dole St., Honolulu, HI 96822-2396
-
"God could have told Moses about galaxies and mitochondria and
all. But behold... It was good enough for government work."
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
Dear R Wizards: To bore everyone to death, below is an improved and
hopefully final version of my native.slope() function. (Thanks, Jim.)
In case you are not asleep yet reading yet another post of mine, may
I ask the experts some programming questions?
[I just saw yesterday's threat---I think it would be very, very, very nice if
R would remember from what files with what linenumbers code came from.
thus ignore below questions that mention this feature. ALAS, I understand
that this cannot be done generally. But could not function definitions at
least have a component that remembers this?]
* Is there a way that I can print an R functional backtrace? Instead
of passing my subroutine name to my function "assert()" below, it
would be nice if I could just print the whole call tree [preferably
with file names+line numbers] when I want my program to gracefully
bonk out. (PS: I think that this is also what R should print when any
user program bonks out, not just the stop message.) Actually, I just
learned about traceback(), which functions nicely AFTER the program
stops, not before. But it gives no traceback earlier---and it does
not give filename+lineno.
* is there a way to print all my user defined functions? I have an
init file, in which I am defining all sorts of useful utility
functions, and I would like to print what I have defined (for memory)
upon a read of this init file? that is, something that has
functionality like
note.all.local.definitions.now.in.vector( all.local.functions )
a <- function() { }
b <- function() { }
cat( all.local.functions ); # should print 'a' and 'b'.
* is there a string variable that gives me the name of the current
function I am in?
* SUGGESTION: can we please offer the "?:" operator ala C in addition
to ifelse()? This would make R code prettier. Similarly, perl has a
nice construct, which would be lovely, but which may not jive with the
R syntax:
condition or die("the condition", condition, "has failed");
condition and cat("my condition", condition, "is met");
I believe "or" and "and" are not defined, so this may be possible...
* has it now become possible to embed sample data sets in R code? (I
asked this a while back, but there were nly kludges, no standard
"pretty" solutions.)
* SUGGESTION: The ?"function" docs would be terrific if they had a
small example at the end that showed how to return multiple arguments,
and then pick them up. I believe this is best done through lists, but
I am not sure.
I find myself programming more and more in R, so I am beginning to see
it as my standard language, rather than as a statistical program.
Regards,
/iaw
native.slope computes a suitable srt from a function around
a point on a function. This is useful until text() gets
an srt parameter that is relative to the coordinate system.
(Ideally, R would be able to slope along a function.)
native.slope <- function( x, y, where.i, debug =0) {
assert <- function( condition, routine, ... ) {
if (condition) return(NULL);
cat(paste(routine,...));
stop("THIS IS A FATAL ERROR!\n");
}
subname= "native.slope"; # until I discover how to print a complete
backtrace, this is it.
assert( length(x) == length(y), subname,
"Sorry, but x and y must have equal dimensions, not ", length(x), "
and ", length(y), "\n");
## try to take a symmetric field around the point to be described
l0= ifelse( where.i<=1, 1, where.i-1);
l1= ifelse( where.i>=length(y), length(y), where.i+1);
assert( !is.na(x[l0]), subname, "Sorry, but x[",l0,"] is NaN");
assert( !is.na(x[l1]), subname, "Sorry, but x[",l1,"] is NaN");
assert( !is.na(y[l0]), subname, "Sorry, but y[",l0,"] is NaN");
assert( !is.na(y[l0]), subname, "Sorry, but y[",l1,"] is NaN");
assert( y[l1] != y[l0], subname, "Sorry, but you cannot draw a slope
on a point");
## native slope in a 1:1 coordinate system
d= ( (y[l0]-y[l1])/(x[l0]-x[l1]) );
if (is.na(d)) return(0); # we do not know how to handle an undefined
spot at a function!
## now adjust by the axis scale and size of plot area
.usr <- par('usr') # dimensions of user units
.plt <- par('plt') # percent of figure area that plot region is
d.m <- (.usr[4] - .usr[3]) / (.usr[2] - .usr[1]) * (.plt[2] -
.plt[1]) / (.plt[4] - .plt[3])
assert( !is.na(d.m), subname, "Internal Error: I do not have
sensible axis dimensions (", d.m, ")\n");
## now handle the drawing system
.fin = par('fin');
asp.ratio = .fin[1]/.fin[2];
assert( !is.na(asp.ratio), subname, "Internal Error: I do not have a
reasonable drawing aspect ratio");
net.slope= d/asp.ratio/d.m;
slope = atan(net.slope)/pi*180.0;
if (debug) {
cat("\t", subname, "debug: d=", d, " (",y[where.i-1],y[where.i+1],
x[where.i-1], x[where.i+1],")\n",
"\t\td.m=",d.m, " (", .usr, ",",
Re: [R] srt --- slope text with function?
I don't think it's got the slope exactly right - you'll see this if you
go to a really extreme aspect ratio by changing the shape of a window to
be long and thin before you call your code. To fix this:
- use "usr" rather than "xaxp" and "yaxp" to get the limits of the
plot region in user coordinates; those two refer to the ticks, not the
whole plot region as "usr" does.
- "fin" is the whole figure region, not just the plot region; you need
to use "plt" to modify it to find the plot region within it. So I think
the aspect ratio should really be done as
pars <- par("fin", "plt")
asp.ratio <- (diff(pars$plt)[1]*pars$fin[1]) /
(diff(pars$plt)[3]*pars$fin[2])
Some other suggestions:
- split the function into two: one that determines a slope from the
data, and one that converts a slope to an angle suitable for "srt". (I
think the latter would have pretty wide use; the former is pretty
specialized for data the way you're using it).
- use the fact that defaults in a function call can be local variables
in the function, so that you only need one call to par() instead of 4.
(The 4 calls probably take a negigible amount of time, but it just looks
wasteful to make them.)
Duncan Murdoch
On 2/4/2006 5:19 PM, ivo welch wrote:
> Thank you, Duncan. This led me to the info I needed. Here is a
> simple utility function that does what I needed---maybe it will come
> in helpful for others.
>
>
> native.slope computes a suitable srt from a function around
> a point on a function. This is useful until text() gets
> an srt parameter that is relative to the coordinate system.
> (Ideally, R would be able to slope along a function.)
>
>
> native.slope <- function( x, y, where.i,
>xlim = par()$xaxp, ylim= par()$yaxp,
>asp.ratio = (par()$fin)[1]/(par()$fin)[2] ) {
> if (where.i<=1) { return(0); }
> if (where.i>=length(y)) { return(0); }
> if (length(x)!=length(y)) {
> stop("native.slope: Sorry, but x and y must have equal dimensions,
> not ", length(x), " and ", length(y), "\n"); }
>
> # native slope in a 1:1 coordinate system
> d= ( (y[where.i-1]-y[where.i+1])/(x[where.i-1]-x[where.i+1]) );
> if (is.na(d)) return(0); # we do not know how to handle an undefined
> spot at a function!
>
> d.m= (ylim[2]-ylim[1])/(xlim[2]-xlim[1]); # now adjust by the axis scale
> if (is.na(d)) stop("native.slope: internal error, I do not have
> sensible axis dimensions (", xlim, ylim, ")\n");
>
> if (is.na(asp.ratio)) stop("native.slope: internal error, I do not
> have a reasonable drawing aspect ratio");
>
> net.slope= d/asp.ratio/d.m;
> return(slope = atan(net.slope)/pi*180.0 )
> }
>
>
> # some test code
> x<- seq(-10,20,by=0.1)
> y<- x*x;
>
> plot( x, y, type="l" );
>
> display= ((1:length(y))%%40 == 0)
>
> for (i in 1:(length(y))) {
> if (display[i]) {
> points(x[i],y[i], pch=19);
> srt= native.slope( x, y, i );
> text( x[i], y[i], paste(i,"=",x[i],"=",srt), srt=srt, cex=0.9 );
> }
> }
>
>
>
> On 2/4/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote:
>> On 2/4/2006 3:50 PM, ivo welch wrote:
>>> [resent, plus small addition; I do not understand why gmail sent a
>>> weird charset.]
>>>
>>> Dear R wizards:
>>>
>>> I would love to write a general function that matches the slope of a plotted
>>> line in an xy-plot at a particular x,y location. something like
>>>
>>>x<- (1:10)^2; y<- 40:50;
>>>plot( x,y, type="l", xlim=c(0,90) )
>>>srt.at5 = text.at.current.plot.with.slope( x, y, 5);
>>>text( x[5],y[5], pos=3, srt=srt.at.5);
>>>
>>> to do this, I first need to compute the function slope around x[5], which is
>>> an easy task. alas, the harder task is that I need to scale this by the
>>> plot aspect ratio and the axes. How can a function read this from the
>>> current plot?
>> I haven't done this, but you can presumably work it out from the
>> conversions implied by the "fig", "fin", "plt", and/or "usr" values.
>>> (Has someone written such a function, perhaps more embellished, to save me
>>> the debugging effort?)
>>>
>>> Or, is there an alternative to srt, which slopes the text relative to the
>>> existing scale?
>>>
>>> *** come to think of it, what I would really like is the ability of
>>> text to 'snake' itself along the line itself. I doubt that this is
>>> easily possible, but I just wanted to ask.
>> Using strsplit and strwidth you should be able to do it, but it will
>> probably look quite ugly.
>>
>> Duncan Murdoch
>>
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
Some more experimentation reveals that I need to also adjust for the
plot dimensions. (And there are other issues I probably do not know
yet and totally misprogrammed...just trying to be helpful.)
native.slope <- function( x, y, where.i,
xlim = par()$xaxp, ylim= par()$yaxp,
asp.ratio = (par()$fin)[1]/(par()$fin)[2],
debug =0) {
if (where.i<=1) { return(0); }
if (where.i>=length(y)) { return(0); }
if (length(x)!=length(y)) {
stop("native.slope: Sorry, but x and y must have equal dimensions,
not ", length(x), " and ", length(y), "\n"); }
# native slope in a 1:1 coordinate system
d= ( (y[where.i-1]-y[where.i+1])/(x[where.i-1]-x[where.i+1]) );
if (is.na(d)) return(0); # we do not know how to handle an undefined
spot at a function!
d.m= (ylim[2]-ylim[1])/(xlim[2]-xlim[1]); # now adjust by the axis scale
if (is.na(d)) stop("native.slope: internal error, I do not have
sensible axis dimensions (", xlim, ylim, ")\n");
if (is.na(asp.ratio)) stop("native.slope: internal error, I do not
have a reasonable drawing aspect ratio");
## alas, we also need to take into account the plot region:
pq= par()$plt; plt.distort= (pq[2]-pq[1])/(pq[4]-pq[3]);
net.slope= d/asp.ratio/d.m / plt.distort;
slope = atan(net.slope)/pi*180.0;
if (debug) {
cat("xlim=", par()$xaxp, "\n");
cat("ylim=", par()$yaxp, "\n\n");
cat("native.slope: d=", d, " (",y[where.i-1],y[where.i+1],
x[where.i-1], x[where.i+1],")",
"d.m=",d.m, " (", ylim[2],ylim[1],xlim[2],xlim[1], ")",
"asp.ratio=", (par()$fin)[1], ":", (par()$fin)[2], "==>", net.slope,
"=", slope, "deg\n");
}
return( slope = slope );
}
On 2/4/06, ivo welch <[EMAIL PROTECTED]> wrote:
> Thank you, Duncan. This led me to the info I needed. Here is a
> simple utility function that does what I needed---maybe it will come
> in helpful for others.
>
>
> native.slope computes a suitable srt from a function around
> a point on a function. This is useful until text() gets
> an srt parameter that is relative to the coordinate system.
> (Ideally, R would be able to slope along a function.)
>
> [old function deleted]
>
> # some test code
> x<- seq(-10,20,by=0.1)
> y<- x*x;
>
> plot( x, y, type="l" );
>
> display= ((1:length(y))%%40 == 0)
>
> for (i in 1:(length(y))) {
> if (display[i]) {
> points(x[i],y[i], pch=19);
> srt= native.slope( x, y, i );
> text( x[i], y[i], paste(i,"=",x[i],"=",srt), srt=srt, cex=0.9 );
> }
> }
>
>
>
> On 2/4/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote:
> > On 2/4/2006 3:50 PM, ivo welch wrote:
> > > [resent, plus small addition; I do not understand why gmail sent a
> > > weird charset.]
> > >
> > > Dear R wizards:
> > >
> > > I would love to write a general function that matches the slope of a
> > > plotted
> > > line in an xy-plot at a particular x,y location. something like
> > >
> > >x<- (1:10)^2; y<- 40:50;
> > >plot( x,y, type="l", xlim=c(0,90) )
> > >srt.at5 = text.at.current.plot.with.slope( x, y, 5);
> > >text( x[5],y[5], pos=3, srt=srt.at.5);
> > >
> > > to do this, I first need to compute the function slope around x[5], which
> > > is
> > > an easy task. alas, the harder task is that I need to scale this by the
> > > plot aspect ratio and the axes. How can a function read this from the
> > > current plot?
> >
> > I haven't done this, but you can presumably work it out from the
> > conversions implied by the "fig", "fin", "plt", and/or "usr" values.
> > >
> > > (Has someone written such a function, perhaps more embellished, to save me
> > > the debugging effort?)
> > >
> > > Or, is there an alternative to srt, which slopes the text relative to the
> > > existing scale?
> > >
> > > *** come to think of it, what I would really like is the ability of
> > > text to 'snake' itself along the line itself. I doubt that this is
> > > easily possible, but I just wanted to ask.
> >
> > Using strsplit and strwidth you should be able to do it, but it will
> > probably look quite ugly.
> >
> > Duncan Murdoch
> >
>
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
Thank you, Duncan. This led me to the info I needed. Here is a
simple utility function that does what I needed---maybe it will come
in helpful for others.
native.slope computes a suitable srt from a function around
a point on a function. This is useful until text() gets
an srt parameter that is relative to the coordinate system.
(Ideally, R would be able to slope along a function.)
native.slope <- function( x, y, where.i,
xlim = par()$xaxp, ylim= par()$yaxp,
asp.ratio = (par()$fin)[1]/(par()$fin)[2] ) {
if (where.i<=1) { return(0); }
if (where.i>=length(y)) { return(0); }
if (length(x)!=length(y)) {
stop("native.slope: Sorry, but x and y must have equal dimensions,
not ", length(x), " and ", length(y), "\n"); }
# native slope in a 1:1 coordinate system
d= ( (y[where.i-1]-y[where.i+1])/(x[where.i-1]-x[where.i+1]) );
if (is.na(d)) return(0); # we do not know how to handle an undefined
spot at a function!
d.m= (ylim[2]-ylim[1])/(xlim[2]-xlim[1]); # now adjust by the axis scale
if (is.na(d)) stop("native.slope: internal error, I do not have
sensible axis dimensions (", xlim, ylim, ")\n");
if (is.na(asp.ratio)) stop("native.slope: internal error, I do not
have a reasonable drawing aspect ratio");
net.slope= d/asp.ratio/d.m;
return(slope = atan(net.slope)/pi*180.0 )
}
# some test code
x<- seq(-10,20,by=0.1)
y<- x*x;
plot( x, y, type="l" );
display= ((1:length(y))%%40 == 0)
for (i in 1:(length(y))) {
if (display[i]) {
points(x[i],y[i], pch=19);
srt= native.slope( x, y, i );
text( x[i], y[i], paste(i,"=",x[i],"=",srt), srt=srt, cex=0.9 );
}
}
On 2/4/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote:
> On 2/4/2006 3:50 PM, ivo welch wrote:
> > [resent, plus small addition; I do not understand why gmail sent a
> > weird charset.]
> >
> > Dear R wizards:
> >
> > I would love to write a general function that matches the slope of a plotted
> > line in an xy-plot at a particular x,y location. something like
> >
> >x<- (1:10)^2; y<- 40:50;
> >plot( x,y, type="l", xlim=c(0,90) )
> >srt.at5 = text.at.current.plot.with.slope( x, y, 5);
> >text( x[5],y[5], pos=3, srt=srt.at.5);
> >
> > to do this, I first need to compute the function slope around x[5], which is
> > an easy task. alas, the harder task is that I need to scale this by the
> > plot aspect ratio and the axes. How can a function read this from the
> > current plot?
>
> I haven't done this, but you can presumably work it out from the
> conversions implied by the "fig", "fin", "plt", and/or "usr" values.
> >
> > (Has someone written such a function, perhaps more embellished, to save me
> > the debugging effort?)
> >
> > Or, is there an alternative to srt, which slopes the text relative to the
> > existing scale?
> >
> > *** come to think of it, what I would really like is the ability of
> > text to 'snake' itself along the line itself. I doubt that this is
> > easily possible, but I just wanted to ask.
>
> Using strsplit and strwidth you should be able to do it, but it will
> probably look quite ugly.
>
> Duncan Murdoch
>
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] srt --- slope text with function?
On 2/4/2006 3:50 PM, ivo welch wrote: > [resent, plus small addition; I do not understand why gmail sent a > weird charset.] > > Dear R wizards: > > I would love to write a general function that matches the slope of a plotted > line in an xy-plot at a particular x,y location. something like > >x<- (1:10)^2; y<- 40:50; >plot( x,y, type="l", xlim=c(0,90) ) >srt.at5 = text.at.current.plot.with.slope( x, y, 5); >text( x[5],y[5], pos=3, srt=srt.at.5); > > to do this, I first need to compute the function slope around x[5], which is > an easy task. alas, the harder task is that I need to scale this by the > plot aspect ratio and the axes. How can a function read this from the > current plot? I haven't done this, but you can presumably work it out from the conversions implied by the "fig", "fin", "plt", and/or "usr" values. > > (Has someone written such a function, perhaps more embellished, to save me > the debugging effort?) > > Or, is there an alternative to srt, which slopes the text relative to the > existing scale? > > *** come to think of it, what I would really like is the ability of > text to 'snake' itself along the line itself. I doubt that this is > easily possible, but I just wanted to ask. Using strsplit and strwidth you should be able to do it, but it will probably look quite ugly. Duncan Murdoch __ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
