[R] "use of NULL environment is defunct" when trying to lock a reference class

2018-08-27 Thread Ivan Krylov
Hi!

I'm trying to create a persistent memoising class with a destructor and
an option to evaluate cache misses in parallel. I want to lock all
its fields because it doesn't make sense to change them after the
filename, the environment object and the function are set in the
object. (I'm not sure whether I should lock the environment field I use
as the hash, though.)

The problem is, I can't figure out how to use class$lock(...) properly:

> test.class <- setRefClass("test", fields=c("field"))
> test.class$lock("field")
Error in .makeDefaultBinding(current@field, current@className, TRUE, 
environment(current)) :
  use of NULL environment is defunct

By looking at traceback() and source of methods:::.lockRefFields,
I see that environment(zzz@generator$def@fieldPrototypes[["field"]])
is, indeed, NULL.

I'm using R version 3.3.3 (2017-03-06),  version$`svn rev` is "72310".

What am I doing wring?

-- 
Best regards,
Ivan

P.S. Please Cc me in your replies to the list, if possible.

__
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] "use of NULL environment is defunct" when trying to lock a reference class

2018-08-28 Thread Ivan Krylov
Hi Eric,

Thank you for your answer!

On Mon, 27 Aug 2018 21:48:50 +0300
Eric Berger  wrote:

> you might want to consider the more recent R6 package

Indeed, R6 has private fields which fits my idea of an object with
mutable state even better.

My original problem seems to be solved and I'm posting my code (CC0) in
case someone else needs it as a reference:

require(digest); require(R6)
memoised <- R6Class(
"memoised", lock_objects=T, lock_class=T, cloneable=F,
private=list(fun=NULL, storage=NULL, cache=NULL),
public=list(
initialize=function(fun, storage) { # constructor
private$fun <- fun
private$storage <- storage
private$cache <- tryCatch(
{
load(storage)
cache
}, error = function(e) {
new.env(T, emptyenv())
}
)
},
eval=function(...) { # behave as cached fun
hash <- digest(list(...), algo="sha1")
if (exists(hash, private$cache)) return(get(hash, 
private$cache))
val <- private$fun(...)
assign(hash, val, private$cache)
val
},
par.eval=function(args, cl) { # args is list of argument lists
# hash all because we'll need them later
hashes <- lapply(args, digest, algo="sha1")
# indices of not yet evaluated in the original args 
array
missing <- Filter(function(i) !exists(hashes[[i]], 
private$cache), seq_along(args))
# evaluate and save them
values <- parLapply(cl, args[missing], function(l) 
do.call(private$fun,l))
private$cache[hashes[missing]] <- values
# get all requested hashes
private$cache[hashes]
},
finalize=function() { # destructor
cache <- private$cache # must have known name for 
restore
save(cache, file=private$storage)
}
)
)

It's still a mystery why did setRefClass refuse to lock my class, but
at least it's not blocking my progress.

-- 
Best regards,
Ivan

__
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] histogram in GNU R....

2018-09-07 Thread Ivan Krylov
On Fri, 7 Sep 2018 08:26:22 +
akshay kulkarni  wrote:

> when I type hist(xht), it goes to the next prompt. More importantly,
> there is no error message. So, the most probable conclusion is that
> the command gets executed

Yes, hist() returns its value invisibly (try typing "? invisible" in
the R prompt without the quotes), which means that you don't see it, but
you can assign it to a variable and then view as usual:

> xht <- c(1,2,3,4,5,6,7,8,9,10)
> hxt <- hist(xht)
> hxt

You can also use the following trick:

> (hist(xht))

to see the invisible returned value without assigning it to a temporary
variable.

> But there is no pop up screen with a histogram, and nothing else...

As to why you cannot see a plot, it depends a lot on your setup. For
example, how exactly do you connect to the R instance running at AWS?
If you use plain SSH from your own Linux machine, try `ssh -X` to allow
the remote server to connect to the X graphics system on your machine
and display windows (alas, it gets very slow). What does `dev.cur()`
show after you run `hist(xht)`? On my machine, when I start R with no
available X connection, it automatically switches to the
non-interactive `pdf` graphics device; all plots get redirected to the
`Rplots.pdf` file in the current directory. Perhaps you can download
that file from the EC2 instance and view it locally?

-- 
Best regards,
Ivan

__
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] getting 21 very different colours

2018-09-11 Thread Ivan Krylov
On Tue, 11 Sep 2018 07:34:51 +
Federico Calboli  wrote:

> Is there a way of getting a set of 21 colours that maximises the
> differences between them?  

In my limited experience, getting even 10 colours to look different
enough is a serious undertaking. Take a look at RColorBrewer:
display.brewer.all(n, "qual") stops offering palettes for n>12.

When I needed a 10-colour categorical/qualitative palette, I opted for
brute force approach of maximising the minimal distance between points
in HCL colourspace, although later my colleague told me that I just
needed an existing algorithm to place the points uniformly. It has to
be HCL and not RGB because HCL signifies the way people perceive
different colours while RGB is only a good representation hardware-wise.

Here is my code; the usual disclaimers about stuff written between 1 and
3 AM apply:

# -8<---

require(nloptr)

h <- c(0,360)
c <- c(0,137) # see the warning about fixup in `?hcl`: not all HCL points are 
representable in RGB

# NOTE: depending on your plot background, you may have to change at least 
luminance range
l <- c(30,90)

npoints <- 24 # I had only 10 here

pts <- matrix(ncol=3, nrow=npoints, dimnames=list(NULL, c("h","c","l")))
pts[,"h"] <- runif(npoints, min=h[1], max=h[2])
pts[,"c"] <- runif(npoints, min=c[1], max=c[2])
pts[,"l"] <- runif(npoints, min=l[1], max=l[2])

lb <- cbind(h=rep(h[1],npoints), c=rep(c[1],npoints), l=rep(l[1],npoints))
ub <- cbind(h=rep(h[2],npoints), c=rep(c[2],npoints), l=rep(l[2],npoints))

obj <- function(x) {
pts[,c("h","c","l")] <- x
# somehow the best results were achieved by calculating Euclidean 
distance from cylindrical coordinates
pts <- cbind(pts[,"c"]*sin(pts[,'h']/360*2*pi), 
pts[,'c']*cos(pts[,'h']/360*2*pi), pts[,'l'])
d <- as.matrix(dist(pts))
diag(d) <- NA
# maximise minimal distance <=> minimize negative of minimal distance
-min(d, na.rm=T)
}

# the stopping criterion is a bit lame, but the objective function here is very 
hard to minimize
# 1e6 iterations take a few minutes on a relatively modern desktop
sol <- nloptr(as.vector(pts), obj, lb=as.vector(lb), ub=as.vector(ub), 
opts=list(algorithm="NLOPT_GN_CRS2_LM", maxeval=1e6))

pts[,c("h","c",'l')] <- sol$solution

plot(pts[,"c"] * sin(pts[,"h"]/360*2*pi), pts[,"c"] * cos(pts[,"h"]/360*2*pi), 
col=hcl(pts[,"h"], pts[,"c"], l), pch=19, cex=2)

# -8<---

I couldn't get my code to produce 24 acceptably different colours, but
maybe you will succeed with a similar approach.

-- 
Best regards,
Ivan

__
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] sink() output to another directory

2018-09-14 Thread Ivan Krylov
В Thu, 13 Sep 2018 15:49:52 -0700 (PDT)
Rich Shepard  пишет:

> sink('stat-summaries/estacada-wnw-precip.txt')
> print(/summary(estacada_wnw_wx))
> sink()

Just remove the slash from your print command (line 25 of
rainfall-dubois-crk-all.r) because it's a syntax error (must be a typo).
I.e. the above should be print(summary(estacada_wnw_wx)), not
print(/summary(estacada_wnw_wx)) (do you notice the difference?). The
rest of your sequence of commands is fine.

-- 
Best regards,
Ivan

__
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] Help with setting locale

2018-09-16 Thread Ivan Krylov
On Fri, 14 Sep 2018 10:02:01 +0200
Kim Titcombe  wrote:

> I am based in Switzerland but work in English (Windows in English),
> hence want English as default.

Which Windows version do you use? Which languages/language packs do you
have installed?

-- 
Best regards,
Ivan

__
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] Help with setting locale

2018-09-17 Thread Ivan Krylov
On Sun, 16 Sep 2018 21:18:45 +0200
Kim Titcombe  wrote:

> I have Windows 10. English version.

Do you have any other problems, besides the warning message at startup?

According to MSDN[1], the combination of English language
and Swiss cultural rules should be supported in Windows 10 >=
v1607 with a call like Sys.setlocale(locale="English_Switzerland"). If
that doesn't work, Sys.setlocale(locale="English") seemed to work even
on my Windows 2008 machine[2]. However, for some reason, when I call
setlocale() with two-letter arguments exactly as described on that page
("en-US", "en-CH", etc) the call fails.

-- 
Best regards,
Ivan

[1]: https://msdn.microsoft.com/library/cc233982.aspx

[2]: Also worked:

> Sys.setlocale(locale="English_United States")
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
> Sys.setlocale(locale="English_United Kingdom")
[1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United 
Kingdom.1252;LC_MONETARY=English_United 
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"

__
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] regexp mystery

2018-10-16 Thread Ivan Krylov
On Tue, 16 Oct 2018 08:36:27 +
PIKAL Petr  wrote:

> > dput(x[11])  
> "et odYezko: 3 \fas odYezku:   15 s"

> gsub("^.*: (\\d+).*$", "\\1", x[11])
> works for 3

This regular expression only matches one space between the colon and
the number, but you have more than one of them before "15".

> gsub("^.*[^:] (\\d+).*$", "\\1", x[11])
> works for 15

Match succeeds because a space is not a colon:

 ^.* matches "et odYezko: 3 \fas odYezku:  "
 [^:] matches space " "
 space " " matches another space " "
 finally, (\\d+) matches "15"
 and .*$ matches " s"

If you need just the numbers, you might have more success by extracting
matches directly with gregexpr and regmatches:

(
function(s) regmatches(
s,
gregexpr("\\d+(\\.\\d+)?", s)
)
)("et odYezko: 3 \fas odYezku:   15 s")

[[1]]
[1] "3"  "15"

(I'm creating an anonymous function and evaluating it immediately
because I need to pass the same string to both gregexpr and regmatches.)

If you need to capture numbers appearing in a specific context, a
better regular expression suiting your needs might be

":\\s*(\\d+(?:\\.\\d+)?)"

(A colon, followed by optional whitespace, followed by a number to
capture, consisting of decimals followed by optional, non-captured dot
followed by decimals)

but I couldn't find a way to extract captures from repeated match by
using vanilla R pattern matching (it's either regexec which returns
captures for the first match or gregexpr which returns all matches but
without the captures). If you can load the stringr package, it's very
easy, though:

str_match_all(
c(
"PYedehYev:  300 s  Záva~í: 2.160 kg",
"et odYezko: 3   \fas odYezku:   15 s"
),
":\\s*(\\d+(?:\\.\\d+)?)"
)
[[1]]
 [,1]  [,2]   
[1,] ":  300"  "300"  
[2,] ": 2.160" "2.160"

[[2]]
 [,1] [,2]
[1,] ": 3""3" 
[2,] ":   15" "15"

Column 2 of each list item contains the requested captures.

-- 
Best regards,
Ivan

__
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] Cannot use R on Windows when installed to an external drive with a space in the path

2018-10-22 Thread Ivan Krylov
This is a bin\R.exe + bin\Rscript.exe bug.

On Windows, where the system call to create a process only takes one
command line string instead of an array of command line parameters[0]
(and the C runtimes usually call CommandLineToArgvW[1] behind the
scenes to get an array back), execv() does nothing to prepare a
properly-quoted command line and just concatenates its arguments[2].
Moreover, Windows version of execv() is effectively undocumented[3],
nor there is any information about the suggested replacement[4].
(You can call it a Windows bug instead.)

Short file names are not a guaranteed solution, because they may
not be available at all on some filesystems[5] (which is how this
error has been encountered). In the general case, "argument quoting
hell"[6] is unavoidable on Windows, but the code from [2] implements
the proper conversion from an array of strings to a single quoted
command line, ready to be parsed by the launched process.

char* getRHOME(int m) in src/gnuwin32/rhome.c tries to return a short
path (assuming it won't contain spaces). This is used by rcmdfn() from
src/gnuwin32/front-ends/rcmdfn.c to system() a command line built with
no quoting, causing the error. rcmdfn() is called from
src/gnuwin32/front-ends/R.c, which seems to contain the entry point for
R\R-3.3.3\bin\R.exe.

Fortunately, in src/unix/Rscript.c the whole mess seems to be avoided by
having it directly include rterm.c and then call AppMain() on Windows,
passing the command line parameter array. Same goes for
src/gnuwin32/front-ends/graphappmain.c. The corresponding executables
reside in the architecture-specific subdirectory of ...\bin\.

So if you run Rgui.exe, Rterm.exe or Rscript.exe from
R-3.3.3\bin\i386 instead of R-3.3.3\bin, no extra cmd.exe processes
should be spawned and the badly-formed system() should be avoided.

-- 
Best regards,
Ivan

[0]
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa

That's probably for historical reasons, because Win32 API wanted to
stay mostly source-compatible with Win16 API, and Win16 API may have
evolved from DOS system calls, where the command line was, indeed, a
single string pointer.

[1]
https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-commandlinetoargvw

[2]
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/

[3]
https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/ms235416(v=vs.110)

[4]
https://docs.microsoft.com/en-us/cpp/c-runtime-library/exec-wexec-functions?view=vs-2017

[5] https://blogs.msdn.microsoft.com/oldnewthing/20181004-00/?p=99895

[6] quoted from Rscript.c

__
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] Encoding issue

2018-11-05 Thread Ivan Krylov
On Mon, 5 Nov 2018 08:36:13 -0500 (EST)
Sebastien Bihorel  wrote:

> [1] "râs"

Interesting. This is what I get if I decode the bytes 72 e2 80 99 73 0a
as latin-1 instead of UTF-8. They look like there is only three
characters, but, actually, there is more:

$ perl -CSD -Mcharnames=:full -MEncode=decode \
 -E'for (split //, decode latin1 => pack "H*", "72e28099730a")
 { say ord, " ", $_, " ", charnames::viacode(ord) }'
114 r LATIN SMALL LETTER R
226 â LATIN SMALL LETTER A WITH CIRCUMFLEX
128  PADDING CHARACTER
153  SINGLE GRAPHIC CHARACTER INTRODUCER
115 s LATIN SMALL LETTER S
10 
 LINE FEED

Does it help if you explicitly specify the file encoding by passing
fileEncoding="UTF-8" argument to scan()?

-- 
Best regards,
Ivan

__
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] Help with if else branching print out

2018-12-18 Thread Ivan Krylov
On Tue, 18 Dec 2018 08:56:23 +
Andrew  wrote:

> How do I:
> 
> (a) reduce the gap between the reported number (i.e., 17, 9, 13) in
> each of the lines? and
> 
> (b) ensure that in the case of the second run using 9 as the input,
> the print is not over two lines?

Build a single string from your string parts. ?sprintf has already been
mentioned; another option is ?paste.

To prevent strings from being printed in quotes, use ?message.

-- 
Best regards,
Ivan

__
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] Problem with system() and source on linux

2018-12-20 Thread Ivan Krylov
On Thu, 20 Dec 2018 12:00:04 +0100
Agustin Lobo  wrote:

> For a given program, I need to set up its environment, which I
> normally do with source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile
> from the terminal.

The problem with this approach is that in Unix-like systems, child
processes cannot directly modify environment variables of their
parents (or any other processes besides those they are about to spawn).
In /bin/bash, `source` is a special builtin command that runs without
spawning child processes - therefore being able to modify the
environment of the *current* process.

> Now, when I try to do the same from within R, I get:
> 
> > system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile",
> > intern=TRUE)  
> sh: 1: source: not found

Moreover, `source` is a bash-ism and R launches the `system` commands
using /bin/sh, which might be different from /bin/bash. The
POSIX-correct way to include a batch file in the current shell session
is a dot: `. /home/alobo/OTB-6.6.0-Linux64/otbenv.profile`
http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#dot

Since this way you can only modify the environment of the temporary
shell process spawned by `system`, you have set up the environment and
launch the executable in a single `system` command:

system(". /home/alobo/OTB-6.6.0-Linux64/otbenv.profile; exec
otbcli_something")

-- 
Best regards,
Ivan

__
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] User Interfaces for R

2019-01-10 Thread Ivan Krylov
On Wed, 9 Jan 2019 14:29:22 -0500 (EST)
Bernard McGarvey  wrote:

> Can anyone point me to such an easy to use package to create GUIs for
> R?

If you want a traditional approach, a lot of R installations have
Tcl/Tk support built in (i.e. capabilities('tcltk') is TRUE). This
means that you can run library(tcltk) and call Tk functions to display
interactive windows almost everywhere.

This toolkit requires some knowledge of Tk [*] and may not look
particularly nice on some platforms, but is probably already included
in most R installations and has low resource footprint. There is a set
of slides [**] that might help you start using Tk, its widgets and
geometry managers.

R commander (Rcmdr, [***]) package has been developed using Tk.

The book Programming Graphical User Interfaces in R by Michael
Lawrence and John Verzani (ISBN 9781439856826) describes the major R
packages for GUI programming: RGtk2, qtbase, Tcl/Tk, and gWidgets.

-- 
Best regards,
Ivan

[*] https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm

[**]
https://uwaterloo.ca/statistics-and-actuarial-science/sites/ca.statistics-and-actuarial-science/files/uploads/files/rtcltk_tcl.pdf
https://uwaterloo.ca/statistics-and-actuarial-science/sites/ca.statistics-and-actuarial-science/files/uploads/files/rtcltk_geometry.pdf

[***] https://rcommander.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] User Interfaces for R

2019-01-11 Thread Ivan Krylov
On Thu, 10 Jan 2019 19:25:30 -0500
Duncan Murdoch  wrote:

> Eric's suggestion of Shiny is newer, and is a really well-designed 
> system.  A fairly steep learning curve, but worth it.

I understand the need for progress and I really like Shiny's API - it
feels very declarative and easy to write, and I don't care about the
presentation as long as it gets the job done - but all things have
their use cases, which is why I waited a bit before suggesting tcltk
and emphasised its low resource footprint and portability.

For example, an undergraduate student I work with has a laptop with
just 2 gigabytes of RAM. Now, of course there is a lot you can do in R
with that amount of memory - I had to make do with less until late 2016
- but the moment he launches a browser, it pages everything else out.
For him, using Shiny would be extremely slow if not impossible to use.
This is something browsers should be fixing and not the fault of Shiny,
but I can't ignore it either way. (And 2G is not the limit: the renderer
process of browser-based[*] Skype client has somehow managed to
allocate 5G of memory on the machine I'm typing this message on.)

Also, personally, I would prefer not to launch an asynchronous web
server and transfer large files from my computer back to my
computer (but into /tmp)[**] for something as simple as what could be
accomplished with tcltk::tk_choose.files().

But those issues (and things like requiring an Internet connection to
render TeX-style math[***]) aside, Shiny is really a fine and modern
user interface package, and should be preferred if you have the
resources.

Feel free to stop me if you think I'm engaging in off-topic here.

-- 
Best regards,
Ivan

[*] https://electronjs.org/apps/skype: "Electron accomplishes [building
cross-platform desktop applications with HTML, CSS, and JavaScript]
by combining Chromium and Node.js into a single runtime"

[**] https://shiny.rstudio.com/reference/shiny/latest/fileInput.html:
"The path to a temp file that contains the data that was uploaded."

[***] https://rdrr.io/cran/shiny/src/R/shinyui.R#sym-withMathJax: does
MathJax license require referencing https://mathjax.rstudio.com instead
of bundling it together with Shiny, like the rest of JavaScript
libraries are?

__
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] Potential R bug in identical

2019-01-17 Thread Ivan Krylov
On Thu, 17 Jan 2019 14:55:18 +
Layik Hama  wrote:

> There seems to be some weird and unidentifiable (to me) characters in
> front of the `Accidents_Index` column name there causing the length
> to be 17 rather than 14 characters.

Repeating the reproduction steps described at the linked pull request,

$ curl -o acc2017.zip
http://data.dft.gov.uk.s3.amazonaws.com/road-accidents-safety-data/dftRoadSafetyData_Accidents_2017.zip
$ unzip acc2017.zip
$ head -n 1 Acc.csv | hd | head -n 2
  ef bb bf 41 63 63 69 64  65 6e 74 5f 49 6e 64 65  |...Accident_Inde|
0010  78 2c 4c 6f 63 61 74 69  6f 6e 5f 45 61 73 74 69  |x,Location_Easti|

The document begins with a U+FEFF BYTE ORDER MARK, encoded in UTF-8.
Not sure which encoding R chooses on Windows by default, but
explicitly passing encoding="UTF-8" (or is it fileEncoding?) might
help decode it as such. (Sorry, cannot test my advice on Windows right
now.)

-- 
Best regards,
Ivan

__
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] Potential R bug in identical

2019-01-17 Thread Ivan Krylov
On Thu, 17 Jan 2019 21:05:07 +
Layik Hama  wrote:

> Why would `identical(str, "Accident_Index", ignore.case = TRUE)`
> behave differently on Linux/MacOS vs Windows?

Because str is different from "Accident_Index" on Windows: it was
decoded from bytes to characters according to different rules when file
was read.

Default encoding for files being read is specified by 'encoding'
options. On both Windows and Linux I get:

> options('encoding')
$encoding
[1] "native.enc"

For which ?file says (in section "Encoding"):

>> ‘""’ and ‘"native.enc"’ both mean the ‘native’ encoding, that is the
>> internal encoding of the current locale and hence no translation is
>> done.

Linux version of R has a UTF-8 locale (AFAIK, macOS does too) and
decodes the files as UTF-8 by default:

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

locale:
 [1] LC_CTYPE=ru_RU.utf8   LC_NUMERIC=C 
 [3] LC_TIME=ru_RU.utf8LC_COLLATE=ru_RU.utf8
 [5] LC_MONETARY=ru_RU.utf8LC_MESSAGES=ru_RU.utf8   
 [7] LC_PAPER=ru_RU.utf8   LC_NAME=C
 [9] LC_ADDRESS=C  LC_TELEPHONE=C   
[11] LC_MEASUREMENT=ru_RU.utf8 LC_IDENTIFICATION=C

While on Windows R uses a single-byte encoding dependent on the locale:

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Russian_Russia.1251  LC_CTYPE=Russian_Russia.1251   
[3] LC_MONETARY=Russian_Russia.1251 LC_NUMERIC=C   
[5] LC_TIME=Russian_Russia.1251

> readLines('test.txt')[1]
[1] "п»їAccident_Index"
> nchar(readLines('test.txt')[1])
[1] 17

R on Windows can be explicitly told to decode the file as UTF-8:

> nchar(readLines(file('test.txt',encoding='UTF-8'))[1])
[1] 15

The first character of the string is the invisible byte order mark.
Thankfully, there is an easy fix for that, too. ?file additionally
says:

>> As from R 3.0.0 the encoding ‘"UTF-8-BOM"’ is accepted for
>> reading and will remove a Byte Order Mark if present (which it
>> often is for files and webpages generated by Microsoft applications).

So this is how we get the 14-character column name we'd wanted:

> nchar(readLines(file('test.txt',encoding='UTF-8-BOM'))[1])
[1] 14

For our original task, this means:

> names(read.csv('Acc.csv'))[1] # might produce incorrect results
[1] "п.їAccident_Index"
> names(read.csv('Acc.csv', fileEncoding='UTF-8-BOM'))[1] # correct
[1] "Accident_Index"

-- 
Best regards,
Ivan

__
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] Function in default parameter value closing over variables defined later in the enclosing function

2019-01-23 Thread Ivan Krylov
Hi!

I needed to generalize a loss function being optimized inside another
function, so I made it a function argument with a default value. It
worked without problems, but later I noticed that the inner function,
despite being defined in the function arguments, somehow closes over a
variable belonging to the outer function, which is defined later.

Example:

outside <- function(inside = function() print(secret)) {
secret <- 'secret'
inside()
}
outside()

I'm used to languages that have both lambdas and variable declaration
(like perl5 -Mstrict or C++11), so I was a bit surprised.

Does this work because R looks up the variable by name late enough at
runtime for the `secret` variable to exist in the parent environment of
the `inside` function? Can I rely on it? Is this considered bad style? 
Should I rewrite it (and how)?

-- 
Best regards,
Ivan

__
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] Function in default parameter value closing over variables defined later in the enclosing function

2019-01-24 Thread Ivan Krylov
Dear Jan & Duncan,

Thanks for your replies!

On Wed, 23 Jan 2019 09:56:25 -0500
Duncan Murdoch  wrote:

> Defaults of variables are evaluated in the evaluation frame of the
> call. So the inside() function is created in the evaluation frame,
> and it's environment will be that frame.
 
> When it is called it will create a new evaluation frame (empty in
> your example), with a parent being its environment, i.e. the
> evaluation frame from when it was created, so it will be able to see
> your secret variable.

Nice explanation about closures in R inheriting not only their
explicitly captured variables, but whole environments of evaluation
(not stack) frames where they have been created.

> in my opinion it would be fine to write it as
> 
>   outside <- function(inside = defaultInsideFn) {
>  defaultInsideFn <- function() print(secret)
>  secret <- 'secret'
>  inside()
>   }

I like this idea; I'm going to use it.

-- 
Best regards,
Ivan

__
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] Function in default parameter value closing over variables defined later in the enclosing function

2019-01-26 Thread Ivan Krylov
On Thu, 24 Jan 2019 06:53:20 -0800
Jeff Newmiller  wrote:

> It would be better to also make secret an argument to outside instead
> of a local variable or to give up on supplying the inside function as
> an argument.

This was in a small, mostly self-contained one-off script that tested
different design of experiment approaches with simulated datasets.

Actually, I should move the "secret" variable to the global level,
together with other global settings like the dataset size and noise
level. There it's accessible to both any functions that might be
interested in it and the user who might want to change it, after all.

-- 
Best regards,
Ivan

__
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] load.wave

2019-02-02 Thread Ivan Krylov
Hello Nick Wray,

Let me offer a simplified explanation of what's going on. Sorry if it's
unnecessary.

Sound is waves of pressure in the air. Devices like microphones can
measure the changing pressure by converting it into voltage. Voltage
can then be sampled by an analog-to-digital converter inside a sound
card and stored as numbers in computer memory.

On Fri, 1 Feb 2019 10:20:57 + (GMT)
Nick Wray via R-help  wrote:

> What I am not sure about, and I can't find any clear explanation, is
> what these elements actually stand for?

Digital sound works by measuring "pressure" a few tens of thousands of
times per second and then recreating the corresponding signal
elsewhere. According to the sampling theorem, sound sampled N times per
second would be losslessly reproduced if it didn't contain frequencies
above N/2 Hz.

To reiterate, these numbers are just audio samples. Feed them to the
sound card at the original sample rate, and you hear the same sound
that had been recorded.

This part is explained well in two 30-minute video lectures here:
https://xiph.org/video/vid1.shtml https://xiph.org/video/vid2.shtml
(I wouldn't normally recommend video lectures, but these are really
good.)

> I would have thought that one needed as a minimum both volume and
> frequency ie a two dimensional vector but as far as I can tell there
> is only one single vector.

You are describing a spectrogram: a surface showing the "volume" of
each individual frequency in the sound recording, over time. How to get
it? If you run a Fourier transform over the original vector, you will
get only one vector showing the magnitudes and phases of all frequencies
through the whole length of the clip.

To get a two-dimensional spectrogram, you should take overlapping parts
of the original vector of samples, multiply them by a special window
function, then take a Fourier transform over that and combine
resulting vectors into a matrix. Computing a spectrogram involves
choosing a lot of parameters: size of the overlapping window, step
between overlapping windows, the window function itself and its own
parameters.

Problems like these should be described in books about digital signal
processing.

Jeff Newmiller sent more useful links while I was typing this, and I
guess I should posting off-topic.

-- 
Best regards,
Ivan

__
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] list with list function

2019-02-04 Thread Ivan Krylov
On Mon, 4 Feb 2019 21:01:06 + (UTC)
Andras Farkas via R-help  wrote:

> listA<-list(a,b,c)
> listB<-list(d,e,f)
> 
> what I would like to do with a function <...> as opposed to manually
> is to derive the following answer
> 
> listfinal<-list(a[-d],b[-e],c[-f])

The `Map` function, unlike `lapply`, iterates over its arguments
simultaneously:

Map(function(dt, idx) dt[-idx], listA, listB)
# [[1]]
# [1] "1999-07-31" "1999-06-12"
#
# [[2]]
# [1] "1999-03-10" "1999-04-04"
#
# [[3]]
# [1] "1999-08-07"


-- 
Best regards,
Ivan

__
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] Difficulty with "\\" in string functions....

2019-02-11 Thread Ivan Krylov
On Mon, 11 Feb 2019 15:01:16 -0500 (EST)
Bernard McGarvey  wrote:

> Now I try to split it using
> 
> 
> str_split(Fname1,"\\")
> 
> 
> but this returns an error
> 
> 
> Error in stri_split_regex(string, pattern, n = n, simplify =
> simplify, : Unrecognized backslash escape sequence in pattern.
> (U_REGEX_BAD_ESCAPE_SEQUENCE)

This happens because the second parameter of str_split is by default a
regular expression, and a backslash has a special meaning in regular
expressions: when preceding other characters, it may change the way
they are interpreted. (For example, w means a literal "w"
character, while \w means "any alphanumeric character". On the
other hand, [ starts a character group, but \[ means just an opening
square bracket.) See ?regex for more info on that.

Since you want a literal backslash, you need to escape it with another
backslash: \\

But to write a string literal of a double-backslash in R, you need to
escape both backslash characters, each with their own backslash: ""

## fname <- "D:\\Data\\OneDrive\\ISTA Documents\\QT_App\\QT Analysis
Input Data Example WorkBook.xlsx"
## message("")
\\
## str_split(fname, "")
[[1]]
[1] "D:" 
[2] "Data"   
[3] "OneDrive"   
[4] "ISTA Documents" 
[5] "QT_App" 
[6] "QT AnalysisInput Data Example WorkBook.xlsx"

You can also avoid all layers of the backslash hell (except the first)
if you choose to split by fixed strings instead of regular expressions
by using stringr::fixed:

## str_split(fname, fixed("\\"))

-- 
Best regards,
Ivan

__
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] Remove Even Number from A Vector

2019-03-03 Thread Ivan Krylov
Hi Darren,

On Sat, 2 Mar 2019 22:27:55 +
Darren Danyluk  wrote:

> It sounds like she is working with the very basics of this software,
> and her task is to write the code which would result in the
> extraction of "odd" data from a dataset of restaurant sales.

Not a native English speaker here; what exactly do you mean by "odd" in
this case?

If I ignore the "subject" field, it looks like your daughter should be
looking for outlier detection methods. For example, https://rseek.org/
offers some good results for "outlier detection".

-- 
Best regards,
Ivan

__
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] Unobtainable source code of specific functions

2019-03-14 Thread Ivan Krylov
On Thu, 14 Mar 2019 15:44:07 +0100 (CET)
julien.lacr...@student.uliege.be wrote:

> My question is therefore the following, how or where could I find
> these source code ? (Note : I have the package Igraph installed)

Functions wrapped in .Call() are implemented in a compiled language,
with corresponding sources in the src/ directory of a package.

For example, the call to C_R_igraph_shortest_paths seems to end up in
https://github.com/igraph/igraph/blob/6faf5610b8ab0175466e93a8a1d1981987976a38/src/structural_properties.c#L460

-- 
Best regards,
Ivan

__
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] Extract data of special character

2019-03-14 Thread Ivan Krylov
On Fri, 15 Mar 2019 03:06:28 +0800
roslinazairimah zakaria  wrote:

> how many of ATTRIBUTE related to TRAITS.

The table() function can be used to count occurrences of each
combination of factor levels. Does extracting the two columns by
dd[,c('ATTRIBUTE','TRAIT')] and passing the result to table() help?

-- 
Best regards,
Ivan

__
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] Extract data of special character

2019-03-15 Thread Ivan Krylov
В Fri, 15 Mar 2019 08:06:52 +0800
roslinazairimah zakaria  пишет:

> I want to draw boxplot for each individual score of the
> attributes.

You mean, a box per every possible ATTRIBUTE value? This is easily
doable with the bwplot() function from library(lattice).

-- 
Best regards,
Ivan

__
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] Help with gsub function

2019-03-15 Thread Ivan Krylov
On Fri, 15 Mar 2019 19:45:27 +
Bill Poling  wrote:

Hello Bill,

> tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "")

Is the pattern supposed to mean something besides the "-" you want to
remove? For the problem you describe, pattern="-" should be enough. It
should locate all hyphens in the string and replace them with empty
strings, i.e. remove them.

-- 
Best regards,
Ivan

__
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] problem with nlsLM.....

2019-03-20 Thread Ivan Krylov
On Wed, 20 Mar 2019 08:02:45 +
akshay kulkarni  wrote:

> formulaDH5 <- as.formula(HM1 ~ (a + (b * ((HM2 + 0.3)^(1/2 +
> (A*sin(w*HM3 + c) + C))

The problem with this formula is simple: the partial derivative with
respect to `a` is the same as the partial derivative with respect to
`C`. This makes the regression problem have an infinite number of
solutions, all of them satisfying equation \lambda_1 * a + \lambda_2 *
C + \lambda_3 = 0 for some values of \lambda_i. Gradient-based
optimizers (which both nls and nlsLM are) don't like problems with
non-unique solutions, especially when the model function has same
partial derivative with respect to different variables, making them
indistinguishable.

Solution: remove one of the variables.

> > formulaDH3  
> HM1 ~ (a + (b * ((HM2 + 0.3)^(1/3 * (c * log(HM3 + 27))

The problem with this formula is similar, albeit slightly different.

Suppose that (a, b, c) is a solution. Then (\lambda * a, \lambda * b,
c / \lambda) is also a solution for any real \lambda. Once again,
removing `c` should get rid of ambiguity.

> I've checked the Internet  for a method of getting the starting
> values, but they are not comprehensiveany resources for how to
> find the starting values?

Starting values depend on the particular function you are trying to
fit. The usual approach seems to be in transforming the formula and
getting rid of parts you can safely assume to be small until it looks
like linear regression, or applying domain specific knowledge (e.g.
when trying to it a peak function, look for the biggest local maximum
in the dataset).

If you cannot do that, there also are global optimization algorithms
(see `nloptr`), though they still perform better on some problems
and worse on others. It certainly helps to have upper and lower
bounds on all parameter values. I've heard about a scientific group
creating a pool of many initial Levenberg-Marquardt parameter estimates,
then improving them using a genetic algorithm. The whole thing
"converged overnight" on a powerful desktop computer.

-- 
Best regards,
Ivan

__
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] problem with nlsLM.....

2019-03-20 Thread Ivan Krylov
On Wed, 20 Mar 2019 09:43:11 +
akshay kulkarni  wrote:

> But doesn't removing some of the parameters reduce the precision of
> the relationship between the response variable and the
> predictors(inefficient estimates of the coefficients)?

No, it doesn't, since there is already more variables in the formula
than it has relationships between response and predictors.

Let me offer you an example. Suppose you have a function y(x) = a*b*x +
c. Let's try to simulate some data and then fit it:

# choose according to your taste
a <- ...
b <- ...
c <- ...

# simulate model data
abc <- data.frame(x = runif(100))
abc$y <- a*b*abc$x + c
# add some normally distributed noise
abc$y <- abc$y + rnorm(100, 0, 0.01)

Now try to fit formula y ~ a*b*x + c using data in data frame abc. Do
you get any results? Do they match the values you have originally
set?[*]

Then try a formula with the ambiguity removed: y ~ d*x + c. Do you get a
result? Does the obtained d match a*b you had originally set?

Note that for the d you obtained you can get an infinite amount of
(a,b) tuples equally satisfying the equation d = a*b and the original
regression problem, unless you constrain a or b.

-- 
Best regards,
Ivan

[*] Using R, I couldn't, but the nonlinear solver in gnuplot is
sometimes able to give *a* result for such a degenerate problem when
data is sufficiently noisy. Of course, such a result usually doesn't
match the originally set variable values and should not be trusted.

__
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] System("source activate condaenv")

2019-03-20 Thread Ivan Krylov
On Wed, 20 Mar 2019 09:59:21 +0100
Sandra Elisabeth Chaudron  wrote:

> I am using the server version of RStudio and I have a script where I
> want to activate the conda environment that I set up for a
> bioinformatic tool called MinVar.
> For that I use in my script the command: system("source
> /home/ubuntu/miniconda3/bin/activate minvar").

Unfortunately, this is probably not going to work as is.

In *nix-like systems, child processes cannot alter the environment
variables of their parents, so when one wants a shell script to alter
environment variables in the current session, they use "source".

The reason your command returns an error message is probably because
"source" is a command specific to /bin/bash, while R calls /bin/sh,
which might be symlinked to /bin/dash instead of /bin/bash on Ubuntu.
The POSIX-correct form would be ". /home/ubuntu/miniconda3/bin/activate
minvar", that is, just a dot instead of the word "source".

But the underlying issue would stay the same: R system()
function launches a subprocess /bin/sh; the "source" or "." command
causes the environment of the *child* shell process to be changed;
environment of the *parent* R process stays unchanged.

Your best bet would be to read the activate script, understand the
changes to the environment it causes and use Sys.getenv() /
Sys.setenv() to make equivalent changes in R environment variables.

-- 
Best regards,
Ivan

__
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] problem with nls....

2019-03-21 Thread Ivan Krylov
One of the assumptions made by least squares method is that the
residuals are independent and normally distributed with same parameters
(or, in case of weighted regression, the standard deviation of the
residual is known for every point). If this is the case, the parameters
that minimize the sum of squared residuals are the maximum likelihood
estimation of the true parameter values.

The problem is, your data doesn't seem to adhere well to your formula.
Have you tried plotting your HF1 - ((m/HF6) + 1) against HF6 (i.e. the
residuals themselves)? With large residual values (outliers?), the loss
function (i.e. sum of squared residuals) is disturbed and doesn't
reflect the values you would expect to get otherwise. Try computing
sum((HF1 - ((m/HF6) + 1))^2) for different values of m and see if
changing m makes any difference.

Try looking up "robust regression" (e.g. minimize sum of absolute
residuals instead of squared residuals; a unique solution is not
guaranteed, but it's be less disturbed by outliers).

-- 
Best regards,
Ivan

__
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] problem with nls....

2019-03-23 Thread Ivan Krylov
On Fri, 22 Mar 2019 12:29:14 +
akshay kulkarni  wrote:

> How do I get the gradient, Hessian, and the jacobian of the
> objective function created by call to the nls?

nls() return value is a list containing an entry named `m`, which is an
object of type "nlsModel". It doesn't seem to be documented in modern
versions of R[*], so what I am describing might be an implementation
detail subject to change. Still, model$m$gradient() should return the
jacobian; Hessian is usually estimated as crossprod() of jacobian; and
the gradient of the objective function is computed as
-2*colSums(model$m$resid() * model$m$gradient()).

> Also, I've checked the residuals...they are approximately normally
> distributedI am still wondering why the nls call is not getting
> converged!

The more important question is, how does the objective function (sum of
squared residuals) depend on the parameter `m` you are trying to find?
Try computing it for various values of `m` and looking at the result:

plot(
Vectorize(
function(m) {
model$m$setPars(m);
model$m$deviance()
}
),
from = ..., to = ... # fill as needed
)

-- 
Best regards,
Ivan

[*] But used to be:
http://unixlab.stat.ubc.ca/R/library/stats/html/nlsModel.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] Just confirming (?): No way to "export" a (Quartz) plot...

2019-03-25 Thread Ivan Krylov
On Sun, 24 Mar 2019 23:35:30 -0700
David Goldsmith  wrote:

> No way to "export" a (Quartz) plot...
> ...once I've got it the way I want it, (e.g., to a PNG)

Well, there seems to be a quartz.save() function, but I cannot attest
to its well-behavedness, not having a Mac to test it on.

-- 
Best regards,
Ivan

__
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] converting a character string to an object name

2019-03-29 Thread Ivan Krylov
On Fri, 29 Mar 2019 14:07:16 +0100
Assa Yeroslaviz  wrote:

> Is there a way to read for each sheet the name from names(wb) and
> convert it to a name for the object?

See `get` and `assign` functions for a way to use strings as object
names. Generally, it might not be a good idea to do that (what if your
spreadsheet contains a sheet named `c` or `q`, or `data.frame`?).

If you want to save yourself some typing, consider using `with` or
`within`, though they suffer from similar problems.

-- 
Best regards,
Ivan

__
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] String replace

2019-04-03 Thread Ivan Krylov
On Wed, 3 Apr 2019 15:01:37 +0100
Graham Leask via R-help  wrote:

Suppose that `BHC$Date` contains a string "M_24".

You do:

> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_2" ,
> "01-04-2017"))

before you have a chance to do:

> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date ,
> "M_24" , "01-02-2019"))

So now it has "01-04-20174" because the first expression had a
successful match and already made a replacement.

help() for stringi::stringi-search-regex says that look-around
expressions are supported, so one of the ways to prevent this would be
to modify your patterns to look like e.g. 'M_2(?!\\d)' to match
'M_2' that is *not* followed by a digit.

-- 
Best regards,
Ivan

__
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] Greek characters in R studio

2019-04-09 Thread Ivan Krylov
On Mon, 8 Apr 2019 18:41:16 +0300
kostas zogopoulos  wrote:

> How do you read a csv file that contains greek characters as part of
> the header (i.e. α, β etc) in R studio?

Determine the character encoding used in the file (is it UTF-8,
ISO8859-7 or something else?) and pass it as fileEncoding="..."
parameter to read.csv() function.

-- 
Best regards,
Ivan

__
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] md5sum issues

2021-02-02 Thread Ivan Krylov
On Tue, 2 Feb 2021 17:01:05 +0100
Ivan Calandra  wrote:

> This happens to all text-based files (Rmd, MD, CSV...) but not to 
> non-editable files (PDF, XLSX...).

This is probably caused by Git helpfully converting text files from LF
(0x10) line endings to CR LF (0x13 0x10) when checking out the
repository clone on Windows (and back when checking in).

This configuration option is described in Pro Git:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_core_autocrlf

-- 
Best regards,
Ivan

__
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] ISO recommendations for plot output format from R to MS Word

2021-02-12 Thread Ivan Krylov
On Fri, 12 Feb 2021 15:05:33 -0800
Robert Dodier  wrote:

> Towards the goal of creating an MS Word document which others with
> unspecified versions of MS Word can view correctly, in what format
> should I export figures from R?

A cross-platform plotting device designed specifically for word
processors is devEMF. In my experience, best compatibility with Word
can be achieved by creating figures with parameters (emfPlus = TRUE,
emfPlusFont = FALSE, emfPlusRaster = TRUE), but I only checked Word
2003 and 2010 (for Windows).

-- 
Best regards,
Ivan

__
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] Assigning several lists to variables whose names are contained in other variables

2021-04-09 Thread Ivan Krylov
Dear Wolfgang,

On Fri, 9 Apr 2021 11:48:55 +0200
Wolfgang Grond  wrote:

> I want to assign the subnets to variables whose names contain the
> name of the subnet

Apologies if this sounds too opinionated, but creating variable names
from variable values is a FAQ in a different dynamic language:

https://perldoc.perl.org/perlfaq7#How-can-I-use-a-variable-as-a-variable-name?

Most of the explanation doesn't apply to R, of course, but the main
idea here is to use data structures instead of causing (potential,
unlikely, but still) conflicts in the variable namespace. What if you
create a list of function values instead of just a bunch of variables?

results <- list()
for(i in 1:nrow(datatable)) {
val <- datatable$column[i]
results[[as.character(val)]] <- my_function(val)
}

Or even

results <- lapply(setNames(nm = datatable$column), my_function)

Wouldn't that be more convenient?

-- 
Best regards,
Ivan

__
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] Plotting the ASCII character set.

2021-07-03 Thread Ivan Krylov
Hello Rolf Turner,

On Sat, 3 Jul 2021 14:02:59 +1200
Rolf Turner  wrote:

> Can anyone suggest how I might get my plot_ascii() function working
> again?  Basically, it seems to me, the question is:  how do I persuade
> R to read in "\260" as "\ub0" rather than "\xb0"?

Part of the problem is that the "\xb0" byte is not in ASCII, which
covers only the lower half of possible 8-bit bytes. I guess that the
strings containing bytes with highest bit set used to be interpreted as
Latin-1 on your machine, but now get interpreted as UTF-8, which
changes their meaning (in UTF-8, the highest bit being set indicates
that there will be more bytes to follow, making the string invalid if
there is none).

The good news is, since it's Latin-1, which is natively supported by R,
there are even multiple options:

1. Mark the string as Latin-1 by setting Encoding(a) <- 'latin1' and
let R do the re-encoding if and when Pango asks it for a UTF-8-encoded
string.

2. Decode Latin-1 into the locale encoding by using iconv(a, 'latin1',
'') (or set the third parameter to 'UTF-8', which would give almost the
same result on a machine with a UTF-8 locale). The result is, again, a
string where Encoding(a) matches the truth. Explicitly setting UTF-8
may be preferable on Windows machines running pre-UCRT builds of R
where the locale encoding may not contain all Latin-1 characters, but
that's not a problem for you, as far as I know.

For any encoding other than Latin-1 or UTF-8, option (2) is still valid.

I have verified that your example works on my GNU/Linux system with a
UTF-8 locale if I use either option.

-- 
Best regards,
Ivan

__
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] Plotting the ASCII character set.

2021-07-04 Thread Ivan Krylov
On Sun, 4 Jul 2021 13:59:49 +1200
Rolf Turner  wrote:

> a substantial number of the characters are displayed as a wee
> rectangle containing a 2 x 2 array of digits such as
> 
> >   0 0
> >   8 0  

Interesting. I didn't pay attention to it at first, but now I see that
a range of code points, U+0080 to U+009F, corresponds to control
characters (also, 0+00A0 is non-breakable space), not anything
printable. Also, Latin-1 doesn't define any meaning for bytes
0x80..0x9f, but here they are decoded to same-valued Unicode code
points. And the actual code point for € is U+20AC, not even close to
what we're working with.

> Also note that there is a bit of difference between the results of
> using Encoding() and the results of using iconv()

You are right. I didn't know that, but my reading of the function
translateToNative in src/main/sysutils.c suggests that R decodes
strings marked as 'latin1' as Windows-1252 (if it's available for the
system iconv()) and uses the actual Latin-1 as a fallback.

?Encoding does warn that 'latin1' is ambiguous and system-dependent
with regards to bytes 0x80..0x9f, so text() seems to be right to use
Latin-1 and not Windows-1252 when trying to plot byte 0x80 encoded as
CE_LATIN1 as U+0080. Although there's a /* FIXME: allow CP1252? */
comment in src/main/sysutils.c, function reEnc, which is used by text().

> Is there any way that I can get the Euro symbol to display correctly
> in such a graphic?

I think that iconv(a, 'CP1252', '', '\ufffd') should work for you. At
least it seems to work for the € sign. It does leave the following
bytes undefined, represented as � U+FFFD REPLACEMENT CHARACTER:

as.raw(which(is.na(
 iconv(sapply(as.raw(1:255), rawToChar), 'CP1252', '')
)))
# [1] 81 8d 8f 90 9d

Not sure what can be done about those. With Latin-1, they would
correspond to unprintable control characters anyway.

-- 
Best regards,
Ivan

__
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] conditional output of string to file n times...

2021-07-07 Thread Ivan Krylov
On Tue, 6 Jul 2021 09:27:20 -0400
Evan Cooch  wrote:

> I was wondering if there was an elegant/simple way to do this?

rep(label, times = count) should give you a character vector with the
answer ready for writeLines().

-- 
Best regards,
Ivan

__
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] Makefile error

2021-07-24 Thread Ivan Krylov
On Sat, 24 Jul 2021 11:19:41 + (UTC)
Bintao Cui via R-help  wrote:

> Makefile.in:87: *** missing separator.  Stop. 

Thanks for showing us the error message, but this isn't enough
information. In order to be able to help, we also need to know what
exactly you did to get the error message.

See also: http://www.catb.org/~esr/faqs/smart-questions.html

> I am trying to install R-4.1.0 version to fedora

And you're building from source because there doesn't seem to be
R-4.1.0 in the repos? I think you can install R-4.1.0 from Rawhide.

See also: the mailing list about R in Fedora:
https://stat.ethz.ch/mailman/listinfo/r-sig-fedora
This list is about programming in R language, so perhaps your question
would be better addressed there.

-- 
Best regards,
Ivan

__
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] Unending loop

2021-07-29 Thread Ivan Krylov
Hi Nick,

Thanks for providing an example that doesn't seem to depend on external
data!

On Thu, 29 Jul 2021 09:07:12 +0100
Nick Wray  wrote:

> I am looking at the extRemes package

It's probable that there aren't many extRemes users on R-help, but if
you contact the maintainer of extRemes (run maintainer('extRemes') to
get their name and address), you might get help that way.

> When I run it on my own laptop it works fine, but when I run it on my
> uni laptop (which is pretty new and I only was given a couple of
> months ago) when it gets to the thresh.range line it goes into an
> unending loop (or something) with the little red Stop button, and
> just sits there.

If you interrupt it there, backtrace() will point you in the direction
of seemingly endless loop. You can also set options(error = recover)
and get a debugger prompt at the point where you interrupted the code
or in any other frame. See ?browser and RShowDoc('R-lang') part 9
"Debugging" for more information.

> I have R 4.0.4 on my uni laptop and R 4.0.5 on my own one.  Could
> this be the reason?

I'm afraid it's easier for you to answer this question. It should be
possible to install R on computers even without administrator access.
Can you reproduce the issue with
https://cloud.r-project.org/bin/windows/base/old/4.0.4/ on your own
laptop or https://cloud.r-project.org/bin/windows/base/old/4.0.5/ on the
university-issued one?

>   [[alternative HTML version deleted]]

This mailing list strips away the HTML part of multipart/mixed
messages, so to make sure that your messages look exactly as you type
them, please compose them in plain text.

-- 
Best regards,
Ivan

__
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] substitute column data frame based on name stored in variable in r

2021-08-09 Thread Ivan Krylov
On Mon, 9 Aug 2021 10:26:03 +0200
Luigi Marongiu  wrote:

> vect = names(df)
> sub_df[vect[1]]

> df$column[df$column == value] <- new.value

Let's see, an equivalent expression without the $ syntax is
`df[['column']][df[['column']] == value] <- new.value`. Slightly
shorter, matrix-like syntax would give us
`df[df[['column']] == value, 'column'] <- new.value`.

Now replace 'column' with vect[i] and you're done. The `[[`-indexing is
used here to get the column contents instead of a single-column
data.frame that `[`-indexing returns for lists.

Also note that df[[names(df)[i]]] should be the same as df[[i]] for
most data.frames.

-- 
Best regards,
Ivan

__
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] substitute column data frame based on name stored in variable in r

2021-08-09 Thread Ivan Krylov
On Mon, 9 Aug 2021 13:16:02 +0200
Luigi Marongiu  wrote:

> df = data.frame(VAR = ..., VAL = ...)
> vect = letters[1:5]

What is the relation between vect and the column names of the data
frame? Is it your intention to choose rows or columns using `vect`?

> df[df[['vect[2]']] == 2, 'vect[2]']

'...' creates a string literal. If you want to evaluate an R
expression, don't wrap it in quotes.

I had assumed you wanted to put column names in the vector `vect`, but
now I'm just confused: `vect` is the same as df$VAR, not colnames(df).
What do you want to achieve?

Again, you can access the second column with much less typing by
addressing it directly: df[[2]]

Does it help if you consult [**] or some other tutorial on subsetting
in R?

-- 
Best regards,
Ivan

[**] 
https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Index-vectors
https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Lists

__
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] substitute column data frame based on name stored in variable in r

2021-08-09 Thread Ivan Krylov
Thanks for providing a reproducible example!

On Mon, 9 Aug 2021 15:33:53 +0200
Luigi Marongiu  wrote:

> df[df[['vect[2]']] == 2, 'vect[2]'] <- "No"

Please don't quote R expressions that you want to evaluate. 'vect[2]'
is just a string, like 'hello world' or 'I want to create a new column
named "vect[2]" instead of accessing the second one'.

> Error in `[<-.data.frame`(`*tmp*`, df[[vect[2]]] == 2, vect[2], value
> = "No") : missing values are not allowed in subscripted assignments
> of data frames

Since df[[2]] containts NAs, comparisons with it also contain NAs. While
it's possible to subset data.frames with NAs (the rows corresponding to
the NAs are returned filled with NAs of corresponding types),
assignment to undefined rows is not allowed. A simple way to remove the
NAs and only leave the cases where df[[vect[2]]] == 2 is TRUE would be
to use which(). Compare:

df[df[[vect[2]]] == 2,]
df[which(df[[vect[2]]] == 2),]

-- 
Best regards,
Ivan

__
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] Cars2

2021-08-17 Thread Ivan Krylov
On Tue, 17 Aug 2021 09:50:54 +
George Bellas  wrote:

> I found cars2 earlier this week, but now when I look at the list,
> it's not there!

That sounds like a dataset provided by a contributed package. Does
https://search.r-project.org/ help find it again?

-- 
Best regards,
Ivan

>   [[alternative HTML version deleted]]

P.S. When you compose hybrid HTML + plain text e-mail, the HTML version
gets stripped by the mailing list software and everyone gets the plain
text version, which may look wildly different from the HTML original.
Please stick to plain text e-mail to avoid nasty surprises.

__
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] Cars2

2021-08-19 Thread Ivan Krylov
On Thu, 19 Aug 2021 00:12:51 +
George Bellas  wrote:

> I'm still looking for it, it had car prices and car models in it. I
> couldn't find it through the r search link.

Could it be the third search result for a query of 'cars2'?

https://search.r-project.org/?P=cars2&SORT=&HITSPERPAGE=10&DB=r-manuals&DB=r-help&DB=cran-views&DB=cran-info&DB=cran-help&DB=cran-news&DB=cran-readme&DB=cran-vignettes&DEFAULTOP=and&FMT=query&xDB=cran-help&xFILTERS=.%7E%7E

->

https://search.r-project.org/CRAN/refmans/fBasics/html/data-examples.html

>> cars2 contains the price, country, reliability, mileage, type,
>> weight, engine displacement and net horsepower of various car
>> models.

-- 
Best regards,
Ivan

__
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] Dynamic Creation and Use of Object Names

2021-08-23 Thread Ivan Krylov
On Mon, 23 Aug 2021 08:37:54 +0200
Dr Eberhard Lisse  wrote:

> create the variables dynamically and add them to to
> the grid (dynamically, ie adding more countries)

In my opinion, creating variables in the global environment
programmatically may lead to code that is hard to understand and debug
[*]. A key-value data structure (a named list or a separate
environment) would avoid the potential problems from variable name
collision. How about the following:

1. Put the countries in a vector: c('Namibia', 'Germany', ...)

2. Use lapply() to get a list of objects returned from your PICTURE
   function

3. To save the pictures into individual files, loop over the list. You
   can use setNames on the step 1 or 2 to make it a named list and keep
   the country names together with their pictures:
   
   for (n in names(pictures)) {
 dev.new()
 print(pictures[[n]])
 ggsave(paste0(n, '.png'), ...)
 dev.off()
   }

   (You can also use the png() device and plot straight to the file,
   avoiding the need to draw the plot in the window for a fraction of a
   second and for ggsave().)

4. Use the grobs= argument of grid.arrange() to pass the list of
   objects to arrange instead of passing individual objects via ...

-- 
Best regards,
Ivan

[*] For example, there's this FAQ for a different language:
https://perldoc.perl.org/perlfaq7#How-can-I-use-a-variable-as-a-variable-name?

__
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] Handling interrupts in long-running R functions

2021-09-10 Thread Ivan Krylov
Hello everyone,

I'm writing an R function that may be running for "long" periods of
time (think tens of minutes), and I would like to be able to tell it:
"please stop what you're doing and return the not-yet converged results
as they are for inspection".

The behaviour I'm striving for is

1) User presses interrupt
2) Function handles the interrupt and returns as if the convergence
   test passed
3) By some black magic, the interrupt condition is raised on the
   previous function call level (to avoid the situation where my
   function is called in a loop by some other function and the user
   wants to interrupt the whole process, not just my function).

Is this a good idea? Is (3) even possible? (I guess I could check the
length of sys.parents() and avoid recovering from the interrupt if
called from some other function, but that feels dirty.) Could something
similar be achieved with options(error = recover) and R restarts? Are
there other ways of, well, interrupting the execution of R functions
without changing the semantics of interrupts in R?

I've been working with MATLAB lately (when in Rome, do as Romans
do...), and their idiom for my desired behaviour is "create a plot
window and return when that window is closed", but that doesn't
translate well to R.

-- 
Best regards,
Ivan

__
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] (no subject)

2021-10-06 Thread Ivan Krylov
On Tue, 5 Oct 2021 22:20:33 +
Thomas Subia  wrote:

> Some co-workers are wondering about how secure R software is.

I'm afraid that this question is too hard to answer without their
threat model. Secure against what, specifically?

> Is there any documentation on this which I can forward to them?

Well, R is a programming language. It's Turing-complete (see halting
problem), will happily run machine code from shared objects (see
dyn.load, .C, .Call), and install.packages() is there to download
third-party code from the Internet. But that's the case with all
programming languages I know that are used for statistics, which aren't
supposed to run untrusted code.

Maybe you're concerned about data input/output instead. Functions are
first-class objects, so it's possible to save and load them from data
files. Not sure if there's a way to run code on data load, but you can
do it on print() (e.g. print.nls(x) calling x$m$getAllPars()), so don't
load()/readRDS() untrusted data files. There are known bugs in the
deserialiser, too: https://bugs.r-project.org/show_bug.cgi?id=16034

Don't know if it's documented anywhere, though. What are your
co-workers concerned about?

-- 
Best regards,
Ivan

__
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] RSQLite slowness

2021-10-07 Thread Ivan Krylov
On Wed,  6 Oct 2021 16:23:15 +
Rasmus Liland  wrote:

>"SELECT * FROM gene2refseq
> LEFT JOIN gene_info ON
>   gene_info.GeneID = gene2refseq.GeneID
> WHERE gene2refseq.`RNA_nucleotide_accession.version` 
>   LIKE ?"

<...>

>   x1 <- DBI::dbGetQuery(conn=conn, 
> statement=statement, 
> param=list(Håkan20210914$RNANucleotideAccession))

I think that the problem here is that you pass a vector as a bound
parameter to LIKE, when parameter placeholders usually expect a scalar.
DBI transparently handles this:

>> The elements of the `params` argument do not need to be scalars,
>> vectors of arbitrary length (including length 0) are supported.  For
>> queries, calling dbFetch() binding such parameters returns
>> concatenated results, equivalent to binding and fetching for each
>> set of values and connecting via rbind().

I think this means that DBI runs a SELECT for each value in
Håkan20210914$RNANucleotideAccession, which is understandably slower
than a single query. Unfortunately, it's hard to pass vectors of values
to queries with bound parameters; the SQL engines I know don't have a
syntax for "WHERE param IN (:multi_placeholder:)". SQLite comes with
carray [1], but I don't know whether it's exposed by RSQLite (could be
hard to do in a pointer-safe way), and you're already aware of the
traditional way of doing that: create a temporary table, populate it
and JOIN with the rest of the query.

-- 
Best regards,
Ivan

[1] https://www.sqlite.org/carray.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] How to read from a file within a gzipped file

2021-10-11 Thread Ivan Krylov
On Mon, 11 Oct 2021 16:24:01 +
"Conklin, Mike (GfK) via R-help"  wrote:

> Is there a way to return a connection to a single file within a
> zipped file using gzfile or some other method.

Sure! Use unz() instead of gzfile().

-- 
Best regards,
Ivan

__
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] tidyverse: read_csv() misses column

2021-11-02 Thread Ivan Krylov
On Mon, 1 Nov 2021 15:24:47 -0700 (PDT)
Rich Shepard  wrote:

> + mutate(
> + sampdt = make_datetime(year, mon, day, hr, min)
> + )

<...>

> produces the sampdt column, but it, and the timezone, are not present
> in the cor_disc tibble

That's because mutate() doesn't, well, mutate its argument. It _returns_
its changes, but it doesn't save them in the original variable. It's
your responsibility to assign the result somewhere:

cor_disc |> select(...) |> mutate(...) -> cor_disc_mutated

(Idiomatic R makes it hard to accidentally change the value of the
variable passed to a function from inside of that function. Anything
short of environments, reference class objects and/or non-standard
evaluation will not affect the value outside the function call.)

-- 
Best regards,
Ivan

__
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] sink() not working as expected

2021-11-02 Thread Ivan Krylov
On Tue, 2 Nov 2021 10:18:07 -0700 (PDT)
Rich Shepard  wrote:

> 'corvalis discharge summary\n'
> summary(cor_disc)
> sd(cor_disc$cfs)
> '-\n'

In the interactive mode, on the top level of execution, these commands
behave as if you had written

print(sink('data-summaries.txt'))
print('corvalis discharge summary\n')
print(summary(cor_disc))
print(sd(cor_disc$cfs))

instead. When you source() a script, auto-printing is not performed.
This is explained in the first paragraph of ?source, but not ?sink. If
you want to source() scripts and rely on their output (including
sink()), you'll need to print() results explicitly.

-- 
Best regards,
Ivan

__
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] names.data.frame?

2021-11-03 Thread Ivan Krylov
On Wed, 3 Nov 2021 20:35:58 +0200
Leonard Mada via R-help  wrote:

> class(p) = c("pm", class(p));

Does NextMethod() work for you?

-- 
Best regards,
Ivan

__
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] Please help me debugg

2021-11-05 Thread Ivan Krylov
On Fri, 5 Nov 2021 12:29:21 +0530
Shrinivas Dharma  wrote:

> I am trying to run the following double loop which  I have written
> but it is not running.

Thanks for providing the code, but it's much easier to help you if you
provide the data, too. Use dput(head(...)) to make it easy for us to
copy-paste the data.

> Unable to get my head around the mistake. Please try to spot the bug
> and help

> > for(i in 1:122){  
> + for(j in 1:68){
> + if(V(FriendGraph)$name[i]==names[j]){
> + gender_vector[i]<-sex[j]
> + break;}
> + else{gender_vector[i]<-NA}
> + } }

> Error in `[.data.frame`(sex, j) : undefined columns selected

sex[j] extracts the `j`th column out of the `sex` data frame. Does
`sex` contain 68 columns? If you want the `j`th row, use sex[j,].

Also, it may help to use merge() and have the R language do some of the
work for you, if you organise your `names` and `sex` into a single
data.frame to merge V(FriendGraph) with.

Additionally, you might save some of the execution time if you move the
V(FriendGraph) calculation out of the loop.

>   [[alternative HTML version deleted]]

P.S. Your post came across mostly fine, but please compose your mail in
plain text, not HTML. We only see the plain text version, and in many
cases, the mailers horribly mangle the post when they automatically
generate it from the HTML message.

-- 
Best regards,
Ivan

__
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] Environmental oddity.

2021-11-07 Thread Ivan Krylov
On Sun, 7 Nov 2021 09:02:36 +0530
Deepayan Sarkar  wrote:

> This sounds like a difference in precedence. The expression
> 
> if (log) 1 else dnorm(x, mean, sd) / sd
> 
> is apparently being interpreted differently as
> 
> d1: (if (log) 1 else dnorm(x, mean, sd)) / sd
> d2: if (log) 1 else (dnorm(x, mean, sd)) / sd)
> 
> It's unclear how environments could affect this, so it would be very
> helpful to have a reproducible example.

This seems to be caused by the deparser producing the same source text
for different expressions:

( x <- expression(`/`(`*`(a, if (b) c else d), e)) )
# expression(a * if (b) c else d/e)
( y <- expression(a * if (b) c else d/e) )
# expression(a * if (b) c else d/e)
all.equal(x, y)
# [1] TRUE

The expressions *seem* to be the same, but:

as.list(x[[1]])
# [[1]]
# `/`
# 
# [[2]]
# a * if (b) c else d
# 
# [[3]]
# e

as.list(y[[1]])
# [[1]]
# `*`
# 
# [[2]]
# a
# 
# [[3]]
# if (b) c else d/e

Perhaps it could be possible to make the deparser output extra
parentheses at the cost of slightly uglier output in cases when they
are not needed. all.equal.language uses deparse(), so it will behave
correctly when the deparse() output is fixed.

In the original example, as.list(body(d1)) and as.list(body(d2)) should
show different results, too.

-- 
Best regards,
Ivan

__
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] Warning msg after reinstall?

2021-11-07 Thread Ivan Krylov
On Sun, 7 Nov 2021 17:07:20 -0500
Brian Lunergan  wrote:

> I run R on Linux Mint 19.3.

If my advice below doesn't help, let's continue this in the other
mailing list, r-sig-deb...@r-project.org
.

> Warning messages:
> 1: In readLines(file, skipNul = TRUE) :
>   cannot open compressed file
> '/usr/lib/R/site-library/labelled/DESCRIPTION', probable reason 'No
> such file or directory'
> 2: In readLines(file, skipNul = TRUE) :
>   cannot open compressed file
> '/usr/lib/R/site-library/TH.data/DESCRIPTION', probable reason 'No
> such file or directory'

It seems that you've been installing packages into the system-wide
library. Could you try removing all R-related deb packages from your
system, wiping the directories /usr/lib/R and /usr/share/R, then
installing R again? Alternatively, write a script to search files and
directories in /usr/lib/R and /usr/share/R, use `dpkg -S` to see if
it's owned by a package and move it away if not. Both options are quite
a bit dangerous, though.

On second thought, what is in /usr/lib/R/site-library/labelled
and /usr/lib/R/site-library/TH.data? If the two directories are empty,
you can just remove them.

-- 
Best regards,
Ivan

__
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] Problem Compiling v-4.x.x

2021-11-08 Thread Ivan Krylov
On Mon, 8 Nov 2021 14:57:24 -0500
"Stephen P. Molnar"  wrote:

> /home/comp/Downloads/R Packages/R-4.1.0/R-4.1.0/etc/Makeconf:10:
> Packages/R-4.1.0/R-4.1.0/share/make/vars.mk: No such file or directory

This is the case of Make breaking things by splitting the file path by
whitespace. Line 9 of Makeconf even contains a comment regarding this:

>> ## This fails if it contains spaces, or if it is quoted

Rename "R Packages" to R_Packages (or anything else without spaces in
it) to work around the error.

-- 
Best regards,
Ivan

__
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] How to use contour plot?

2021-11-16 Thread Ivan Krylov
On Tue, 16 Nov 2021 09:45:34 +0100
Luigi Marongiu  wrote:

> contour(df$X, df$Y, df$Z)  

contour() works on matrices (sometimes called "wide format" data). Z
must be a numeric matrix, X must be a numeric vector with length(X) ==
nrow(Z), and Y must be a numeric vector with length(Y) == ncol(Z). This
is described in ?contour.

Since you have a three-column data.frame ("long format" data), you can
use lattice::contourplot instead (e.g. contourplot(Z ~ X + Y, data =
df)) or the appropriate combination of ggplot2 functions.

Alternatively, if your data is already on a grid, you can make a matrix
out of your three-column data.frame, but the procedure is a bit awkward:

ret <- reshape(
 df, direction = "wide", v.names = 'Z', idvar = 'X', timevar = 'Y'
)
contour(
 X <- ret[, 1],
 Y <- attr(ret, "reshapeWide")$times,
 Z <- as.matrix(ret[,-1])
)

(Can be also done with xtabs(); reshape2 and many other contributed
packages also offer some ways of doing that.)

-- 
Best regards,
Ivan

__
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] severe bug in LICORS/kmeanspp

2021-11-19 Thread Ivan Krylov
On Wed, 3 Nov 2021 23:04:06 +0100
frit...@web.de wrote:

> Since LICORS has not been updated since 2013, I am not sure if there
> is an active maintainer. The named maintainer works for Google since
> 2014 and may have abandoned the package.

Computing the wrong distances is a serious problem. Perhaps you could
take a chance at contacting the original package author via
https://www.gmge.org/ ? Perhaps Georg M. Goerg could be persuaded to
publish this small fix.

-- 
Best regards,
Ivan

__
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] Defining Parameters in arules

2021-11-23 Thread Ivan Krylov
Hello,

If you don't get an answer here, consider asking the package
maintainer: Michael Hahsler

-- 
Best regards,
Ivan

__
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] Error: vector memory exhausted (limit reached?)

2021-11-24 Thread Ivan Krylov
Hello Olivia!

On Tue, 23 Nov 2021 20:26:07 +
Olivia Keefer  wrote:

> We have been able to run other regressions, but when we go to run a
> certain set of variables, we both are getting this message each time
> we try.

It would help if you provided more information about how exactly you
run this regression (e.g. "I'm running `reg <- lm(y ~ X)`") and what
are the sizes of the matrices involved (e.g. "dim(X) is c(1,
1000)"). A reproducible example we could run ("When I copy&paste the
following code into my R session, I always get the error") would be
ideal, but it's not always possible.

I'm assuming you run relaimpo::boot.relimp, but with what kind of data?

(Perhaps you had attached an example, but the mailing list ate the
attachment. It eats most kinds of attachments:
https://www.r-project.org/posting-guide.html)

Adding more RAM to the computer running R would probably help. If the
code makes excessive copies of data (by taking a large variable,
changing something in it and keeping it referenced, maybe indirectly
via an environment), perhaps it could be restructured to avoid doing
that. I'm afraid it's hard to be more specific without more information.

>   [[alternative HTML version deleted]]

P.S. Your message reached us almost intact, but in order to be sure
that we'd be seeing your messages exactly as you compose them, please
compose them in plain text, not HTML.

-- 
Best regards,
Ivan

__
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] read_csv() error I cannot find

2021-11-24 Thread Ivan Krylov
On Wed, 24 Nov 2021 08:37:32 -0800 (PST)
Rich Shepard  wrote:

> Error in list(site_nbr = col_character(), sampdate = col_date(),
> param = col_character(),  : argument 6 is empty

This typically happens when you leave a trailing comma at the end of a
list() call:

list(
 site_nbr = col_character(),
 sampdate = col_date(format = "D"),
 param = col_character(),
 quant = col_double(),
 unit = col_character(), # <-- this comma causes the error
)

As for the rest of it, I'm not sure. Does read.csv() work on your file?

-- 
Best regards,
Ivan

__
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] Error: vector memory exhausted (limit reached?)

2021-11-25 Thread Ivan Krylov
On Wed, 24 Nov 2021 13:36:28 +
Olivia Keefer  wrote:

> dim(OrigData)

What is the size of OrigData? How much random-access memory does the
computer have? If there's a lot of memory, are you running a 32-bit or
a 64-bit build of R?

> linmod4 <- lm( B5 ~ B6_1 + B6_2 + B6_3 + B6_4 + B6_5 + B6_6 + B6_7 +
> B6_8 + B6_9 + B6_10 + B6_11 + B6_12 + B6_13 + B6_14 + B6_15 + B6_16 +
> B6_17 + B6_18 + B6_19 + B6_20 + B6_21 + B6_22 + B6_23 + B6_24 + B6_25
> + B6_26 + B6_27 , data= categorydata)

> f4 <-calc.relimp(linmod4,type = c("lmg","first","last"), rela = TRUE)

Which function call crashes with "vector memory exhausted", the lm()
one, the calc.relimp() one, or some other one? What does traceback()
show after the error?

-- 
Best regards,
Ivan

__
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] readxl/lifecycle/rlang

2021-12-02 Thread Ivan Krylov
On Wed, 1 Dec 2021 16:38:37 -0500
Dennis Weygand  wrote:

> Installing package into
> ‘C:/Users/dennisweygand/Documents/R/win-library/3.5’ (as ‘lib’ is
> unspecified)

It could be time to install a newer version of R. CRAN doesn't provide
binary builds of packages for R 3.5...

>   binary source needs_compilation
> rlang  0.4.5 0.4.12  TRUE
> lifecycle  0.2.0  1.0.1 FALSE

...so the latest version of rlang you can currently install is 0.4.5,
but lifecycle wants rlang (≥ 0.4.10). You can either follow the link
https://cran.rstudio.com/bin/windows/Rtools/ and install Rtools to
compile rlang 0.4.12 from source, or install a later version of R (≥
4.0) that CRAN provides Windows binaries for.

You could also try the XLConnect package (it works fine for me on
older installations of R), but it depends on Java. 

>   [[alternative HTML version deleted]]

P.S. Please compose your messages in plain text to make sure that we
see them exactly as you write them.

-- 
Best regards,
Ivan

__
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] Fwd: alternative way to define a function

2021-12-02 Thread Ivan Krylov
On Thu, 2 Dec 2021 12:23:27 +0100
Martin Møller Skarbiniks Pedersen  wrote:

> Is that exactly the same as:
> f <- function(x,y) x * y
> ?
> 
> Is there any benefit to the first or second way to define a function?

The \(arguments) syntax has been introduced in R 4.1.0:
https://cran.r-project.org/doc/manuals/r-release/NEWS.html (search for
"\(x)"). It is the same as function(arguments).

The only benefit is slightly less typing; could be useful if you use
the native R pipe (also introduced in R 4.1.0, search for "|>") and
want to call an anonymous function in the right-hand side of the pipe.

-- 
Best regards,
Ivan

__
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] Problem with lm Giving Wrong Results

2021-12-02 Thread Ivan Krylov
On Thu, 2 Dec 2021 14:34:42 +
"Labone, Thomas"  wrote:

> Can someone point me to the procedure for switching from the Intel
> Math Library back to the standard math library so that I can see if
> the problem is associated with using MKL?

Depends on how you have installed it. You mentioned using Linux Mint
(see also: the R-SIG-Debian mailing list), so try using
`update-alternatives` to set the value for all alternatives with "blas"
or "lapack" in their names. See `ls /etc/alternatives/ | grep -i -e blas
-e lapack` for the list.

-- 
Best regards,
Ivan

__
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] Forwarding missing arguments to the `[` method

2021-12-02 Thread Ivan Krylov
Sorry for sending an unfinished message!

On Thu, 2 Dec 2021 23:57:11 +0300
Ivan Krylov  wrote:

> Why can I forward missing i,j to built-in `[`

Why can I forward missing i,j to the built-in `[` method but not
to user-defined methods? How can I fix this? Default i and j to TRUE?
Are there less involved methods of handling my problem of subsetting
attr(x, 'foo') with the dimnames of x? Preferrably without setting
dimnames on attr(x, 'foo') itself.

-- 
Best regards,
Ivan

__
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] Forwarding missing arguments to the `[` method

2021-12-02 Thread Ivan Krylov
Hi everyone,

Suppose I've got a class 'foo' that's a matrix tagged with a vector of
length() == nrow(foo):

foo <- function(x, ...) UseMethod('foo')
foo.matrix <- function(x, y, ...) {
stopifnot(
!is.recursive(y),
length(y) == nrow(x),
length(list(...)) == 0
)
structure(x, class = 'foo', foo = y)
}

The invariant that the vector follows the rows of the matrix must be
maintained, so I write a [.foo method:

`[.foo` <- function(x, i, j, ..., drop = TRUE) {
ret <- NextMethod() # could drop dimensions
if (is.matrix(ret)) foo(
ret,
unname(setNames(attr(x, 'foo'), dimnames(x)[[1]])[i])
)
else ret
}

Note the acrobatics required to handle indexing objects of class foo by
the dimnames of the matrix. If I used foo(attr(x,'foo')[i]) instead,
indexing by dimnames would result in the correct subset of the matrix
tagged with a vector of NAs.

Now I would like to implement sub-assignment. I would like to access
the correct subset of the 'foo' vector (for example, if value is also
of class foo, I would like to check if the 'foo' attributes match
between value and x), but I don't want to copy&paste the code or create
a separate method, so I call `[.foo` instead:

`[<-.foo` <- function(x, i, j, ..., value) {
xsub <- x[i,j]
# safety checks on attr(xsub, 'foo')
NextMethod()
}

...except this doesn't quite work:

x <- foo(volcano, 1:nrow(volcano))
x[] <- 1
# Error in NextMethod() : argument "i" is missing, with no default
traceback()
# 5: NextMethod() at foo.R#15
# 4: `[.foo`(x, i, j) at foo.R#24
# 3: x[i, j] at foo.R#24
# 2: `[<-.foo`(`*tmp*`, , value = 1)
# 1: `[<-`(`*tmp*`, , value = 1)

Why can I forward missing i,j to built-in `[`

-- 
Best regards,
Ivan

__
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] Forwarding missing arguments to the `[` method

2021-12-02 Thread Ivan Krylov
On Thu, 02 Dec 2021 13:41:52 -0800
Jeff Newmiller  wrote:

> I think you need a reprex... I don't think your claim is correct as
> stated.

Sorry, let me try again. The following "works" for me in the sense of
throwing the same error on R 3.3 and R-devel:

foo <- function(x, ...) UseMethod('foo')

# foo is a matrix tagged with a vector of length() == nrow(foo)
foo.matrix <- function(x, y, ...) {
stopifnot(
!is.recursive(y),
length(y) == nrow(x),
length(list(...)) == 0
)
structure(x, class = 'foo', foo = y)
}

# the vector must be subsetted at the same time as x
`[.foo` <- function(x, i, j, ..., drop = TRUE) {
ret <- NextMethod() # could drop dimensions
if (is.matrix(ret)) foo(
ret,
unname(setNames(attr(x, 'foo'), dimnames(x)[[1]])[i])
)
else ret
}

`[<-.foo` <- function(x, i, j, ..., value) {
xsub <- x[i, j, drop = FALSE]
# TODO: some checks on xsub
NextMethod()
}

x <- foo(volcano, 1:nrow(volcano))
invisible(x[]) # doesn't stop despite missing i
x[] <- 1   # fails in `[.foo` with "i missing"
# Error in NextMethod() : argument "i" is missing, with no default
traceback()
# 9: NextMethod() at foo.R#15
# 8: `[.foo`(x, i, j, drop = FALSE) at foo.R#24
# 7: x[i, j, drop = FALSE] at foo.R#24
# 6: `[<-.foo`(`*tmp*`, , value = 1) at foo.R#30
# 5: `[<-`(`*tmp*`, , value = 1) at foo.R#30
# 4: eval(ei, envir)
# 3: eval(ei, envir)
# 2: withVisible(eval(ei, envir))
# 1: source("foo.R", echo = TRUE)

Or did you mean some other claim?

-- 
Best regards,
Ivan

__
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] Puzzled about loading the Rattle interface package...

2021-12-07 Thread Ivan Krylov
On Mon, 6 Dec 2021 19:33:25 -0500
Brian Lunergan  wrote:

> Running R 4.1.2 on Linux Mint 19.3.

> configure: error: GTK version 2.8.0 required

Thanks for mentioning your GNU/Linux distro! You need the libgtk2.0-dev
APT package installed in order to build the RGtk2 R package from
source. Alternatively, if you use the distro-built R (installed via
APT), you can install the r-cran-rgtk2 APT package and avoid building
the RGtk2 package from source.
 
Ask in the R-SIG-Debian mailing list if your problems continue.

-- 
Best regards,
Ivan

__
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] Forwarding missing arguments to the `[` method

2021-12-08 Thread Ivan Krylov
Got some progress on this, but still not sure how to continue. First, a
much more simple script reproducing the behaviour that confuses me:

foo <- function(x, ...) structure(x, class = 'foo')
`[.foo` <- function(x, i, ..., drop = TRUE) {
print(sys.call())
print(match.call())
foo(NextMethod()) # x[] should return an object of class foo
}
`[<-.foo` <- function(x, i, ..., value) {
print(sys.call())
print(match.call())
x[i, ..., drop = FALSE]
# imagine that we need to perform some checks on the
# subset of x that we're about to replace
NextMethod()
}

x <- foo(42)
x[]  # this works
x[] <- 1 # this doesn't

Now, the crucial difference between the x[] and x[] <- 1 calls is that
in the former case, the `i` argument isn't even specified:

x[]
# `[.foo`(x, )
# `[.foo`(x = x)
# [1] 42
# attr(,"class")
# [1] "foo"

While in the latter case, `i` is specified in the x[] call (but
missing):

x[] <- 1
# `[<-.foo`(`*tmp*`, , value = 1)
# `[<-.foo`(x = `*tmp*`, value = 1)
# x[i, ..., drop = FALSE]
# `[.foo`(x = x, i = i, drop = FALSE)
Error in NextMethod() : argument "i" is missing, with no default

What's the best way to forward the arguments from [<-.class methods to
the [.class methods in order to handle cases like x[] properly?

-- 
Best regards,
Ivan

__
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] Forwarding missing arguments to the `[` method

2021-12-10 Thread Ivan Krylov
On Thu, 9 Dec 2021 15:47:02 +0100
Martin Maechler  wrote:

> What you should take from there:
> Do work with *both*
> missing(drop)
> and
> nargs()
> 
> (and more)
> in order to distinguish m[i] from m[i,] etc

Thanks for the advice! `[<-.data.frame` does make for enlightening
reading. 

I've also considered editing the call, like lm() does:

`[<-.foo` <- function(x, i, ..., value) {
cl <- match.call(expand.dots = FALSE)
cl[[1L]] <- quote(`[`)
cl$value <- NULL
x.subset <- eval(cl, parent.frame())
# ... checks on x.subset ...
NextMethod()
}

...but that fails _some_ of my tests (but not others) with "object
`*tmp*` not found"). Non-standard evaluation is probably not the answer
here. I'll have to handle the forms of the call with different nargs()
manually and separately.

-- 
Best regards,
Ivan

__
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] Format dates issues with R

2021-12-14 Thread Ivan Krylov
On Tue, 14 Dec 2021 11:54:08 +0100
Luigi Marongiu  wrote:

> "9/29/2021"

> format = "%d/%m/%y"

> Why the conversion did not work?

The according to the format, the date above is 9th of month No. 29 in
the year 2020, followed by junk characters "21". Swap %m and %d to make
them follow the actual order and use %Y to parse the 4-digit year
instead of 2-digit year.

-- 
Best regards,
Ivan

__
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 For Windows - Apache Log4J Vulnerability Inquiry

2021-12-14 Thread Ivan Krylov
On Tue, 14 Dec 2021 14:37:47 +
"Franklin, Mark via R-help"  wrote:

> Would you be able to confirm if R for Windows v3.1.1 is impacted by
> this vulnerability?

R itself isn't written in Java, so it cannot, but the third-party Java
code that you might be calling using rJava might be.

Bob Rudis has been very kind to scan the CRAN [*] looking for packages
written in Java that might bundle the vulnerable version of log4j, and
didn't find any, but your environment may contain different versions of
packages from different sources, and those might still be vulnerable.

There could be other vulnerabilities in R v3.1.1, some of them fixed
since 2014.

-- 
Best regards,
Ivan

[*] https://stat.ethz.ch/pipermail/r-package-devel/2021q4/007589.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] transformTukey

2021-12-15 Thread Ivan Krylov
On Wed, 15 Dec 2021 05:11:47 -0800
Thomas Subia via R-help  wrote:

> data = read.delim("clipboard")

What type of value does read.delim return? Use str() to find out.

> > library(rcompanion)
> > trans_data <- transformTukey(data)  
> 
> Error in is.infinite(TRANS) : 
>   default method not implemented for type 'list'

What type of value does transformTukey expect in the "x" argument? See
help(transformTukey).

Most likely, your "data" is a data.frame, and transformTukey() expects a
vector, requiring you to extract the "Flatness" component from the
data.frame using the `[` or the `$` operator.

-- 
Best regards,
Ivan

__
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] Need older version of R

2021-12-15 Thread Ivan Krylov
On Wed, 15 Dec 2021 17:40:15 +0530
TALARI PRAVEEN  wrote:

> I am a Linux ubuntu user and I want to install an older version of r
> 4.0.2.

Try asking in R-SIG-Debian , but if all
else fails, you could always download
https://cran.r-project.org/src/base/R-4/R-4.0.2.tar.gz and build it
from source:
https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-Unix_002dalikes

-- 
Best regards,
Ivan

__
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] for loop question in R

2021-12-22 Thread Ivan Krylov
On Wed, 22 Dec 2021 16:58:18 + (UTC)
Kai Yang via R-help  wrote:

> mpg %>%    filter(hwy <35) %>%     ggplot(aes(x = displ, y = y[i],
> color = c[i])) +     geom_point()

Your code relies on R's auto-printing, where each line of code executed
at the top level (not in loops or functions) is run as if it was
wrapped in print(...the rest of the line...).

Solution: make that print() explicit.

A better solution: explicitly pass the plot object returned by the
ggplot functions to the ggsave() function instead of relying on the
global state of the program.

> ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units =
> "in")

When you type "c:/temp/f[i].jpg", what do you get in return?

Use paste0() or sprintf() to compose strings out of parts.

>   [[alternative HTML version deleted]]

P.S. Please compose your messages in plain text, not HTML. See the
R-help posting guide for more info.

-- 
Best regards,
Ivan

__
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] How to modify object's code living in some environment?

2021-12-27 Thread Ivan Krylov
On Mon, 27 Dec 2021 14:06:49 +0100
Grzegorz Smoliński  wrote:

> I know it is possible to find the environment in which some object
> lives using the 'environment()' function

That's not exactly what environment() does. This function returns the
environment belonging to a function (where it searches for its
variables), not where it's defined. A function may have many names, but
only one "own" environment (plus its parent, plus the parent's parent
and so on).

You'll need to modify some other environment, where the function
actually lives, to make this work.

Is the shiny app a package? Perhaps trace() could help you? Modifying
package environments is even trickier.

-- 
Best regards,
Ivan

__
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] How to modify object's code living in some environment?

2021-12-27 Thread Ivan Krylov
On Mon, 27 Dec 2021 15:36:01 +0100
Grzegorz Smoliński  wrote:

> write on the beginning of the body of this chosen function
> 'browser()'. 

Is your desired result similar to what debug() (or debugonce()) does?
Perhaps you won't even need to construct a call and evaluate it in a
given environment for it to work.

-- 
Best regards,
Ivan

__
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] mixture univariate distributions fit

2021-12-31 Thread Ivan Krylov
On Fri, 31 Dec 2021 07:59:11 +
PIKAL Petr  wrote:

> x <- (0:100)/100
> y1 <- dnorm((x, mean=.3, sd=.1)
> y2 <- dnorm((x, mean=.7, sd=.1)
> ymix <- ((y1+2*y2)/max(y1+2*y2))
 
> My question is if there is some package or function which could get
> those values ***directly from x and ymix values***, which is
> basically what is measured in my case.

Apologies if I'm missing something, but, this being a peak fitting
problem, shouldn't nls() (or something from the minpack.lm or nlsr
packages) work for you here?

minpack.lm::nlsLM(
 ymix ~ a1 * dnorm(x, mu1, sigma1) + a2 * dnorm(x, mu2, sigma2),
 start = c(a1 = 1, mu1 = 0, sigma1 = 1, a2 = 1, mu2 = 1, sigma2 = 1),
 lower = rep(0, 6) # help minpack avoid NaNs
)
# Nonlinear regression model
#  model: ymix ~ a1 * dnorm(x, mu1, sigma1) + a2 * dnorm(x, mu2, sigma2)
#  data: parent.frame()
#  a1mu1 sigma1 a2mu2 sigma2
#  0.1253 0.3000 0.1000 0.2506 0.7000 0.1000
# residual sum-of-squares: 1.289e-31
# 
# Number of iterations to convergence: 23 
# Achieved convergence tolerance: 1.49e-08

(Some nonlinear least squares problems will be much harder to solve
though.)

-- 
Best regards,
Ivan

__
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] Error: if statement: missing value where TRUE/FALSE needed

2022-01-05 Thread Ivan Krylov
On Wed, 5 Jan 2022 15:47:07 + (UTC)
Chuck Coleman via R-help  wrote:

> I then made the browser() statement conditional to stop execution if
> a missing argument were encountered and detectspikes() happily passed
> it and crashed.

How exactly do you check for a missing argument?

Could options(error = recover) shed some more light on the situation?
This way a crash happens first, then you get to use browser() to take a
look at the environment at the time of the crash.

-- 
Best regards,
Ivan

__
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] Error: if statement: missing value where TRUE/FALSE needed

2022-01-05 Thread Ivan Krylov
On Wed, 5 Jan 2022 21:22:37 +0300
Ivan Krylov  wrote:

> How exactly do you check for a missing argument?

To answer my own question, the check is

>> browser(expr = {
>>  is.na(w1) | is.na(ub) | is.na(m) | is.na(wi) | is.na(lb)
>> })

I think that there are NAs in xseg, which I don't see a check for. In
particular, a few lines above, there's

>> md = median( xseg, na.rm = TRUE);
>> if (robust) sdd = mad(xseg,na.rm = TRUE)
>> else sdd = sd(xseg,na.rm = TRUE)

...which seems to suggest they are expected to happen there.

Not sure which advice to give regarding the choice of file hosting
service. On the one hand, SourceForge lets you upload a whole directory
of files in arbitrary formats, but isn't very convenient do download
more than one file from. On the other hand, "pastebin" services like
http://paste.debian.net/ or http://paste.org.ru/ let us browse the code
right away, without having to look at the "Your download will start
shortly..." screen, but don't host *.RData or more than one file per
link. I guess that some other code hosting service like
SourceHut/GitHub/GitLab/Gitea/... could fulfil both criteria.

-- 
Best regards,
Ivan

__
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] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed

2022-01-26 Thread Ivan Krylov
On Wed, 26 Jan 2022 14:47:16 +0100
javed khan  wrote:

> Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed

> var <- ifelse(test$operator == 'T14', 1, 0)

The error must be in a place different from your test$operator
comparison. Have you tried traceback() to get the call stack leading to
the error? Or options(error = recover) to land in a debugger session
the moment an uncaught error happens? (Use options(error = NULL) to go
back to the default behaviour.)

Unrelated: var <- test$operator == 'T14' will also give you an
equivalent logical vector with a bit less work.

-- 
Best regards,
Ivan

__
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] Funky calculations

2022-02-01 Thread Ivan Krylov
On Tue, 1 Feb 2022 20:45:40 +0700
Nathan Boeger  wrote:

> I found something strange and maybe I am going nuts but this does not
> make sense:
> 
> >  (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1  
> [1] TRUE

Unfortunately, this always happens when computers approximate real
numbers with fractions in binary. See R FAQ 7.31 (RShowDoc('FAQ') or
).

-- 
Best regards,
Ivan

__
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] Using .Fortran in R - how can I use file i/o flexibly OR arrays of variable length as arguments?

2022-02-05 Thread Ivan Krylov
On Sat, 5 Feb 2022 09:41:02 +
Peter Green  wrote:

> Any thought on the other route I suggested, that is, binary file i/o 
> from with the Fortran code (e.g. full use of the connections 
> functionality), again perhaps via C?

I think that Fortran I/O is only forbidden when it comes to units * and
6. It should be allowed to open a unit, use unformatted I/O to write an
array into it and readBin() back on the R side.

On the other hand, the use of connections means that we're calling into
R, which means that we have to interact with the garbage collector. It's
easier to just return the values to R in that case.

> I wondered whether you (or anyone) thought there was a way to do this 
> using C wrapper routines in some way?

I've never done this myself, but I think that the following should work:

/*
 * We'll need a single pointer to everything returned by the function,
 * so we'll store the outputs in a struct. If the function returns only
 * one allocated pointer, we don't need this structure.
 */
struct calculation_result {
 double *a, *b, *c; // ...
};

/* See WRE 5.13 */
static void free_calculation_result(SEXP * ptr) {
 struct calculation_result * res = R_ExternalPtrAddr(ptr);
 if (!res) return;
 /*
  * Call the Fortran function to deallocate a, b, c here, using C
  * interoperability or the F77_CALL macro.
  */
 Free(res);
 R_ClearExternalPtr(ptr);
}

/* See WRE 5.10.1 */
SEXP run_calculation(SEXP arg1, SEXP arg2, ...) {
 /*
  * Allocate the result store and make it known to R, including how to
  * dispose of it.
  */
 SEXP sres = PROTECT(R_MakeExternalPtr(NULL, R_NilValue, R_NilValue));
 R_RegisterCFinalizerEx(sres, free_calculation_result, TRUE);
 /* NOTE: if Calloc fails, it longjmp()s instead of returning NULL */
 struct calculation_result * res = Calloc(1, struct calculation_result);
 R_SetExternalPtrAddr(sres, res);

 /*
  * Call the Fortran function to perform the calculation here, store the
  * pointers it returns inside the res structure.
  */

 /*
  * It is now safe to call into R again. If an error happens or we're
  * interrupted, free_calculation_result will prevent a memory leak.
  * Allocate a result object (e.g. a named list) and store the data from
  * `res` in R vectors.
  */

 UNPROTECT(as many as PROTECT() calls above);
 return the_result_list;
}

Alternatively, provide some place with a static lifetime for the
pointers to the calculation results (put them in a COMMON block with
the SAVE attribute?) and split the code into three functions:

1. Perform the calculations and remember the pointers in variables
   with static lifetime; return the sizes of the arrays.
2. Copy the data from the COMMON pointers to the provided arrays.
3. Deallocate the Fortran-owned COMMON arrays.

Then the R wrapper should do the following:

on.exit(.Fortran('free_fortran_arrays'))
sizes <- .Fortran('perform_calculations', sizes = integer(k))$sizes
result <- .Fortran(
 'copy_fortran_arrays',
 a = double(sizes[1]), b = double(sizes[2]), ...
)

If R is interrupted or has an allocation failure between
perform_calculations and copy_fortran_arrays, the destructor still runs
and prevents resource leaks. This makes the code non-reentrant, but so
is R itself.

A third solution would be to abstract the Fortran code behind a C++
class and use the magic of Rcpp to return the data as R objects while
keeping the promise that the destructor eventually runs. This is more
or less the same as the first solution, but most of the work is done by
Rcpp.

Now that we both had to mention Writing R Extensions, I think we're
firmly in the  territory.

-- 
Best regards,
Ivan

__
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] Question About lm()

2022-02-09 Thread Ivan Krylov
On Wed, 9 Feb 2022 22:00:40 +
"Bromaghin, Jeffrey F via R-help"  wrote:

> These models are equivalent and the estimated coefficients come out
> fine, but the R-squared and F statistics returned by summary() differ
> markedly.

Is the mean of yResp far from zero? Here's what summary.lm says about
that:

>> r.squared: R^2, the ‘fraction of variance explained by the model’,
>> 
>>   R^2 = 1 - Sum(R[i]^2) / Sum((y[i] - y*)^2),
>> 
>>where y* is the mean of y[i] if there is an intercept and
>>zero otherwise.

-- 
Best regards,
Ivan

__
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] gstat installation problem

2022-02-17 Thread Ivan Krylov
On Wed, 16 Feb 2022 15:20:48 +0100
Poizot Emmanuel  wrote:

> I tried to install gstat package via install.packages('gstat')
> command on R (version 3.5.2) and my OS is Debian buster.

> mtrx.c: In function ‘CHfactor’:
> mtrx.c:391:74: error: ‘FC_LEN_T’ undeclared (first use in this 
> function); did you mean ‘FD_SET’?
> F77_CALL(dpotrf)("Upper", (int *)&(m->n), m->v, (int *)&(m->n), 
> info, (FC_LEN_T) 5);

The gstat package doesn't declare a correct R version dependency, but
it does use a feature that appeared in a newer version of R. (3.6.2, I
think?)

Try installing older versions of gstat from
 until you find
one that's working. Alternatively, install a newer version of R from
.

-- 
Best regards,
Ivan

__
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] Pixel Image Reshaping using R

2022-02-24 Thread Ivan Krylov
On Thu, 24 Feb 2022 11:00:08 -0500
Paul Bernal  wrote:

> Each pixel column in the training set has a name like pixel x, where
> x is an integer between 0 and 783, inclusive. To locate this pixel on
> the image, suppose that we have decomposed x as x = i ∗ 28 + j, where
> i and j are integers between 0 and 27, inclusive. 

> I have been looking for information about how to process this with R,
> but have not found anything yet.

Given a 784-element vector x, you can reshape it into a 28 by 28 matrix:

dim(x) <- c(28, 28)

Or create a new matrix: matrix(x, 28, 28)

Working with more dimensions is also possible. A matrix X with dim(X)
== c(n, 784) can be transformed into a three-way array in place or
copied into one:

dim(X) <- c(dim(X)[1], 28, 28)
array(X, c(dim(X)[1], 28, 28))

(Replace 28,28 with 784 for an inverse transformation. In modern
versions of R, two-way arrays are more or less the same as matrices,
but old versions may disagree with that in some corner cases.)

For more information, see ?dim, ?matrix, ?array.

-- 
Best regards,
Ivan

__
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] Pixel Image Reshaping using R

2022-02-24 Thread Ivan Krylov
On Thu, 24 Feb 2022 13:31:09 -0500
Paul Bernal  wrote:

> Basically, what I am being asked to do is to take the
> train.csv dataset, and store it in a data structure so that the data
> can be reshaped into a matrix of size 28 x 28, then I just need to
> print some indices of that (e.g. index 1, 2, 4, 7,, etc.)

Is this homework? It's considered a good idea to contact your
instructor with homework questions.

> dataframe_train <- array(read.csv(file_path_2, header=TRUE,
> stringsAsFactors = FALSE), 28, 28)

Almost there. Two problems left:

1. You did not remove the first column. R will do what is asked of it
and mix the labels with the pixel values. You will probably need to
subset the data frame that read.csv() returns to remove it before
reshaping the rest of it into a three-way array.

3. The dimensions of the array should be specified in a single
3-element vector: c(N, 28, 28). I don't think there's a convenient way
to do that in a single expression. Once you have your pixels in an N by
784 matrix, use c(nrow(your.matrix), 28, 28) to construct the
dimension vector.

> then printing out several indices by doing (I know this could be done
> with a for loop better but I tried this):

> print(dataframe_train[1])

That chooses the N'th pixel value from the whole array. The syntax to
extract whole slices of an array is different:
https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Array-indexing

-- 
Best regards,
Ivan

__
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] arrow keys in R

2022-02-26 Thread Ivan Krylov
On Thu, 24 Feb 2022 16:14:49 -0800
Bogdan Tanasa  wrote:

> On one hand, the symbols ^[[A^[[A^[[A appear;

> On the other hand, shall I start typing a command, such as "library",
> I begin by typing the first 2 letters "li", click "left arrow", and
> the result is "li "(i.e. lots of spaces) instead of having the command
> "library" written on the screen.

Did you mean Tab here? I.e. you type "libr", you press Tab, and R
autocompletes that to "library".

Sounds like readline and/or terminal problems. How was R compiled and
installed on this system? What terminal are you running R in, and is the
$TERM variable set correctly? Does the target system have the correct
termcap definitions for this terminal?

Perhaps https://cran.r-project.org/doc/manuals/r-release/R-admin.html
could be of some help to you.

-- 
Best regards,
Ivan

__
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] saving png images and X11

2022-02-26 Thread Ivan Krylov
On Fri, 25 Feb 2022 04:33:38 -0800
Bogdan Tanasa  wrote:

> Error in png_dev(..., res = dpi, units = "in") : X11 is not available
> 
> Shall I add the option :
> 
> options(bitmapType='cairo')
> 
> the png images are skipped and not saved on the disk.

Is this a question or a statement that png('file.png', type = 'cairo');
plot(1,1); dev.off() doesn't produce any files for you?

What's the output of capabilities() and sessionInfo() in this build of
R?

>   [[alternative HTML version deleted]]

P.S. Please compose your messages in plain text. Since this mailing
list strips the HTML parts of your messages, we only get the plain text
part automatically produced by your mailer, some of it mangled quite a
bit.

-- 
Best regards,
Ivan

__
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] Problem installing Rcmdr on version 4.1.2...

2022-03-04 Thread Ivan Krylov
On Fri, 4 Mar 2022 08:23:43 -0500
Brian Lunergan  wrote:

> Running R 4.1.2 on Linux Mint 19.3.

> g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG -I../inst/include
> -I'/home/brian/R/x86_64-pc-linux-gnu-library/4.1/testthat/include'
> -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-J7pprH/r-base-4.1.2=.
> -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time
> -D_FORTIFY_SOURCE=2 -g  -c test-runner.cpp -o test-runner.o
> g++ -std=gnu++11 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions
> -Wl,-z,relro -o nloptr.so init_nloptr.o nloptr.o test-C-API.o
> test-runner.o -llapack -lblas -lgfortran -lm -lquadmath -Lnlopt/lib
> -lnlopt -L/usr/lib/R/lib -lR
> /usr/bin/ld: cannot find -lnlopt
> collect2: error: ld returned 1 exit status

Typically, when an R package wraps a third-party library, you need a
development version of it installed in order to install that package
from source.

If you're running R from the Linux Mint repos, try to install
r-cran-nloptr from Linux Mint repositories. If you don't, or have some
trouble installing the package, install the libnlopt-dev Linux Mint
package before trying to install the nloptr R package.

-- 
Best regards,
Ivan

__
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] Hello

2022-03-08 Thread Ivan Krylov
On Tue, 8 Mar 2022 15:55:19 +0530
Shrinivas Dharma  wrote:

> I am not sure if this is the right place to ask the question of my
> type.

> I am wirking on a social network analysis project with R igraph
> software.
> 
> My graph data has multiple edges and multiple self loops

Have you tried the igraph forum? It's probably a better fit because
it's more likely to have people with igraph experience:
https://igraph.discourse.group/c/usage/11

-- 
Best regards,
Ivan

__
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] NA's in segmented

2022-03-10 Thread Ivan Krylov
Hi Mélina,

If you don't get an answer here, consider running maintainer(segmented)
and contacting the e-mail address shown by the command.

On Tue, 8 Mar 2022 20:44:35 +
Mélina Cointe  wrote:

> I have some trouble with the slope() function of segmented. When I
> plot the predicted value everything seems to have worked well. But
> when I use slope(), the slope for the first segment is a line with 0
> and Nas_ Also I can get a negative slope whereas on the graph it_s
> clearly a positive slope_

It would help if you show us a small sample of your data. Use dput() to
produce a plain text representation of it. Otherwise it's next to
impossible to reproduce the problems you're having.

> [[alternative HTML version deleted]]

When you post in HTML, the list removes the HTML version for safety
reasons, and all we're left with is the plain text version
automatically prepared by your mailer. This plain text version can be
severely mangled and hard to understand. Please post in plain text:
http://www.R-project.org/posting-guide.html

-- 
Best regards,
Ivan

__
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] question

2022-03-15 Thread Ivan Krylov
On Tue, 15 Mar 2022 20:57:03 +0330
"dalfardi.s"  wrote:

> I have a function of a package named priority flow as bellow: 
> 
> RIVSMOOTH=RIVERSMOOTH(DEM=TRAVHS$DEM, DIRECTION=TRAVHS$DIRECTION,
> MASK=FARWATERSHED.MASK,RIVER.SUMMARY=SUBBASIN$SUMMARY,RIVER.SEGMENTS=SUBBASIN$SEGMENTS,BANK.EPSILON
> =0.01,RIVER.EPSILON = 0,D4=C(1,2,3,4),PRINTFLAG = T) 
> 
> and all needed inputs are available but when i run it give me the
> bellow error: 
> 
> [1] "No terminal river segments provided, not adjusting DEM"
> Error in RiverSmooth(dem = travHS$dem, direction = travHS$direction,
> mask = FARwatershed.mask, :
> object 'active' not found 

This could be a bug in the RiverSmooth function. It could be trying to
access a variable called "active" without defining it first. Where did
you get this function? It's not anywhere on CRAN, as far as I can tell:
https://search.r-project.org/?P=RiverSmooth&DB=r-manuals&DB=r-help&DB=cran-help

>   [[alternative HTML version deleted]]

As you can see in my quoted part and in the archive at
, the
code in your message as we see it is somehow in ALL CAPS. When you
compose your messages to this list in HTML, your mailer tries to
automatically create a plain text version, typically mangling it in
process, while the list strips the HTML part for security reasons.
Please compose your messages in plain text. 

-- 
Best regards,
Ivan

__
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 and tcltk Package

2022-03-17 Thread Ivan Krylov
On Thu, 17 Mar 2022 11:13:02 -0400
John Fox  wrote:

> James Wettenhall used to maintain a nice set of R Tcl/Tk examples,
> but I can't find them now.

The Internet Archive seems to remember them, with the last working
snapshot being from 2013:
http://web.archive.org/web/20130619032200/http://bioinf.wehi.edu.au:80/~wettenhall/RTclTkExamples/

-- 
Best regards,
Ivan

__
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.


  1   2   3   4   5   6   >