Re: [R] Double buffering plots on Windows

2024-03-24 Thread Paul Murrell

Hi

Take a look at dev.hold() and dev.flush()

Paul

On 23/03/24 06:43, Bickis, Mikelis wrote:

Hello:

I want to present a sequence of plots as an animation. As a toy example 
consider the code


function(n){for (i in 1:n){
plot(1:100,sin(i*(1:100)),type="l")
title(paste("n=",i))
segments(0,0,100,0,col=2)
}}

This sort-of works on a MacOS platform, but the rendering of the plots 
is a bit choppy. Inserting a sleep function allows the plots to evolve 
smoothly.


function(n){for (i in 1:n){
plot(1:100,sin(i*(1:100)),type="l")
title(paste("n=",i))
segments(0,0,100,0,col=2)
Sys.sleep(.2)
}}

However, on a Windows platform, only the last plot is rendered without 
the Sys.sleep, so the dynamic element is lost. Inserting the Sys.sleep 
does allow all the plots to be rendered, but they seem to be erased 
before they are drawn again, so there is substantial flicker in the 
appearance.


Is there some kind of double-buffering available within R, so that plots 
are rendered only after they are fully drawn, leaving the previous plot 
visible until it is replaced? I just used the default graphics driver on 
Windows — is there perhaps a different driver that will the graphics 
smoother?


Mik Bickis
Professor Emeritus
Department of Mathematics and Statistics
University of Saskatchewan
__
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.


--
Dr Paul Murrell (he/him)
Te Kura Tatauranga | Department of Statistics
Waipapa Taumata Rau | The University of Auckland
Private Bag 92019, Auckland 1142, New Zealand
64 9 3737599 x85392
p...@stat.auckland.ac.nz
www.stat.auckland.ac.nz/~paul/

__
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] geom_edge & color

2024-03-24 Thread SIBYLLE STÖCKLI via R-help
Dear Kommo

Many thanks for the valuable solution.

Sibylle

-Original Message-
From: R-help  On Behalf Of Kimmo Elo
Sent: Friday, March 22, 2024 12:10 PM
To: r-help@r-project.org
Subject: Re: [R] geom_edge & color

Hi,

this seems to work (assuming that your problem was the setting of
colours...):

--- snip ---

network %>%
ggraph(., layout = "auto") +
# This produces an error...
# geom_edge_arc(curvature=0.3, aes(width=(E(network)$weight/10), 
color=c("darkblue", "red")[as.factor(edge_list$relationship)], alpha=0.5))
+ 
# ... this works :-)
geom_edge_arc(curvature=0.3, aes(width=E(network)$weight/10, 
color=edge_list$relationship), alpha=.5) +
scale_edge_color_manual(values=c("pos"="darkblue", "neg"="red")) +

# This does not work either...
# geom_node_point(aes(size = V(network)$hub_score*200, color=
as.factor(V(network)$community))) +
# ... so try this :)
geom_node_point(aes(size = V(network)$hub_score*200,
color=V(network)$Subcategory_type)) +
geom_node_text(aes(label =  V(network)$name), size=3, color="white", 
repel=T)+
scale_color_scico_d(palette = "batlow")+
scale_edge_width(range = c(0.2,4))+
scale_size(range = c(0.5,15)) +
theme(plot.background = element_rect(fill = "black"),
  legend.position = "right",
  panel.background = element_rect(fill = "black"))

--- snip ---

At least in my data created with your code, the object "network" does not have 
an attribute "community". I use the existing "Subcategory_type"
instead, because I had no time to debug this problem :-)

I do not know whether this produces what you expect or want to visualise.

HTH,

Kimmo


pe, 2024-03-22 kello 08:59 +0100, sibylle.stoec...@gmx.ch kirjoitti:
> Dear community
> 
>  
> 
> Find enclosed the full working example.
> 
>  
> 
> Many thanks
> 
> Sibylle
> 
>  
> 
> Test_cat.csv
> 
> 
> Names
> 
> Subcategory_type
> 
> sources.cyto
> 
> source
> 
> Factor
> 
> 
> A.A
> 
> material
> 
> "A"
> 
> A
> 
> 1
> 
> 
> B.B
> 
> material
> 
> "B"
> 
> B
> 
> 1
> 
> 
> C.C
> 
> regulation
> 
> "C"
> 
> C
> 
> 1
> 
> 
> D.D
> 
> regulation
> 
> "D"
> 
> D
> 
> 1
> 
> 
> E.E
> 
> habitat
> 
> "E"
> 
> E
> 
> 1
> 
> 
> F.F
> 
> cultural
> 
> "F"
> 
> F
> 
> 1
> 
>  
> 
> Test_adjac.csv
> 
> 
> A.A
> 
> B.B
> 
> C.C
> 
> D.D
> 
> E.E
> 
> F.F
> 
> 
> A.A
> 
> 0
> 
> 0
> 
> 5
> 
> 5
> 
> 5
> 
> 5
> 
> 
> B.B
> 
> 4
> 
> 0
> 
> 1
> 
> 1
> 
> 1
> 
> 1
> 
> 
> C.C
> 
> 5
> 
> 5
> 
> 0
> 
> 5
> 
> 4
> 
> 2
> 
> 
> D.D
> 
> 5
> 
> 0
> 
> 5
> 
> 0
> 
> 5
> 
> 3
> 
> 
> E.E
> 
> 5
> 
> 1
> 
> 5
> 
> 5
> 
> 0
> 
> 4
> 
> 
> F.F
> 
> 1
> 
> 2
> 
> 3
> 
> 4
> 
> 5
> 
> 5
> 
>  
> 
>  
> 
> Edges_table-Test.csv
> 
>  
> 
> 
> Names
> 
> target
> 
> weight
> 
> relationship
> 
> 
> B.B
> 
> A.A
> 
> 4
> 
> pos
> 
> 
> C.C
> 
> A.A
> 
> 5
> 
> pos
> 
> 
> D.D
> 
> A.A
> 
> 5
> 
> neg
> 
> 
> E.E
> 
> A.A
> 
> 5
> 
> pos
> 
> 
> F.F
> 
> A.A
> 
> 1
> 
> pos
> 
> 
> C.C
> 
> B.B
> 
> 5
> 
> pos
> 
> 
> E.E
> 
> B.B
> 
> 1
> 
> pos
> 
> 
> F.F
> 
> B.B
> 
> 2
> 
> neg
> 
> 
> A.A
> 
> C.C
> 
> 5
> 
> pos
> 
> 
> B.B
> 
> C.C
> 
> 1
> 
> pos
> 
> 
> D.D
> 
> C.C
> 
> 5
> 
> pos
> 
> 
> E.E
> 
> C.C
> 
> 5
> 
> pos
> 
> 
> F.F
> 
> C.C
> 
> 3
> 
> pos
> 
> 
> A.A
> 
> D.D
> 
> 5
> 
> neg
> 
> 
> B.B
> 
> D.D
> 
> 1
> 
> pos
> 
> 
> C.C
> 
> D.D
> 
> 5
> 
> pos
> 
> 
> E.E
> 
> D.D
> 
> 5
> 
> pos
> 
> 
> F.F
> 
> D.D
> 
> 4
> 
> pos
> 
> 
> A.A
> 
> E.E
> 
> 5
> 
> pos
> 
> 
> B.B
> 
> E.E
> 
> 1
> 
> pos
> 
> 
> C.C
> 
> E.E
> 
> 4
> 
> pos
> 
> 
> D.D
> 
> E.E
> 
> 5
> 
> pos
> 
> 
> F.F
> 
> E.E
> 
> 5
> 
> pos
> 
> 
> A.A
> 
> F.F
> 
> 5
> 
> pos
> 
> 
> B.B
> 
> F.F
> 
> 1
> 
> neg
> 
> 
> C.C
> 
> F.F
> 
> 2
> 
> pos
> 
> 
> D.D
> 
> F.F
> 
> 3
> 
> pos
> 
> 
> E.E
> 
> F.F
> 
> 4
> 
> pos
> 
> 
> F.F
> 
> F.F
> 
> 5
> 
> pos
> 
>  
> 
>  
> 
>  
> 
> #upload librairies
> 
> library(circlize)
> 
> library(ggplot2)
> 
> library(igraph)
> 
> library(tidyverse)
> 
> library(RColorBrewer)
> 
> library(stringi)
> 
> library(scico)
> 
> library(plotly)
> 
> library(ggraph)
> 
>  
> 
> #upload
> 
> aes<-read.csv("Test_adjac.csv", row.names = 1)
> 
> details<-read.csv("Test_cat.csv")
> 
>  
> 
>  
> 
> # adjacency  table
> 
> aes_collapsed<-aes %>%
> 
>   rownames_to_column(var='Names') %>%
> 
>   tidyr::gather(target, weight, 1:ncol(aes)+1) %>%
> 
>   dplyr::filter(weight != 0) %>%
> 
>   mutate(weight = ifelse(weight == "-1", 0, weight)) # here 0 = 
> negative values
> 
>  
> 
> write.csv(aes_collapsed, "edges_table-Test.csv", row.names = F)
> 
> edge_list<-read.csv("edges_table-Test.csv")
> 
>  
> 
>  
> 
> #create network and add some necessary attributes (vertices) for the 
> plot
> 
>  
> 
> network <- graph_from_data_frame(aes_collapsed, directed= FALSE,
> 
>  vertices = details)
> 
>  
> 
> ### network and vertex with 'subcategory_type'
> 
>  
> 
> temp<-cluster_optimal(network)
> 
> temp<-cbind(membership=temp$membership, 

[R] an issue about subsetting a vector

2024-03-24 Thread Paulo Barata



To the R-Help list,

I would like to have a clarification about an issue that I observe when 
subsetting a vector. This is R version 4.3.3 on Windows 10.


-- Example with a vector:

> a <- 1:5
> a
[1] 1 2 3 4 5

So we have, with a negative index:
> a[-3]
[1] 1 2 4 5

But when the index is integer(0), we have this:

> a[integer(0)]
integer(0)
> a[-integer(0)]
integer(0)

When it comes to the function integer(), the R Help file says:

"Value: integer creates a integer vector of the specified length. Each 
element of the vector is equal to 0."


But we see below that integer(0) is some kind of null vector, that is, 
no numbers are represented by integer(0):


> class(integer(0))
[1] "integer"
> length(integer(0))
[1] 0

So my question: in the expression a[-integer(0)], what happens exactly 
with the index -integer(0)? We see that integer(0), although of class 
integer, does not represent any numbers, as its length is zero, so it 
seems to me that it makes no sense to calculate its negative 
-integer(0). What exactly is -integer(0)?


In particular, why a[-integer(0)] is not the whole vector a, that is, 
[1] 1 2 3 4 5? In the example below, if the invalid index -99 is 
presented to a, the result is the whole vector:


> a[-99]
[1] 1 2 3 4 5

If -integer(0) is an invalid index, why do we have this?

> a[-integer(0)]
integer(0)

Why a[-integer(0)] is not the whole vector a?

Thank you very much.

Paulo Barata

Rio de Janeiro, Brazil

__
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] an issue about subsetting a vector

2024-03-24 Thread Ben Bolker
   As with a lot of things in R, the behaviour is counterintuitive but 
(arguably) logical. The key (maybe) is that `a[-x]` does not mean "take 
all of the elements of a except those indicated by `x`, but rather, 
"negate x, then take all but the negative elements".  In other words,


 -integer(0)  is   integer(0)  (we multiply *each of the elements of 
integer(0)* by -1, but integer(0) has no elements)


  that reduces to a[integer(0)]

and from there you get "select none of the elements".

  A related point is explained in the R Inferno 
https://www.burns-stat.com/pages/Tutor/R_inferno.pdf section 8.1.13, 
"negative nothing is something".


  See also

https://stackoverflow.com/questions/42615728/subsetting-vector-how-to-programatically-pass-negative-index-safely

https://stackoverflow.com/questions/40026975/subsetting-with-negative-indices-best-practices/40029485#40029485

On 2024-03-24 2:19 p.m., Paulo Barata wrote:


To the R-Help list,

I would like to have a clarification about an issue that I observe when 
subsetting a vector. This is R version 4.3.3 on Windows 10.


-- Example with a vector:

 > a <- 1:5
 > a
[1] 1 2 3 4 5

So we have, with a negative index:
 > a[-3]
[1] 1 2 4 5

But when the index is integer(0), we have this:

 > a[integer(0)]
integer(0)
 > a[-integer(0)]
integer(0)

When it comes to the function integer(), the R Help file says:

"Value: integer creates a integer vector of the specified length. Each 
element of the vector is equal to 0."


But we see below that integer(0) is some kind of null vector, that is, 
no numbers are represented by integer(0):


 > class(integer(0))
[1] "integer"
 > length(integer(0))
[1] 0

So my question: in the expression a[-integer(0)], what happens exactly 
with the index -integer(0)? We see that integer(0), although of class 
integer, does not represent any numbers, as its length is zero, so it 
seems to me that it makes no sense to calculate its negative 
-integer(0). What exactly is -integer(0)?


In particular, why a[-integer(0)] is not the whole vector a, that is, 
[1] 1 2 3 4 5? In the example below, if the invalid index -99 is 
presented to a, the result is the whole vector:


 > a[-99]
[1] 1 2 3 4 5

If -integer(0) is an invalid index, why do we have this?

 > a[-integer(0)]
integer(0)

Why a[-integer(0)] is not the whole vector a?

Thank you very much.

Paulo Barata

Rio de Janeiro, Brazil

__
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: [ESS] Displaying R plots within an Emacs buffer

2024-03-24 Thread Ramon Diaz-Uriarte via ESS-help

Wow! This is really nice! Thanks, Stephen and Rodney!!

A few comments:
- I works perfectly for me in two different machines (both running Debian and 
Emacs 29.2.50)

- Maybe I am not understanding 
https://github.com/sje30/ess-unigd#adjust-to-size-of-buffer--dynamically-update 
, but resize of image (on changing frame and window sizes) happens 
automagically for me.

- The only minor thing is that, for some reason, the svg is shown in 
Fundamental mode (if I find-file other svgs, they are shown as images 
directly); I guess it is something with my setup. revert-buffer solves it.

Best,

R. 


P.S. FWIW, for a while I've been using a kind of similar thing with browsers 
that run inside Emacs. First I started using xwidget-webkit-browse-url, but 
since this recently broke or will be broken soon[1], I then started using EAF 
(Emacs Application Framework). With a browser one gets the browse history of 
plots, mentioned in https://github.com/sje30/ess-unigd#browse-history-of-plots 
. The procedure is similar to what Stephen does: first, in R load library 
httpgd and launch hgd. Then have xwidget-webkit-browse-url or eaf-open-browser 
open the URL returned by hgd.


[1]  https://mail.gnu.org/archive/html/bug-gnu-emacs/2023-09/msg02681.html , 
https://debbugs.gnu.org/db/66/66068.html

On Fri, 22-March-2024, at 16:43:23, "Sparapani, Rodney via ESS-help" 
 wrote:
> Oh, yeah, sorry that was stupid.  So now I have execute permission.
> But, this is what latest.svg shows now…
> Status: 404
> Reason: Not Found

-- 
Ramon Diaz-Uriarte
Department of Biochemistry, Lab B-31
Facultad de Medicina 
Universidad Autónoma de Madrid 
Arzobispo Morcillo, 4
28029 Madrid
Spain

Phone: +34-91-497-2412

Email: rdia...@gmail.com
   r.d...@uam.es
   ramon.d...@iib.uam.es

https://ligarto.org/rdiaz


__
ESS-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/ess-help