[Rd] Small typo in Sweave.Rnw

2023-12-11 Thread Enrico Schumann
In the first paragraph of Sweave.Rnw
(./src/library/utils/vignettes/Sweave.Rnw), it reads

  for literate programming \cite{fla:Knuth:1984}.

but probably should be

  for literate programming \citep{fla:Knuth:1984}.
^

kind regards
Enrico

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-04 Thread Enrico Schumann
On Thu, 03 Aug 2023, Sami Tuomivaara writes:

> Dear all,
>
> I have used optim a lot in contexts where it would
> useful to be able to iterate function myfun that, in
> addition to the primary objective to be minimized
> ('minimize.me'), could return other values such as
> alternative metrics of the minimization, informative
> intermediate values from the calculations, etc.
>
> myfun  <- function()
> {
> ...
> return(list(minimize.me = minimize.me, R2 = R2, pval = pval, etc.))
> }
>
> During the iteration, optim could utilize just the first value from the myfun 
> return list; all the other values calculated and returned by myfun could be 
> ignored by optim.
> After convergence, the other return values of myfun
> could be finally extracted and appended into the optim
> return value (which is a list) as additional entry
> e.g.: $aux <- list(R2, pval, etc.), (without
> 'minimize.me' as it is already returned as $value).
>
> The usual ways for accessing optim return values, e.g.,
> $par, $value, etc. are not affected.  Computational
> cost may not be prohibitive either.  Is this feasible
> to consider?
>

If you only wish to store additional information, you could do
so with an environment, without changing optim.  For instance,
like so (using the first example from ?optim):

data <- new.env()
data$i <- 0
data$fun.value <- numeric(1000)

fr <- function(x, data) {   ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
ans <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
data$i <- data$i + 1
data$fun.value[data$i] <- ans
ans
}
optim(c(-1.2,1), fr, data = data)
## $par
## [1] 1.000260 1.000506
## 
## $value
## [1] 8.825241e-08
## 
## $counts
    ## function gradient 
##  195   NA 
## 
## 

data$i
## 195

plot(data$fun.value[1:data$i])




-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] order of operations

2021-08-27 Thread Enrico Schumann
On Fri, 27 Aug 2021, Gabor Grothendieck writes:

> Are there any guarantees of whether x will equal 1 or 2 after this is run?
>
> (x <- 1) * (x <- 2)
> ## [1] 2
> x
> ## [1] 2

At least the "R Language Definition" [1] says

  "The exponentiation operator ‘^’ and the left
   assignment plus minus operators ‘<- - = <<-’
   group right to left, all other operators group
   left to right.  That is  [...]  1 - 1 - 1 is -1"

which would imply 2.

[1] 
https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Infix-and-prefix-operators

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Producing different text formats in R

2019-08-08 Thread Enrico Schumann
>>>>> "Lluís" ==writes:

Lluís> Dear all,
Lluís> I am facing a strange problem with text files formats.

Lluís> I am currently using a C++ code named voro++ 
(http://math.lbl.gov/voro++/). This code accepts text files with four columns:

Lluís>

Lluís> where id is an identification number and x,y,z are the coordinates 
of a point in a 3D space.

Lluís> The input file, name it myfile_cpp.txt, is generated by another C++ 
code written by myself (named quadscheme.cpp). So far, these calculations 
involve no R code and it works just fine.

Lluís> However, now I am trying to replace my C++ code quadscheme.cpp by a 
R function. The four columns (id,x,y,z) are produced in a matrix or Data Frame 
and then saved in a file myfile_r.txt using write.table(). For example using 
the following function:

Lluís> quadscheme <- function(window, ntile) {
Lluís>   # Creates a grid of points in a 3D orthohedron. 
Lluís>   # First point lies at the (mix,miny,miz) corner of the volume and 
the last one at (maxx,maxy,maxz)
Lluís>   # window: vector. Contains the minimum and maximum values of a 3D 
orthohedron
Lluís>   #   window <- c(minx, maxx, miny, maxy, minz, maxz)
Lluís>   # ntile: vector. Number of points per dimension minus one. We 
manually add a extra row of points per dimension
Lluís>   #   ntile <- c(nstepx, nstepy, nstepz)
Lluís>   M <- prod(ntile+1) 
Lluís>   mat <- matrix(NA,M,4)
Lluís>   mat[,1] <- 1:M # id column
  
Lluís>   # step length per dimension
Lluís>   hi <- (window[2] - window[1])/ntile[1]
Lluís>   hj <- (window[4] - window[3])/ntile[2]
Lluís>   hk <- (window[6] - window[5])/ntile[3]
  
Lluís>   c <- 1
Lluís>   for(i in 0:ntile[1]) {
Lluís> x <- i*hi + window[1]
Lluís> for(j in 0:ntile[2]) {
Lluís>   y <- hj*j + window[3]
Lluís>   for(k in 0:ntile[3]) {
Lluís> z <- k*hk + window[5]
Lluís> mat[c,2:4] <- c(x,y,z) # Add a new point to the grid
Lluís> c <- c + 1
Lluís>   }
Lluís> }
Lluís>   }
Lluís>   write.table(mat, file="myfile_r.txt", row.names=FALSE, 
col.names=FALSE)
Lluís> }

Lluís> And then calling:

>> window <- c(18, 43, 171, 196, 95, 102)
>> ntile <- c(100,100,28)
>> quadscheme(window, ntile)

Lluís> I see no difference between both myfile_*.txt files,
Lluís> one is created with C++ and the other one with
Lluís> R. However, function voro++ do not accept the later one
Lluís> (created with R and saved with write.table). I've also
Lluís> noted that voro++ usually accepts R generated files
Lluís> when they are small enough, but it accepts all C++
Lluís> generated files, regardless the size.

Lluís> I know this is more a C++ than a R question, but may be
Lluís> you can help me as well. I suspect that even if both
Lluís> files are *.txt they have some differences that I
Lluís> cannot see. Is there any way in R to produce different
Lluís> kinds of txt files? Like binary instead of text files?
Lluís> Could this be the problem?

Lluís> I attach two identical files, one produced by C++
Lluís> (myfile_cpp.txt) and another one by the previous R
Lluís> function (myfile_r.txt). I attach as well the C++ code
Lluís> used to generate myfile_cpp.txt.

Lluís> Thank you in advance,

Lluís> Lluís Hurtado-Gil

In your R file, scientific notation is used:

R:
  9 26.5 174.5 96.5
  1e+05 26.5 174.5 96.75
  11 26.5 174.5 97

cpp:
  9 26.5 174.5 96.5
  10 26.5 174.5 96.75
  11 26.5 174.5 97

Try setting options(scipen = 20) before you write the
file in R.



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] install packages with missing pkg argument

2019-07-29 Thread Enrico Schumann
Maybe related to this?

https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17556

>>>>> "Antoine" == Ant F  writes:

Antoine> Dear all,
Antoine> The help for `?install.packages` decribes, in the `pkg` argument
Antoine> description :

>> If this is missing, a listbox of available packages is presented where
Antoine> possible in an interactive R session.

Antoine> In fact running it with a missing argument triggers an error :

Antoine> install.packages()
>> Error in install.packages : argument "pkgs" is missing, with no default

Antoine> What however doesn't trigger an error is callinginstall.packages 
on a zero
Antoine> length character :

Antoine> install.packages(character(0))

Antoine> On my colleague's R 3.5.1 windows installation it shows the 
listbox of
Antoine> available packages, on my 3.6.0 installation it pauses for a 
couple seconds
Antoine> and doesn't do anything.

Antoine> A character vector of length zero is what you get when you compute 
an empty
Antoine> `setdiff` or `intersection`, so it was very surprising to us to see
Antoine> something popup where we were expecting a vector of missing 
packages to be
Antoine> installed (or none if there was no missing package).

Antoine> I believe having the function work as advertised with a proper 
missing
Antoine> argument, and having it do nothing silently when the argument is 
of length
Antoine> zero, would make more sense.

Antoine> Best regards,

Antoine> Antoine


-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] A trap for young players with the lapply() function.

2017-03-29 Thread Enrico Schumann
(inline)

On Tue, 28 Mar 2017, Rolf Turner writes:

> On 28/03/17 04:21, Barry Rowlingson wrote:
>> On Mon, Mar 27, 2017 at 1:17 AM, Rolf Turner  wrote:
>>>
>>> Is there any way to trap/detect the use of an optional argument called
>>> "X" and thereby issue a more perspicuous error message?
>>>
>>> This would be helpful to those users who, like myself, are bears of very
>>> little brain.
>>>
>>> Failing that (it does look impossible)
>>
>> You can get the names of named arguments:
>>
>>  > z = function(x,X){cos(x*X)}
>>  > names(formals(z))
>>  [1] "x" "X"
>
> That doesn't seem to help.  I tried putting a browser inside lapply()
> and looked at formals(FUN).  This gave NULL, because FUN has already
> been taken to be the list argument "x" to which lapply() is being
> applied.

You can get the call, without the arguments being
matched, with `sys.call`. In your call of lapply,
saying `sys.call()` before anything else would give
you


lapply(y, function(x, X) {
cos(x * X)
}, X = 2 * pi)

which would allow you to get at the argument names of
your function.

if ("X" %in% names(sys.call()[[3L]][[2L]]))
   warnings("found 'X'")

But more would be needed: you need to figure out which
argument you actually meant to be FUN. (In the above,
I simply assumed it was the second passed argument.)
That is, you would need to figure out which passed
argument is a function, which is not safe either,
since ... may also contain functions.


>>> might it not be a good idea to
>>> add a warning to the help for lapply(), to the effect that if FUN has an
>>> optional argument named "X" then passing this argument via "..." will
>>> cause this argument to be taken as the first argument to lapply() and
>>> thereby induce an error?
>>
>> Another idea might be to use purrr:map instead, which is quite happy
>> with X in your function:
>>
>>  >  xxx <- purrr::map(y,function(x,X){cos(x*X)},X=2*pi)
>>  > xxx
>> [[1]]
>> [1] 0.08419541
>>
>> [[2]]
>> [1] 0.6346404
>>
>> [[3]]
>> [1] 0.9800506
>>
>> [[4]]
>> [1] 0.8686734
>>
>> [[5]]
>> [1] -0.9220073
>>
>> But don't feed `.x` to your puing cats, or fails silently:
>>
>>  >  xxx <- purrr::map(y,function(x,.x){cos(x*.x)},.x=2*pi)
>>  > xxx
>> [[1]]
>> NULL
>>
>> But who would have a function with `.x` as an argument?
>
> Indeed.  It struck me that a possible workaround would be to change
> the name of the first argument of lapply() from "X" to ".X".  No-one
> would have a function with an argument names ".X" --- at least I
> wouldn't, so this would solve the problem for me.
>
> It seems to me that this change could be made without breaking anything.
> But perhaps I am engaging in my usual PollyAnna-ish optimism! :-)
>
> cheers,
>
> Rolf

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Problem with build and check

2014-11-12 Thread Enrico Schumann
On Wed, 12 Nov 2014, "Therneau, Terry M., Ph.D."  writes:

> I am getting failure of build and check, for an Rd file that has a long 
> argument list.
> Guess diagnosis: a quoted string beyond a certain point in the argument list 
> is fatal.

Another guess: should you not escape the '%'?  That is, write
"\%Y-\%m-\%d"?  [Untested.]

> Example:  Use the function below, create an Rd file for it with
> prompt().  Move the .Rd file to the man directory (no need to edit it)
> and try building
>
> dart.control <- function(server=c("production", "integration", "development",
>   "http"),
>  out.poll.duration = 5,
>  out.poll.increase = 1.1,
>  out.poll.max = 30,
>  out.poll.timeout = 3600,
>  netrc.path,
>  netrc.server = "ldap",
>  rtype = c("xml", "json"),
>  dateformat= "%Y-%m-%d") {
>
> server <- match.arg(server)
> server
> }
>
> I created a package "dummy" with only this function, and get the following on 
> my Linux box.
>
> tmt-local2021% R CMD build dummy
> * checking for file ‘dummy/DESCRIPTION’ ... OK
> * preparing ‘dummy’:
> * checking DESCRIPTION meta-information ... OK
> Warning: newline within quoted string at dart.control.Rd:11
> Warning:
> /tmp/RtmpjPjz9V/Rbuild398d6e382572/dummy/man/dart.control.Rd:46:
> unexpected section header '\value'
> Warning: newline within quoted string at dart.control.Rd:11
> Error in 
> parse_Rd("/tmp/RtmpjPjz9V/Rbuild398d6e382572/dummy/man/dart.control.Rd", :
>   Unexpected end of input (in " quoted string opened at dart.control.Rd:88:16)
> Execution halted
>
> Session info for my version
>> sessionInfo()
> R Under development (unstable) (2014-10-30 r66907)
> Platform: i686-pc-linux-gnu (32-bit)
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
>  [3] LC_TIME=en_US.UTF-8LC_COLLATE=C
>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>  [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
>  [9] LC_ADDRESS=C   LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods base
>
>
> Terry T.
>


-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] minor typo in docs for 'sort'

2013-05-23 Thread Enrico Schumann
Dear all, 

on the help page for '?sort':


  'Method "shell" uses Shellsort ([...] from Sedgewick (1996))'


but in the references it is Sedgewick (1986). 1986 seems correct:

http://dx.doi.org/10.1016/0196-6774(86)90001-5


Thank you,
Enrico

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel