Re: [R] Extracting Comments from Functions/Packages

2021-10-07 Thread Enrico Schumann
On Thu, 07 Oct 2021, Leonard Mada via R-help writes:

> Dear R Users,
>
>
> I wrote a minimal parser to extract strings and
> comments from the function definitions.
>
>
> The string extraction works fine. But there are no comments:
>
> a.) Are the comments stripped from the compiled packages?
>
> b.) Alternatively: Is the deparse() not suited for this task?
>
> b.2.) Is deparse() parsing the function/expression itself?
>
> [see code for extract.str.fun() function below]
>
>
> ### All strings in "base"
> extract.str.pkg("base")
> # type = 2 for Comments:
> extract.str.pkg("base", type=2)
> extract.str.pkg("sp", type=2)
> extract.str.pkg("NetLogoR", type=2)
>
> The code for the 2 functions (extract.str.pkg &
> extract.str.fun) and the code for the parse.simple()
> parser are below.
>
>
> Sincerely,
>
>
> Leonard
>
> ===
>
> The latest code is on GitHub:
>
> https://github.com/discoleo/R/blob/master/Stat/Tools.Formulas.R
>
>
> ### Code to process functions in packages:
> extract.str.fun = function(fn, pkg, type=1, strip=TRUE) {
>     fn = as.symbol(fn); pkg = as.symbol(pkg);
>     fn = list(substitute(pkg ::: fn));
>     # deparse
>     s = paste0(do.call(deparse, fn), collapse="");
>     npos = parse.simple(s);
>     extract.str(s, npos[[type]], strip=strip)
> }
> extract.str.pkg = function(pkg, type=1, exclude.z = TRUE, strip=TRUE) {
>     nms = ls(getNamespace(pkg));
>     l = lapply(nms, function(fn) extract.str.fun(fn,
> pkg, type=type, strip=strip));
>     if(exclude.z) {
>         hasStr = sapply(l, function(s) length(s) >= 1);
>         nms = nms[hasStr];
>         l = l[hasStr];
>     }
>     names(l) = nms;
>     return(l);
> }
>
> ### minimal Parser:
> # - proof of concept;
> # - may be useful to process non-conformant R "code", e.g.:
> #   "{\"abc\" + \"bcd\"} {FUN}"; (still TODO)
> # Warning:
> # - not thoroughly checked &
> #   may be a little buggy!
>
> parse.simple = function(x, eol="\n") {
>     len = nchar(x);
>     n.comm = list(integer(0), integer(0));
>     n.str  = list(integer(0), integer(0));
>     is.hex = function(ch) {
>         # Note: only for 1 character!
>         return((ch >= "0" && ch <= "9") ||
>             (ch >= "A" && ch <= "F") ||
>             (ch >= "a" && ch <= "f"));
>     }
>     npos = 1;
>     while(npos <= len) {
>         s = substr(x, npos, npos);
>         # State: COMMENT
>         if(s == "#") {
>             n.comm[[1]] = c(n.comm[[1]], npos);
>             while(npos < len) {
>                 npos = npos + 1;
>                 if(substr(x, npos, npos) == eol) break;
>             }
>             n.comm[[2]] = c(n.comm[[2]], npos);
>             npos = npos + 1; next;
>         }
>         # State: STRING
>         if(s == "\"" || s == "'") {
>             n.str[[1]] = c(n.str[[1]], npos);
>             while(npos < len) {
>                 npos = npos + 1;
>                 se = substr(x, npos, npos);
>                 if(se == "\\") {
>                     npos = npos + 1;
>                     # simple escape vs Unicode:
>                     if(substr(x, npos, npos) != "u") next;
>                     len.end = min(len, npos + 4);
>                     npos = npos + 1;
>                     isAllHex = TRUE;
>                     while(npos <= len.end) {
>                         se = substr(x, npos, npos);
>                         if( ! is.hex(se)) { isAllHex = FALSE; break; }
>                         npos = npos + 1;
>                     }
>                     if(isAllHex) next;
>                 }
>                 if(se == s) break;
>             }
>             n.str[[2]] = c(n.str[[2]], npos);
>             npos = npos + 1; next;
>         }
>         npos = npos + 1;
>     }
>     return(list(str = n.str, comm = n.comm));
> }
>
>
> extract.str = function(s, npos, strip=FALSE) {
>     if(length(npos[[1]]) == 0) return(character(0));
>     strip.FUN = if(strip) {
>             function(id) {
>                 if(npos[[1]][[id]] + 1 < npos[[2]][[id]]) {
>                     nStart = npos[[1]][[id]] + 1;
>                     nEnd = npos[[2]][[id]] - 1; # TODO:
> Error with malformed string
>                     return(substr(s, nStart, nEnd));
>                 } else {
>                     return("");
>                 }
>             }
>         } else function(id) substr(s, npos[[1]][[id]], npos[[2]][[id]]);
>     sapply(seq(length(npos[[1]])), strip.FUN);
> }
>

On a.) There is an option "keep.source" that controls
   this behaviour. When you install a package via
   R CMD INSTALL, you can specify the option; see
   R CMD INSTALL --help .

There is also the "remindR" package on CRAN which
(I think) does something similar.


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

__
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

Re: [R] How to use ifelse without invoking warnings

2021-10-07 Thread John Fox

Dear Ravi,

It's already been suggested that you could disable warnings, but that's 
risky in case there's a warning that you didn't anticipate. Here's a 
different approach:


> kk <- k[k >= -1 & k <= n]
> ans <- numeric(length(k))
> ans[k > n] <- 1
> ans[k >= -1 & k <= n] <- pbeta(p, kk + 1, n - kk, lower.tail=FALSE)
> ans
[1] 0.0 0.006821826 0.254991551 1.0

BTW, I don't think that you mentioned that p = 0.3, but that seems 
apparent from the output you showed.


I hope this helps,
 John

--
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

On 2021-10-07 12:29 p.m., Ravi Varadhan via R-help wrote:

Hi,
I would like to execute the following vectorized calculation:

   ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), ifelse 
(k < -1, 0, 1) )

For example:



k <- c(-1.2,-0.5, 1.5, 10.4)
n <- 10
ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < 
-1, 0, 1) )

Warning message:
In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced

print(ans)

[1] 0.0 0.006821826 0.254991551 1.0

The answer is correct.  However, I would like to eliminate the annoying 
warnings.  Is there a better way to do this?

Thank you,
Ravi


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to use ifelse without invoking warnings

2021-10-07 Thread Sarah Goslee
Bert's approach is much less risky!

On Thu, Oct 7, 2021 at 1:37 PM Bert Gunter  wrote:
>
> ?suppressWarnings
>
> > p <- .05
> > k <- c(-1.2,-0.5, 1.5, 10.4)
> > n <- 10
> >
> > ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE),
> ifelse (k < -1, 0, 1) )
> Warning message:
> In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
> >
> > suppressWarnings(ans <- ifelse (k >= -1 & k <= n,
> pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < -1, 0, 1) ))
> ## no warnings
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Thu, Oct 7, 2021 at 10:21 AM Ravi Varadhan via R-help <
> r-help@r-project.org> wrote:
>
> > Hi,
> > I would like to execute the following vectorized calculation:
> >
> >   ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE),
> > ifelse (k < -1, 0, 1) )
> >
> > For example:
> >
> >
> > > k <- c(-1.2,-0.5, 1.5, 10.4)
> > > n <- 10
> > > ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE),
> > ifelse (k < -1, 0, 1) )
> > Warning message:
> > In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
> > > print(ans)
> > [1] 0.0 0.006821826 0.254991551 1.0
> >
> > The answer is correct.  However, I would like to eliminate the annoying
> > warnings.  Is there a better way to do this?
> >
> > Thank you,
> > Ravi
> >
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Sarah Goslee (she/her)
http://www.sarahgoslee.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] How to use ifelse without invoking warnings

2021-10-07 Thread Sarah Goslee
If you are positive the warnings don't matter for your application,
you can disable them: see ?options for details of warn.

But that can be dangerous, so be careful!

Sarah

On Thu, Oct 7, 2021 at 1:21 PM Ravi Varadhan via R-help
 wrote:
>
> Hi,
> I would like to execute the following vectorized calculation:
>
>   ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), 
> ifelse (k < -1, 0, 1) )
>
> For example:
>
>
> > k <- c(-1.2,-0.5, 1.5, 10.4)
> > n <- 10
> > ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse 
> > (k < -1, 0, 1) )
> Warning message:
> In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
> > print(ans)
> [1] 0.0 0.006821826 0.254991551 1.0
>
> The answer is correct.  However, I would like to eliminate the annoying 
> warnings.  Is there a better way to do this?
>
> Thank you,
> Ravi
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Sarah Goslee (she/her)
http://www.sarahgoslee.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] How to use ifelse without invoking warnings

2021-10-07 Thread Bert Gunter
?suppressWarnings

> p <- .05
> k <- c(-1.2,-0.5, 1.5, 10.4)
> n <- 10
>
> ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE),
ifelse (k < -1, 0, 1) )
Warning message:
In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
>
> suppressWarnings(ans <- ifelse (k >= -1 & k <= n,
pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < -1, 0, 1) ))
## no warnings

Bert Gunter

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


On Thu, Oct 7, 2021 at 10:21 AM Ravi Varadhan via R-help <
r-help@r-project.org> wrote:

> Hi,
> I would like to execute the following vectorized calculation:
>
>   ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE),
> ifelse (k < -1, 0, 1) )
>
> For example:
>
>
> > k <- c(-1.2,-0.5, 1.5, 10.4)
> > n <- 10
> > ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE),
> ifelse (k < -1, 0, 1) )
> Warning message:
> In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
> > print(ans)
> [1] 0.0 0.006821826 0.254991551 1.0
>
> The answer is correct.  However, I would like to eliminate the annoying
> warnings.  Is there a better way to do this?
>
> Thank you,
> Ravi
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to use ifelse without invoking warnings

2021-10-07 Thread Ravi Varadhan via R-help
Hi,
I would like to execute the following vectorized calculation:

  ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), 
ifelse (k < -1, 0, 1) )

For example:


> k <- c(-1.2,-0.5, 1.5, 10.4)
> n <- 10
> ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k 
> < -1, 0, 1) )
Warning message:
In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
> print(ans)
[1] 0.0 0.006821826 0.254991551 1.0

The answer is correct.  However, I would like to eliminate the annoying 
warnings.  Is there a better way to do this?

Thank you,
Ravi


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Extracting Comments from Functions/Packages

2021-10-07 Thread Leonard Mada via R-help

Dear R Users,


I wrote a minimal parser to extract strings and comments from the 
function definitions.



The string extraction works fine. But there are no comments:

a.) Are the comments stripped from the compiled packages?

b.) Alternatively: Is the deparse() not suited for this task?

b.2.) Is deparse() parsing the function/expression itself?

[see code for extract.str.fun() function below]


### All strings in "base"
extract.str.pkg("base")
# type = 2 for Comments:
extract.str.pkg("base", type=2)
extract.str.pkg("sp", type=2)
extract.str.pkg("NetLogoR", type=2)

The code for the 2 functions (extract.str.pkg & extract.str.fun) and the 
code for the parse.simple() parser are below.



Sincerely,


Leonard

===

The latest code is on GitHub:

https://github.com/discoleo/R/blob/master/Stat/Tools.Formulas.R


### Code to process functions in packages:
extract.str.fun = function(fn, pkg, type=1, strip=TRUE) {
    fn = as.symbol(fn); pkg = as.symbol(pkg);
    fn = list(substitute(pkg ::: fn));
    # deparse
    s = paste0(do.call(deparse, fn), collapse="");
    npos = parse.simple(s);
    extract.str(s, npos[[type]], strip=strip)
}
extract.str.pkg = function(pkg, type=1, exclude.z = TRUE, strip=TRUE) {
    nms = ls(getNamespace(pkg));
    l = lapply(nms, function(fn) extract.str.fun(fn, pkg, type=type, 
strip=strip));

    if(exclude.z) {
        hasStr = sapply(l, function(s) length(s) >= 1);
        nms = nms[hasStr];
        l = l[hasStr];
    }
    names(l) = nms;
    return(l);
}

### minimal Parser:
# - proof of concept;
# - may be useful to process non-conformant R "code", e.g.:
#   "{\"abc\" + \"bcd\"} {FUN}"; (still TODO)
# Warning:
# - not thoroughly checked &
#   may be a little buggy!

parse.simple = function(x, eol="\n") {
    len = nchar(x);
    n.comm = list(integer(0), integer(0));
    n.str  = list(integer(0), integer(0));
    is.hex = function(ch) {
        # Note: only for 1 character!
        return((ch >= "0" && ch <= "9") ||
            (ch >= "A" && ch <= "F") ||
            (ch >= "a" && ch <= "f"));
    }
    npos = 1;
    while(npos <= len) {
        s = substr(x, npos, npos);
        # State: COMMENT
        if(s == "#") {
            n.comm[[1]] = c(n.comm[[1]], npos);
            while(npos < len) {
                npos = npos + 1;
                if(substr(x, npos, npos) == eol) break;
            }
            n.comm[[2]] = c(n.comm[[2]], npos);
            npos = npos + 1; next;
        }
        # State: STRING
        if(s == "\"" || s == "'") {
            n.str[[1]] = c(n.str[[1]], npos);
            while(npos < len) {
                npos = npos + 1;
                se = substr(x, npos, npos);
                if(se == "\\") {
                    npos = npos + 1;
                    # simple escape vs Unicode:
                    if(substr(x, npos, npos) != "u") next;
                    len.end = min(len, npos + 4);
                    npos = npos + 1;
                    isAllHex = TRUE;
                    while(npos <= len.end) {
                        se = substr(x, npos, npos);
                        if( ! is.hex(se)) { isAllHex = FALSE; break; }
                        npos = npos + 1;
                    }
                    if(isAllHex) next;
                }
                if(se == s) break;
            }
            n.str[[2]] = c(n.str[[2]], npos);
            npos = npos + 1; next;
        }
        npos = npos + 1;
    }
    return(list(str = n.str, comm = n.comm));
}


extract.str = function(s, npos, strip=FALSE) {
    if(length(npos[[1]]) == 0) return(character(0));
    strip.FUN = if(strip) {
            function(id) {
                if(npos[[1]][[id]] + 1 < npos[[2]][[id]]) {
                    nStart = npos[[1]][[id]] + 1;
                    nEnd = npos[[2]][[id]] - 1; # TODO: Error with 
malformed string

                    return(substr(s, nStart, nEnd));
                } else {
                    return("");
                }
            }
        } else function(id) substr(s, npos[[1]][[id]], npos[[2]][[id]]);
    sapply(seq(length(npos[[1]])), strip.FUN);
}

__
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] Unusual Error Loading tidyverse

2021-10-07 Thread Conklin, Mike (GfK) via R-help
My experience is that the combination of OneDrive and R leads to lack of 
productivity.

--
W. Michael Conklin
EVP Marketing & Data Sciences
GfK 
M +1 612 567 8287

-Original Message-
From: R-help  On Behalf Of Kevin Thorpe
Sent: Thursday, October 7, 2021 10:50 AM
To: Jeff Newmiller 
Cc: R Help Mailing List 
Subject: Re: [R] Unusual Error Loading tidyverse



Nice fortune.

In retrospect, maybe it would have worked to re-build the user library. Things 
were acting so strangely to me I opted for the direct, if more dangerous 
approach. :-)


> On Oct 7, 2021, at 11:37 AM, Jeff Newmiller  wrote:
>
> Sad, more like.
>
> fortunes::fortune(337)
>
> You would have done just as well to delete the user library and let R prompt 
> you to re-create it if things were that bad. Note that the default R 
> configuration always looks first in the user library and only falls back to 
> the system library if the desired package is not found in the user library. 
> In most user-administered R installations you are better off acting as though 
> the system library wasn't there.
>
> On October 7, 2021 7:56:05 AM PDT, Kevin Thorpe  
> wrote:
>> I thought I would close the loop on this. It was really weird and I don’t 
>> understand everything that went on.
>>
>> First, it was indeed the case that the main library was not writeable so 
>> packages were being installed in a user library.
>>
>> Here is where it gets confusing to me. Both library paths did appear in 
>> .libPaths(). I could not figure out where that was being set since there was 
>> no user .Rprofile and Rprofile.site was not modified. To start over I got 
>> the user to delete the local library and started R as an administrator and 
>> forced installation in the main library with the lib argument. However, even 
>> with dependencies=TRUE in install.packages() everything was not getting 
>> installed. I then had the user start RStudio as an admin and use the install 
>> packages from the menu, again specifying the main library and asking for 
>> dependencies. When this was done, many additional packages were then 
>> installed that were not installed when running the native R application. 
>> Eventually, after that, tidyverse loaded (I realize it is a wrapper to load 
>> a bunch of other packages). I also had the user install rms (which we use) 
>> and again, various bits did not get installed and had to be manually 
>> requested (I don’t remember which ones).
>>
>> Anyway, in the end we got his system functioning. I realize that running as 
>> admin to install packages is probably not best practice, but it was the only 
>> way I saw to get things working. I _think_ some of the problems were because 
>> his home directory is synced with OneDrive and the user library path was to 
>> a OneDrive folder.
>>
>> I am often shocked by the difficulties students have installing packages. 
>> They manage to get errors that I have never seen in all my user of using R. 
>> On a Win10 box of my own, I installed R and packages with no difficulties 
>> so, naturally am surprised when things go this haywire with an installation.
>>
>> That’s my story. Hope it was entertaining. :-)
>>
>> Kevin
>>
>>
>>> On Sep 24, 2021, at 3:26 PM, Duncan Murdoch  
>>> wrote:
>>>
>>> It is worth checking that the library where things were most recently 
>>> installed is the first place R looks, i.e. the first entry in .libPaths().  
>>> Often R is installed by an administrator, and users can't write to the main 
>>> library, so when they install packages they go somewhere else.  If 
>>> "somewhere else" isn't first in .libPaths(), R won't see the new installs.
>>>
>>> Duncan Murdoch
>>>
>>> On 24/09/2021 2:04 p.m., Kevin Thorpe wrote:
 I did try installing xml2 and it appeared to complete. I will ask him to 
 try again and send me the output.
> On Sep 24, 2021, at 1:58 PM, Jeff Newmiller  
> wrote:
>
> Seems like they should install the xml2 package before proceeding to load 
> whatever (tidyverse).
>
> This kind of "dependency missing" problem tends to be a recurring problem 
> particularly on Windows but in general when some deeply-embedded 
> dependency fails to load or is removed in preparation for upgrading.
>
> On September 24, 2021 10:40:41 AM PDT, Kevin Thorpe 
>  wrote:
>> Below is some output from one of my students. I have never seen this 
>> error and tried a few things (updating packages for one) but am at a 
>> loss to help further. Would appreciate suggestions that I can pass along.
>>
>> Here is the error. I tried an install.packages(“xml2”) which appeared to 
>> complete but the error persists.
>>
>>> library("tidyverse")
>> Error: package or namespace load failed for ‘tidyverse’ in 
>> library.dynam(lib, package, package.lib):
>> DLL ‘xml2’ not found: maybe not installed for this architecture?
>>
>> Here is the sessionInfo()
>>
>>> sessionInfo()
>> R 

Re: [R] Unusual Error Loading tidyverse

2021-10-07 Thread Kevin Thorpe
Nice fortune.

In retrospect, maybe it would have worked to re-build the user library. Things 
were acting so strangely to me I opted for the direct, if more dangerous 
approach. :-)


> On Oct 7, 2021, at 11:37 AM, Jeff Newmiller  wrote:
> 
> Sad, more like.
> 
> fortunes::fortune(337)
> 
> You would have done just as well to delete the user library and let R prompt 
> you to re-create it if things were that bad. Note that the default R 
> configuration always looks first in the user library and only falls back to 
> the system library if the desired package is not found in the user library. 
> In most user-administered R installations you are better off acting as though 
> the system library wasn't there.
> 
> On October 7, 2021 7:56:05 AM PDT, Kevin Thorpe  
> wrote:
>> I thought I would close the loop on this. It was really weird and I don’t 
>> understand everything that went on.
>> 
>> First, it was indeed the case that the main library was not writeable so 
>> packages were being installed in a user library.
>> 
>> Here is where it gets confusing to me. Both library paths did appear in 
>> .libPaths(). I could not figure out where that was being set since there was 
>> no user .Rprofile and Rprofile.site was not modified. To start over I got 
>> the user to delete the local library and started R as an administrator and 
>> forced installation in the main library with the lib argument. However, even 
>> with dependencies=TRUE in install.packages() everything was not getting 
>> installed. I then had the user start RStudio as an admin and use the install 
>> packages from the menu, again specifying the main library and asking for 
>> dependencies. When this was done, many additional packages were then 
>> installed that were not installed when running the native R application. 
>> Eventually, after that, tidyverse loaded (I realize it is a wrapper to load 
>> a bunch of other packages). I also had the user install rms (which we use) 
>> and again, various bits did not get installed and had to be manually 
>> requested (I don’t remember which ones).
>> 
>> Anyway, in the end we got his system functioning. I realize that running as 
>> admin to install packages is probably not best practice, but it was the only 
>> way I saw to get things working. I _think_ some of the problems were because 
>> his home directory is synced with OneDrive and the user library path was to 
>> a OneDrive folder.
>> 
>> I am often shocked by the difficulties students have installing packages. 
>> They manage to get errors that I have never seen in all my user of using R. 
>> On a Win10 box of my own, I installed R and packages with no difficulties 
>> so, naturally am surprised when things go this haywire with an installation.
>> 
>> That’s my story. Hope it was entertaining. :-)
>> 
>> Kevin
>> 
>> 
>>> On Sep 24, 2021, at 3:26 PM, Duncan Murdoch  
>>> wrote:
>>> 
>>> It is worth checking that the library where things were most recently 
>>> installed is the first place R looks, i.e. the first entry in .libPaths().  
>>> Often R is installed by an administrator, and users can't write to the main 
>>> library, so when they install packages they go somewhere else.  If 
>>> "somewhere else" isn't first in .libPaths(), R won't see the new installs.
>>> 
>>> Duncan Murdoch
>>> 
>>> On 24/09/2021 2:04 p.m., Kevin Thorpe wrote:
 I did try installing xml2 and it appeared to complete. I will ask him to 
 try again and send me the output.
> On Sep 24, 2021, at 1:58 PM, Jeff Newmiller  
> wrote:
> 
> Seems like they should install the xml2 package before proceeding to load 
> whatever (tidyverse).
> 
> This kind of "dependency missing" problem tends to be a recurring problem 
> particularly on Windows but in general when some deeply-embedded 
> dependency fails to load or is removed in preparation for upgrading.
> 
> On September 24, 2021 10:40:41 AM PDT, Kevin Thorpe 
>  wrote:
>> Below is some output from one of my students. I have never seen this 
>> error and tried a few things (updating packages for one) but am at a 
>> loss to help further. Would appreciate suggestions that I can pass along.
>> 
>> Here is the error. I tried an install.packages(“xml2”) which appeared to 
>> complete but the error persists.
>> 
>>> library("tidyverse")
>> Error: package or namespace load failed for ‘tidyverse’ in 
>> library.dynam(lib, package, package.lib):
>> DLL ‘xml2’ not found: maybe not installed for this architecture?
>> 
>> Here is the sessionInfo()
>> 
>>> sessionInfo()
>> R version 4.1.1 (2021-08-10)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>> Running under: Windows 10 x64 (build 19042)
>> 
>> Matrix products: default
>> 
>> locale:
>> [1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252 
>> LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
>> [5] 

Re: [R] Unusual Error Loading tidyverse

2021-10-07 Thread Jeff Newmiller
Sad, more like.

fortunes::fortune(337)

You would have done just as well to delete the user library and let R prompt 
you to re-create it if things were that bad. Note that the default R 
configuration always looks first in the user library and only falls back to the 
system library if the desired package is not found in the user library. In most 
user-administered R installations you are better off acting as though the 
system library wasn't there.

On October 7, 2021 7:56:05 AM PDT, Kevin Thorpe  
wrote:
>I thought I would close the loop on this. It was really weird and I don’t 
>understand everything that went on.
>
>First, it was indeed the case that the main library was not writeable so 
>packages were being installed in a user library.
>
>Here is where it gets confusing to me. Both library paths did appear in 
>.libPaths(). I could not figure out where that was being set since there was 
>no user .Rprofile and Rprofile.site was not modified. To start over I got the 
>user to delete the local library and started R as an administrator and forced 
>installation in the main library with the lib argument. However, even with 
>dependencies=TRUE in install.packages() everything was not getting installed. 
>I then had the user start RStudio as an admin and use the install packages 
>from the menu, again specifying the main library and asking for dependencies. 
>When this was done, many additional packages were then installed that were not 
>installed when running the native R application. Eventually, after that, 
>tidyverse loaded (I realize it is a wrapper to load a bunch of other 
>packages). I also had the user install rms (which we use) and again, various 
>bits did not get installed and had to be manually requested (I don’t remember 
>which ones).
>
>Anyway, in the end we got his system functioning. I realize that running as 
>admin to install packages is probably not best practice, but it was the only 
>way I saw to get things working. I _think_ some of the problems were because 
>his home directory is synced with OneDrive and the user library path was to a 
>OneDrive folder.
>
>I am often shocked by the difficulties students have installing packages. They 
>manage to get errors that I have never seen in all my user of using R. On a 
>Win10 box of my own, I installed R and packages with no difficulties so, 
>naturally am surprised when things go this haywire with an installation.
>
>That’s my story. Hope it was entertaining. :-)
>
>Kevin
>
>
>> On Sep 24, 2021, at 3:26 PM, Duncan Murdoch  wrote:
>> 
>> It is worth checking that the library where things were most recently 
>> installed is the first place R looks, i.e. the first entry in .libPaths().  
>> Often R is installed by an administrator, and users can't write to the main 
>> library, so when they install packages they go somewhere else.  If 
>> "somewhere else" isn't first in .libPaths(), R won't see the new installs.
>> 
>> Duncan Murdoch
>> 
>> On 24/09/2021 2:04 p.m., Kevin Thorpe wrote:
>>> I did try installing xml2 and it appeared to complete. I will ask him to 
>>> try again and send me the output.
 On Sep 24, 2021, at 1:58 PM, Jeff Newmiller  
 wrote:
 
 Seems like they should install the xml2 package before proceeding to load 
 whatever (tidyverse).
 
 This kind of "dependency missing" problem tends to be a recurring problem 
 particularly on Windows but in general when some deeply-embedded 
 dependency fails to load or is removed in preparation for upgrading.
 
 On September 24, 2021 10:40:41 AM PDT, Kevin Thorpe 
  wrote:
> Below is some output from one of my students. I have never seen this 
> error and tried a few things (updating packages for one) but am at a loss 
> to help further. Would appreciate suggestions that I can pass along.
> 
> Here is the error. I tried an install.packages(“xml2”) which appeared to 
> complete but the error persists.
> 
>> library("tidyverse")
> Error: package or namespace load failed for ‘tidyverse’ in 
> library.dynam(lib, package, package.lib):
> DLL ‘xml2’ not found: maybe not installed for this architecture?
> 
> Here is the sessionInfo()
> 
>> sessionInfo()
> R version 4.1.1 (2021-08-10)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 19042)
> 
> Matrix products: default
> 
> locale:
> [1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252 
> LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
> [5] LC_TIME=English_Canada.1252
> 
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
> 
> loaded via a namespace (and not attached):
> [1] Rcpp_1.0.7 cellranger_1.1.0 pillar_1.6.2 compiler_4.1.1 dbplyr_2.1.1 
> forcats_0.5.1 tools_4.1.1
> [8] jsonlite_1.7.2 lubridate_1.7.10 lifecycle_1.0.0 tibble_3.1.4 
> gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.11
> 

Re: [R] RSQLite slowness

2021-10-07 Thread Rasmus Liland
Dear Martin Morgan,

Thanks for all those links!  Yes, my 
question can be characterized like that 
I think, traditional way writing 
a temporary table into the database and 
left JOINing the others vs. 
parameterized query. 

A relevant example would be to first 
create the database from the compressed 
tsv files:

for i in gene_info gene2refseq; do
  wget -c https://ftp.ncbi.nlm.nih.gov/gene/DATA/$i.gz
  gzip -d $i.gz
  sqlite3 gene_info.sqlite ".mode tabs" ".import $i $i"
  rm $i; done

then run this R code:

H <- data.frame(Group = c(1, 1, 2, 2),
  NM = c("NM_001267695", "NM_001007636",
 "NM_001003706", "NM_001353612"))
conn <- DBI::dbConnect(RSQLite::SQLite(), "gene_info.sqlite")
DBI::dbWriteTable(conn, "H", H, overwrite=T)

statement.1 <-
 "SELECT * FROM gene2refseq R
  LEFT JOIN gene_info I ON I.GeneID = R.GeneID
  WHERE R.`RNA_nucleotide_accession.version`
LIKE '%' || ? || '%'"
time.1 <- proc.time()
x1 <- DBI::dbGetQuery(
  conn=conn,
  statement=statement.1,
  param=list(H$NM))
time.1 <- proc.time() - time.1

statement.2 <-
 "SELECT * FROM H
  LEFT JOIN gene2refseq R ON
R.`RNA_nucleotide_accession.version`
LIKE '%' || H.NM || '%'
  LEFT JOIN gene_info I ON I.GeneID = R.GeneID"
time.2 <- proc.time()
x2 <- DBI::dbGetQuery(
  conn=conn,
  statement=statement.2)
time.2 <- proc.time() - time.2

DBI::dbDisconnect(conn)

saveRDS(object=x1, file="ex1_x1.rds", compress="xz")
saveRDS(object=x2, file="ex1_x2.rds", compress="xz")
saveRDS(object=list("Time x1"=list(time.1),
"Time x2"=list(time.2)),
file="ex1_t.rds", compress="xz")

I got these timings in the ex1_t.rds 
file:

$`Time x1`
   user  system elapsed
571.731 182.006 772.199

$`Time x2`
   user  system elapsed
200.068  90.529 295.086

As you can see, statement.1 takes a lot 
longer to process compared to 
statement.2 ...  When I add the rest of 
the 31 search terms, the difference gets 
a lot bigger like I pointed out 
initially, beyond the full hour vs. only 
a few minutes. 

Best,
Rasmus


signature.asc
Description: PGP signature
__
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] Unusual Error Loading tidyverse

2021-10-07 Thread Kevin Thorpe
I thought I would close the loop on this. It was really weird and I don’t 
understand everything that went on.

First, it was indeed the case that the main library was not writeable so 
packages were being installed in a user library.

Here is where it gets confusing to me. Both library paths did appear in 
.libPaths(). I could not figure out where that was being set since there was no 
user .Rprofile and Rprofile.site was not modified. To start over I got the user 
to delete the local library and started R as an administrator and forced 
installation in the main library with the lib argument. However, even with 
dependencies=TRUE in install.packages() everything was not getting installed. I 
then had the user start RStudio as an admin and use the install packages from 
the menu, again specifying the main library and asking for dependencies. When 
this was done, many additional packages were then installed that were not 
installed when running the native R application. Eventually, after that, 
tidyverse loaded (I realize it is a wrapper to load a bunch of other packages). 
I also had the user install rms (which we use) and again, various bits did not 
get installed and had to be manually requested (I don’t remember which ones).

Anyway, in the end we got his system functioning. I realize that running as 
admin to install packages is probably not best practice, but it was the only 
way I saw to get things working. I _think_ some of the problems were because 
his home directory is synced with OneDrive and the user library path was to a 
OneDrive folder.

I am often shocked by the difficulties students have installing packages. They 
manage to get errors that I have never seen in all my user of using R. On a 
Win10 box of my own, I installed R and packages with no difficulties so, 
naturally am surprised when things go this haywire with an installation.

That’s my story. Hope it was entertaining. :-)

Kevin


> On Sep 24, 2021, at 3:26 PM, Duncan Murdoch  wrote:
> 
> It is worth checking that the library where things were most recently 
> installed is the first place R looks, i.e. the first entry in .libPaths().  
> Often R is installed by an administrator, and users can't write to the main 
> library, so when they install packages they go somewhere else.  If "somewhere 
> else" isn't first in .libPaths(), R won't see the new installs.
> 
> Duncan Murdoch
> 
> On 24/09/2021 2:04 p.m., Kevin Thorpe wrote:
>> I did try installing xml2 and it appeared to complete. I will ask him to try 
>> again and send me the output.
>>> On Sep 24, 2021, at 1:58 PM, Jeff Newmiller  
>>> wrote:
>>> 
>>> Seems like they should install the xml2 package before proceeding to load 
>>> whatever (tidyverse).
>>> 
>>> This kind of "dependency missing" problem tends to be a recurring problem 
>>> particularly on Windows but in general when some deeply-embedded dependency 
>>> fails to load or is removed in preparation for upgrading.
>>> 
>>> On September 24, 2021 10:40:41 AM PDT, Kevin Thorpe 
>>>  wrote:
 Below is some output from one of my students. I have never seen this error 
 and tried a few things (updating packages for one) but am at a loss to 
 help further. Would appreciate suggestions that I can pass along.
 
 Here is the error. I tried an install.packages(“xml2”) which appeared to 
 complete but the error persists.
 
> library("tidyverse")
 Error: package or namespace load failed for ‘tidyverse’ in 
 library.dynam(lib, package, package.lib):
 DLL ‘xml2’ not found: maybe not installed for this architecture?
 
 Here is the sessionInfo()
 
> sessionInfo()
 R version 4.1.1 (2021-08-10)
 Platform: x86_64-w64-mingw32/x64 (64-bit)
 Running under: Windows 10 x64 (build 19042)
 
 Matrix products: default
 
 locale:
 [1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252 
 LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
 [5] LC_TIME=English_Canada.1252
 
 attached base packages:
 [1] stats graphics grDevices utils datasets methods base
 
 loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7 cellranger_1.1.0 pillar_1.6.2 compiler_4.1.1 dbplyr_2.1.1 
 forcats_0.5.1 tools_4.1.1
 [8] jsonlite_1.7.2 lubridate_1.7.10 lifecycle_1.0.0 tibble_3.1.4 
 gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.11
 [15] reprex_2.0.1 DBI_1.1.1 haven_2.4.3 withr_2.4.2 dplyr_1.0.7 httr_1.4.2 
 fs_1.5.0
 [22] generics_0.1.0 vctrs_0.3.8 hms_1.1.0 grid_4.1.1 tidyselect_1.1.1 
 glue_1.4.2 R6_2.5.1
 [29] fansi_0.5.0 readxl_1.3.1 tzdb_0.1.2 tidyr_1.1.3 ggplot2_3.3.5 
 purrr_0.3.4 readr_2.0.1
 [36] modelr_0.1.8 magrittr_2.0.1 backports_1.2.1 scales_1.1.1 
 ellipsis_0.3.2 assertthat_0.2.1 colorspace_2.0-2
 [43] utf8_1.2.2 munsell_0.5.0 broom_0.7.9 crayon_1.4.1
 
>>> 
>>> -- 
>>> Sent from my phone. Please excuse my brevity.
> 

-- 
Kevin E. Thorpe
Head of Biostatistics,  Applied Health 

Re: [R] RSQLite slowness

2021-10-07 Thread Rasmus Liland
On 2021-10-06 12:11 -0700, Jeff Newmiller wrote:
> FWIW all SQL implementations work 
> better with indexes

An index seems to be a good way to 
improve sql performance, I'll look into 
it.

Best,
Rasmus


signature.asc
Description: PGP signature
__
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 Rasmus Liland
Dear Ivan,

Thanks for that explaination!  I think 
it explains the slowness clearly.

It is possible to use carray is in Rust 
[1] so it might be available in R in the 
future(?)  I'll look into rusqlite some 
time at least.

sqlite is supposed to be one of the 
fastest sql implementations.  Realm [2] 
might be faster, but I don't know how to 
use it in R so ...  

I thought someone might suggest Redis, 
but I think it is way slower.  

sqlite with indexes and the traditional 
way of doing that by writing in a 
temporary table might just be it ... 

Best,
Rasmus

[1] https://docs.rs/rusqlite/0.24.2/rusqlite/vtab/array/index.html
[2] https://en.wikipedia.org/wiki/Realm_(database)
[3] https://en.wikipedia.org/wiki/Redis


signature.asc
Description: PGP signature
__
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] adding results to plot

2021-10-07 Thread PIKAL Petr
Hallo Rui.

I finally tested your function and it seems to me that it should propagate
to the core R or at least to the stats package.

Although it is a bit overkill for my purpose, its use is straightforward and
simple. I checked it for several *test functions and did not find any
problem.

Thanks and best regards.

Petr

> -Original Message-
> From: Rui Barradas 
> Sent: Friday, September 17, 2021 9:56 PM
> To: PIKAL Petr ; r-help 
> Subject: Re: [R] adding results to plot
> 
> Hello,
> 
> *.test functions in base R return a list of class "htest", with its own
> print method.
> The method text.htest for objects of class "htest" below is a hack. I
> adapted the formating part of the code of print.htest to plot text().
> I find it maybe too complicated but it seems to work.
> 
> Warning: Not debugged at all.
> 
> 
> 
> text.htest <- function (ht, x, y = NULL, digits = getOption("digits"),
>  prefix = "", adj = NULL, ...) {
>out <- list()
>i_out <- 1L
>out[[i_out]] <- paste(strwrap(ht$method, prefix = prefix), sep = "\n")
>i_out <- i_out + 1L
>out[[i_out]] <- paste0("data:  ", ht$data.name)
> 
>stat_line <- NULL
>i_stat_line <- 0L
>if (!is.null(ht$statistic)) {
>  i_stat_line <- i_stat_line + 1L
>  stat_line[[i_stat_line]] <- paste(names(ht$statistic), "=",
>format(ht$statistic, digits =
> max(1L, digits - 2L)))
>}
>if (!is.null(ht$parameter)) {
>  i_stat_line <- i_stat_line + 1L
>  stat_line[[i_stat_line]] <- paste(names(ht$parameter), "=",
>format(ht$parameter, digits =
> max(1L, digits - 2L)))
>}
>if (!is.null(ht$p.value)) {
>  fp <- format.pval(ht$p.value, digits = max(1L, digits - 3L))
>  i_stat_line <- i_stat_line + 1L
>  stat_line[[i_stat_line]] <- paste("p-value",
>if (startsWith(fp, "<")) fp else
> paste("=", fp))
>}
>if(!is.null(stat_line)){
>  i_out <- i_out + 1L
>  #out[[i_out]] <- strwrap(paste(stat_line, collapse = ", "))
>  out[[i_out]] <- paste(stat_line, collapse = ", ")
>}
>if (!is.null(ht$alternative)) {
>  alt <- NULL
>  i_alt <- 1L
>  alt[[i_alt]] <- "alternative hypothesis: "
>  if (!is.null(ht$null.value)) {
>if (length(ht$null.value) == 1L) {
>  alt.char <- switch(ht$alternative, two.sided = "not equal to",
> less = "less than", greater = "greater than")
>  i_alt <- i_alt + 1L
>  alt[[i_alt]] <- paste0("true ", names(ht$null.value), " is ",
> alt.char,
> " ", ht$null.value)
>}
>else {
>  i_alt <- i_alt + 1L
>  alt[[i_alt]] <- paste0(ht$alternative, "\nnull values:\n")
>}
>  }
>  else {
>i_alt <- i_alt + 1L
>alt[[i_alt]] <- ht$alternative
>  }
>  i_out <- i_out + 1L
>  out[[i_out]] <- paste(alt, collapse = " ")
>}
>if (!is.null(ht$conf.int)) {
>  i_out <- i_out + 1L
>  out[[i_out]] <- paste0(format(100 * attr(ht$conf.int, "conf.level")),
> " percent confidence interval:\n", " ",
> paste(format(ht$conf.int[1:2], digits =
> digits), collapse = " "))
>}
>if (!is.null(ht$estimate)) {
>  i_out <- i_out + 1L
>  out[[i_out]] <- paste("sample estimates:", round(ht$estimate,
> digits = digits), sep = "\n")
>}
>i_out <- i_out + 1L
>out[[i_out]] <- "\n"
>names(out)[i_out] <- "sep"
>out <- do.call(paste, out)
>if(is.null(adj)) adj <- 0L
>text(x, y, labels = out, adj = adj, ...)
>invisible(out)
> }
> 
> 
> res <- shapiro.test(rnorm(100))
> plot(1,1, ylim = c(0, length(res) + 1L))
> text(res, 0.6, length(res) - 1)
> res
> 
> res2 <- t.test(rnorm(100))
> plot(1,1, ylim = c(0, length(res2) + 1L))
> text(res2, 0.6, length(res2) - 1L)
> res2
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> 
> 
> Às 15:12 de 16/09/21, PIKAL Petr escreveu:
> > Dear all
> >
> > I know I have seen the answer somewhere but I am not able to find it.
> Please
> > help
> >
> >> plot(1,1)
> >> res <- shapiro.test(rnorm(100))
> >> res
> >
> >  Shapiro-Wilk normality test
> >
> > data:  rnorm(100)
> > W = 0.98861, p-value = 0.5544
> >
> > I would like to add whole res object to the plot.
> >
> > I can do it one by one
> >> text(locator(1), res$method)
> >> text(locator(1), as.character(res$p.value))
> > ...
> > But it is quite inconvenient
> >
> > I could find some way in ggplot world but not in plain plot world.
> >
> > Best regards
> > Petr
> >
> >
> > __
> > 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] Tidygrraph example not working

2021-10-07 Thread Monica Palaseanu-Lovejoy
Hi,

I am trying to learn more about tidygraph, so i am following this blog:
https://www.data-imaginist.com/2017/introducing-tidygraph/

Few months ago the examples in the blog worked very well, but today they do
not.

I just installed R x64 4.1.1, so i should be up and running with the latest
on a Windows machine.

>From the blog:

iris_clust <- hclust(dist(iris[1:4]))
iris_tree <- as_tbl_graph(iris_clust)

iris_tree <- iris_tree %>%
activate(nodes) %>%
mutate(Species = ifelse(leaf, as.character(iris$Species)[label], NA)) %>%
activate(edges) %>%
mutate(to_setose = .N()$Species[to] == 'setosa')
iris_tree#> # A tbl_graph: 299 nodes and 298 edges#> ##> # A rooted
tree#> ##> # Edge Data: 298 x 3 (active)#>fromto to_setose#>
  #> 1 3 1  TRUE#> 2 3 2
TRUE#> 3 7 5  TRUE#> 4 7 6  TRUE#> 5 8
4  TRUE#> 6 8 7NA#> # ... with 292 more rows#> ##>
# Node Data: 299 x 5#>  height  leaf  label members Species#>
 #> 1 0.000  TRUE108   1
 setosa#> 2 0.000  TRUE131   1  setosa#> 3 0.2645751 FALSE
 2#> # ... with 296 more rows
Now from my R session:

library(tidygraph)

Attaching package: ‘tidygraph’

The following object is masked from ‘package:stats’:

filter
iris_clust <- hclust(dist(iris[1:4]))
iris_tree <- as_tbl_graph(iris_clust)

iris_tree <- iris_tree %>%
 activate(nodes) %>%
 mutate(Species = ifelse(leaf, as.character(iris$Species)[label], NA)) %>%
 activate(edges) %>%
 mutate(to_setose = .N()$Species[to] == "setosa")
 iris_tree
# A tbl_graph: 299 nodes and 298 edges
#
# A rooted tree
#
# Edge Data: 298 x 3 (active)
   fromto to_setose

1 3 1 NA
2 3 2 NA
3 7 5 NA
4 7 6 NA
5 8 4 NA
6 8 7 NA
# ... with 292 more rows
#
# Node Data: 299 x 5
  height leaf  label members Species
 
1  0 TRUE  "108"   1 
2  0 TRUE  "131"   1 
3  0.265 FALSE ""  2 
# ... with 296 more rows

As you see the attributes from the Species column are not added to the
table, and all are NA.

It is not clear to me where i made my mistake. Any help is very much
appreciated.

Thanks,

Monica

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] 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] Strange behavior of 2-d array within function

2021-10-07 Thread PIKAL Petr
Hi

I would print/save iteration number to see at what time this occured and
probably traceback() could give you some hint.
Alternatively you could make a function from your code see ?function and use
debug to trace the error.

Without some working example it is impossible to see where is the problem.

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Gabriel Toro
> Sent: Wednesday, October 6, 2021 8:32 PM
> To: r-help@r-project.org
> Subject: [R] Strange behavior of 2-d array within function
> 
> Hi,
> 
> I have a function, which defines an array of dimensions 5000 by 60,
calculates
> the values within that array and then returns the array on exit.
> 
> I get an error: Error in my_simulated[ir, 1:it] : incorrect number of
dimensions
> 
> For some strange reason, the array is somehow being changed from
>mode "numeric" and attributes $dim=6000 by 50
>to
>mode "list" and attributes NULL
> 
> This change occurs at more or less random iterations within a loop (all
within
> the same function call). I am not explicitly manipulating the mode or
> attributes of the array after it is created.
> 
> I would appreciate any suggestions on what may be causing this problem. I
> have stared at the code for a long time, run the debugger, etc.
> 
> Thanks,
> 
> Gabriel
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R-es] Respuestas múltiples en una sola columna

2021-10-07 Thread Marcelino de la Cruz Rot

Hola:
Otra posibilidad sería la que aparece a continuación.
Saludos,
Marcelino

library(readxl)
library(reshape2)
datin <- read_excel("Ejes formativos.xlsx")
datin_long <- melt(datin, id.vars=c("ID", "area_pertenencia"))[,c("ID", 
"value", "area_pertenencia")]

names(datin_long) <- c("ID", "ejes", "area")
datin_long <- datin_long[!is.na(datin_long$ejes),]





El 07/10/2021 a las 4:40, juan manuel dias escribió:

Muchas gracias a ambos!

El mié., 6 de octubre de 2021 15:52, Carlos Ortega 
escribió:


Hola,

Sí, otra alternativa para dejarlo como has comentado y leyendo
directamente de Excel...

#---

library(dplyr)
library(tidyr)
library(readr)
library(readxl)

datin <- read_excel("Ejes formativos.xlsx")

datin_long <- datin %>%
   pivot_longer(
 cols = starts_with("eje"),
 values_to = "ejes"
   ) %>%
   rename( area = area_pertenencia) %>%
   select(-name) %>%
   relocate(ejes, .before = area)

#---


Saludos,
Carlos Ortega
www.qualityexcellence.es

El mié, 6 oct 2021 a las 20:17, juan manuel dias ()
escribió:


Hola, cómo andan!

Tengo una base de una encuesta sobre formación profesional realizada a un
conjunto de empleados de una institución.

Una de las preguntas es de respuesta múltiple, eje_tem_1,eje_tem_2,
eje_tem_3hasta eje_tem_17.

Necesitaría que las respuestas de cada persona que están en esas
distintas columnas (eje_tem a eje_tem_17) queden en una misma columna, y
asimismo que se repita el "ID" y el "area_pertenencia" tantas veces como
opciones haya marcado cada caso/persona/"ID".

Estuve intentando con pivot_longer pero sin resultados!

Actualmente está así

*id  | eje_tem_1  | eje_tem_2 | eje_tem_3  | eje_tem_4area*
1 rh  sist   comunfilos
admin
2 rh  arte
  medic
3 sistmatem empre comun
  asist
4 arte   matem empre  sist
   asist

Y necesitaría que quede así:

*id  |  ejes|  area*
1 rh   admin
1 sist admin
1 comunadmin
1 filosadmin
1 admin admin
2  rh   medic
2 arte medic
..   .

Adjunto un recorte de la base!

Muchas gracias!

Saludos, Juan.
___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es



--
Saludos,
Carlos Ortega
www.qualityexcellence.es


[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es
.



--
Marcelino de la Cruz Rot
Depto. de Biología y Geología
Física y Química Inorgánica
Universidad Rey Juan Carlos
Móstoles España

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es