similar to: Question about substitute (and eval, parse and deparse)

Displaying 20 results from an estimated 20000 matches similar to: "Question about substitute (and eval, parse and deparse)"

2002 Jan 06
1
How to get setGeneric() to work?
Dear all, I can't see how setGeneric() works. I know perfectly well how to define library(methods); setClass("Square", representation(side="numeric")); # Will become the default method. getArea <- function(object) { stop(paste("Method getArea() is not defined for this class:", data.class(object))); } setMethod("getArea",
2002 Jan 09
0
methods: Extending classes
Hi (again), let's say I want to create a class Matrix that extends the class matrix, but I can't get it to work. I define the class as > setClass("Matrix", "matrix") > getClass("Matrix") No Slots, prototype of class "matrix" Extends: Class "matrix" directly. Class "structure" by class "matrix".
2002 Jan 24
3
Best way to check/assert a certain version of or a package
When loading a package with library(APkg) or require(APkg) I would like to make sure that (1) the correct version of [R] is installed. If not an informative error message should be given. I would also like to make sure that (2) another required package which is loaded from within the APkg package (by require(OtherPkg)) is of a certain version or later. First of all, I believe that the check
2001 Nov 16
0
Best/safest way to use UseMethod()?
I raised the questions below a couple of months ago by sending it to the developers mailing list, but I don't know if it got through since no one replied to it. Looking at the [R] distribution, the UseMethod() is used in the following way: alias <- function(object, ...) UseMethod("alias") or barplot <- function(height, ...) UseMethod("barplot") What I
2001 Sep 10
3
Offline change detection methods?
Dear [R] people, I am looking for a method to test if there are any changes (jump points) in a time series data set or not. If there are, I would like to identify the jump points. All the data is available from the beginning so an offline method could be used. The current data set that I have could be seen as a time series with five almost equally sized segments that stands out to have different
2010 May 14
1
debugging substitute function in R 2.11
Dear list, A while ago I found in the web a function (sadly I can't credit the author as I don't remember where I picked it up from) whose output is a dataframe that lists for every object in the global environment its class, mode, dim & length (where applicable). It is meant to be an upgrade to the ls () function with a far more informative and detailed output and it quickly became
2002 Feb 06
4
Weighted median
Is there a weighted median function out there similar to weighted.mean() but for medians? If not, I'll try implement or port it myself. The need for a weighted median came from the following optimization problem: x* = arg_x min (a|x| + sum_{k=1}^n |x - b_k|) where a : is a *positive* real scalar x : is a real scalar n : is an integer b_k: are negative and positive scalars
2016 Mar 13
1
formals(x)<- drops attributes including class
Just checking in to see whether it is intended or not that assigning new formals to a function/closure causes any attributes to be dropped: EXAMPLE: > fcn <- structure(function() {}, foo="foo", class=c("foo")) > str(fcn) function () - attr(*, "srcref")=Class 'srcref' atomic [1:8] 1 18 1 30 18 30 1 1 .. ..- attr(*, "srcfile")=Classes
2003 Oct 30
2
'nls' and its arguments
Dear R experts! I'd to fit data by 'nls' with me-supplied function 'fcn'. 1) I'd like 'fcn' to accept arbitrary arguments, i.e. I defined it as f(...) {<body>}. (Ok, that's not actually impotant). 2) Second, I would NOT like to supply every parameter in the formula. To illustrate this, let's look at the last example of 'nls' help
2006 Apr 05
1
predict.smooth.spline.fit and Recall() (Was: Re: Return function from function and Recall())
Hi, forget about the below details. It is not related to the fact that the function is returned from a function. Sorry about that. I've been troubleshooting soo much I've been shoting over the target. Here is a much smaller reproducible example: x <- 1:10 y <- 1:10 + rnorm(length(x)) sp <- smooth.spline(x=x, y=y) ypred <- predict(sp$fit, x) # [1] 2.325181 2.756166 ...
2005 Aug 03
1
deparse(substitute(x)) and S3 methods
Dear List, I have the following function: coca <- function(x, ...) { if(is.null(class(x))) class(x) <- data.class(x) UseMethod("coca", x) } and a default method coca.default <- function(x, y, method = c("predictive", "symmetric"), reg.method = c("simpls", "eigen"), weights = NULL,
2004 Apr 18
1
deparse(substitute(arg)) in C?
In R code, "deparse(substitute(arg))" is the usual way to construct a label for an argument. Is there an equivalent to this in C code working on an SEXP, inside a function called by .Internal()? Duncan Murdoch
2006 Apr 04
2
Return function from function with minimal environment
Hi, this relates to the question "How to set a former environment?" asked yesterday. What is the best way to to return a function with a minimal environment from a function? Here is a dummy example: foo <- function(huge) { scale <- mean(huge) function(x) { scale * x } } fcn <- foo(1:10e5) The problem with this approach is that the environment of 'fcn' does not
2007 Aug 22
3
integrate
Hi, I am trying to integrate a function which is approximately constant over the range of the integration. The function is as follows: > my.fcn = function(mu){ + m = 1000 + z = 0 + z.mse = 0 + for(i in 1:m){ + z[i] = rnorm(1, mu, 1) + z.mse = z.mse + (z[i] - mu)^2 + } + return(z.mse/m) + } > my.fcn(-10) [1] 1.021711 > my.fcn(10) [1] 0.9995235 > my.fcn(-5) [1] 1.012727 > my.fcn(5)
2010 Jun 09
2
OOP and passing by value
Greetings, I love the R system and am sincerely grateful for the great effort the product and contributors are delivering. My question is as follows: I am trying to use S4 style classes but cannot write functions that modify an object because paramter passing is by value. For example I want to do this: setGeneric("setData", function(this,fcn,k){ standardGeneric("setData")
2002 Mar 07
2
Is it possible to open a specific file in the HTML browser like help(htmlhelp=TRUE) does?
Dear all/anyone, after generating a HTML file "result.html" from within [R], I would like to open the default HTML browser (like help.start() does) to show the file. Is it possible to do this (without trying to do a system dependent search for possible browsers)? I browsed the source of package 'base' (R v1.4.1 WinMe) and I found the following in help():
2002 Jun 27
2
Fastest way to find the last index k such that x[k] < y in a sorted vector x?
Hi, I am trying to find the fastest way to "find the last index k such that x[k] < y in a *sorted* vector x" These are my two alternatives: x <- sort(rnorm(1e4)) y <- 0.2 # Alt 1 k <- max(1, sum(x < y)) # Alt 2 "divide and conquer" lastIndexLessThan <- function(x, y) { k0 <- 1; k1 <- length(x) while ((dk <- (k1 - k0)) >
2002 Feb 26
1
abs() on data frames?!
I might be wrong, but didn't the following work before R v1.4.x? R --vanilla > df <- data.frame(-10:10) > abs(df) Error in abs(df) : non-numeric argument to function I have tried this on both Rv1.4.1 on WinMe and Rv1.4.0 on Solaris 8. I regexp searched bugs.r-project.org on "abs" but I found nothing about this. I can't see why abs(df) shouldn't work when sqrt(df),
2002 Feb 26
1
abs() on data frames?!
I might be wrong, but didn't the following work before R v1.4.x? R --vanilla > df <- data.frame(-10:10) > abs(df) Error in abs(df) : non-numeric argument to function I have tried this on both Rv1.4.1 on WinMe and Rv1.4.0 on Solaris 8. I regexp searched bugs.r-project.org on "abs" but I found nothing about this. I can't see why abs(df) shouldn't work when sqrt(df),
2010 Sep 15
1
retrieving object names passed indirectly to a function
Hi folks, I'm stuck with a problem that I suspect has a trivial solution... I have a function, call it foo, that takes a number of arguments which it uses as parameters for a simulation. For later analysis, foo stores the names of the objects passed to it along with the simulation results in its output (written to a database). The objects names are accessed with deparse(substitute(argname)).