Venn Diagram i R Studio - rstudio

How do I make a Venn diagram that looks like this:

There are several options. For instance, I created a library called nVennR which you can install from CRAN. For your example,
library(nVennR)
myV <- createVennObj(nSets = 5, sNames = c('A', 'B', 'C', 'D', 'E'))
myV <- setVennRegion(myV, c('E'), 2)
myV <- setVennRegion(myV, c('E', 'A'), 4)
myV <- setVennRegion(myV, c('B'), 8)
myV <- setVennRegion(myV, c('A', 'B'), 10)
myV <- setVennRegion(myV, c('A'), 9)
myV <- setVennRegion(myV, c('D', 'A'), 4)
myV <- setVennRegion(myV, c('D'), 8)
myV <- setVennRegion(myV, c('A', 'C'), 3)
myV <- setVennRegion(myV, c('C'), 5)
pV <- plotVenn(nVennObj = myV)
And the result is:
The result is a vectorial image that can be saved and edited. For more details, you can read the vignette

Related

Go slice overwrite

Here is the source code:
package main
func main() {
testSlice()
}
func testSlice() {
slice := make([]int, 0)
//slice = append(slice, 1) ①
//slice = append(slice, 1, 2) ②
//slice = append(slice, 1, 2, 3) ③
//slice = append(slice, 1, 2, 3, 4) ④
slice = append(slice, 1, 2, 3, 4, 5) ⑤
//slice = append(slice, 1, 2, 3, 4, 5, 6) ⑥
slice2 := append(slice, 1)
slice3 := append(slice, 2)
for _, i := range slice2 {
print(i)
}
println()
for _, i := range slice3 {
print(i)
}
}
Expected output:
123451
123452
Actual output:
123452
123452
The output of ①~⑥ except ⑤ is as expected. But why ⑤ slice3 overwrites slice2?
Is the reason related to pointer or slice resize?
Check out this SO answer for a really helpful explanation of what a slice actually is before reading the rest of my answer. It might be easier to understand what's going on by printing out the actual slice headers. See the following example code (and go playground):
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
testSlice()
}
func testSlice() {
slice := make([]int, 0)
slice = append(slice, 1, 2, 3, 4, 5)
fmt.Printf("%+v\n", (*reflect.SliceHeader)(unsafe.Pointer(&slice)))
slice2 := append(slice, 1)
fmt.Printf("%+v\n", (*reflect.SliceHeader)(unsafe.Pointer(&slice2)))
slice3 := append(slice, 2)
fmt.Printf("%+v\n", (*reflect.SliceHeader)(unsafe.Pointer(&slice3)))
for _, i := range slice2 {
print(i)
}
println()
for _, i := range slice3 {
print(i)
}
}
This will print something like this:
&{Data:824634441776 Len:5 Cap:6}
&{Data:824634441776 Len:6 Cap:6}
&{Data:824634441776 Len:6 Cap:6}
This shows that all variables slice, slice2, and slice3 are pointing at the same data (Data which is a pointer to the first element of the slice) but the slice headers themselves are different. When you're performing your appends, you are modifying the underlying slice shared by all the variables, and storing new slice headers into new variables. The slice headers for slice2 and slice3 are looking at the same slice of data, so when you come along and perform your append for slice3, you're overwriting the 6th element in the underlying slice that is shared by all the variables.

How to create a block matrix in Golang?

I'm trying to create a block matrix which contains 4 blocks (n*n submatrices).
I tried many things but I can't get it to work.
func newBlocMatrix(A Matrix, B Matrix, C Matrix, D Matrix) (M Matrix) {
var M Matrix
// Something here
// Filled with A, B, C, and D
return M, nil
}
Any suggestions to fill the matrix M with matrices A, B, C and D?
For simplicity I'll assume Matrix is a square (n*n) [][]int:
package main
import "fmt"
type Matrix [][]int
func Block(a, b, c, d Matrix) Matrix {
l := len(a)
s := l * 2
m := make([][]int, s)
for i := 0; i < s; i++ {
m[i] = make([]int, s)
}
copy(a, m, 0, 0)
copy(b, m, 0, l)
copy(c, m, l, 0)
copy(d, m, l, l)
return m
}
func copy(a, m Matrix, x, y int) {
for i := 0; i < len(a); i++ {
for j := 0; j < len(a[i]); j++ {
m[i+x][j+y] = a[i][j]
}
}
}
func main() {
a := Matrix{{1, 2}, {3, 4}}
b := Matrix{{5, 6}, {7, 8}}
c := Matrix{{9, 10}, {11, 12}}
d := Matrix{{13, 14}, {15, 16}}
m := Block(a, b, c, d)
fmt.Printf("a: %+v\n", a)
fmt.Printf("b: %+v\n", b)
fmt.Printf("c: %+v\n", c)
fmt.Printf("d: %+v\n", d)
fmt.Printf("m: %+v\n", m)
}
Running that prints:
a: [[1 2] [3 4]]
b: [[5 6] [7 8]]
c: [[9 10] [11 12]]
d: [[13 14] [15 16]]
m: [[1 2 5 6] [3 4 7 8] [9 10 13 14] [11 12 15 16]]
Which I believe is what you want.

Need help understand logic behind concocurrent of goroutine ,select and channel

I try to understand logic behind concurrency of goroutine ,select and channel. The sample code is below. The base code is from tour go. I add some Printf to help me understand better.
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
fmt.Printf("(%v, %v)\n", x ,y)
x, y = y, x+y
fmt.Printf("(%v, %v)\n", x ,y)
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 4; i++ {
fmt.Println(<-c)
fmt.Printf("%v from main\n",i)
}
quit <- 0
}()
fibonacci(c, quit)
}
Output is
0
0 from main
(0, 1)
(1, 1)
(1, 1)
(1, 2)
1
1 from main
1
2 from main
(1, 2)
(2, 3)
(2, 3)
(3, 5)
2
3 from main
quit
There are concurrency behind goroutine and channel operation. My question is why the output is not
0
0 from main
(0, 1)
(1, 1)
1
1 from main
(1, 1)
(1, 2)
1
2 from main
(1, 2)
(2, 3)
2
3 from main
(2, 3)
(3, 5)
quit
It might clarify things to rewrite the body of the for loop in the goroutine slightly:
x := <-c
// block until a value has been read, then continue
fmt.Println(x)
fmt.Printf("%v from main\n",i)
And similarly the body of the select loop in fibonacci() is effectively:
c <- x
// block until a value has been written, then continue
fmt.Printf("(%v, %v)\n", x ,y)
x, y = y, x+y
fmt.Printf("(%v, %v)\n", x ,y)
In both cases you have a guarantee that the second print statement will print after the first.
When you run the program, say the goroutine starts immediately. It gets up to this read, but there is no value in the channel, so it blocks. Then the main program calls fibonacci(). It gets to the select statement. There is a reader for the channel c, so it sends the number x there.
Once this has happened both the goroutine and the main program are free to go. The select statement has triggered one of its branches and sent its value; the read has completed. Both goroutines are free to run, and can do so in any order (so long as each executes its own statements in order). You'll eventually reach a point where either the goroutine is blocked on the read or fibonacci() is blocked on the select, and once both catch up to each other, both will be free to execute again.
The order you propose would require the reader to "wake up" before the writer did, but nothing in Go requires this, and indeed on a multi-core system both can be running at the same time.

Multiple senders to single channel in Golang

The concept seems simple to explain, but a tad harder to implement ("correctly").
The tl;dr is I want to run multiple functions that push output into a single channel.
As a sample working test (with multiple channels), to elaborate on my question https://play.golang.org/p/1ztCvPFLXKv
package main
import (
"fmt"
"time"
)
type intTest struct {
ID int
Number int
}
func modify1(channelID string, res chan []intTest) {
s := []intTest{}
for i := 0; i < 10; i++ {
fmt.Printf("Adding inside: %s\n", channelID)
s = append(s, intTest{i, 0})
time.Sleep(100 * time.Millisecond)
}
res <- s
}
func modify2(channelID string, res chan []intTest) {
s := []intTest{}
for i := 10; i < 20; i++ {
fmt.Printf("Adding inside: %s\n", channelID)
s = append(s, intTest{i, 0})
time.Sleep(200 * time.Millisecond)
}
res <- s
}
func modify3(channelID string, res chan []intTest) {
s := []intTest{}
for i := 20; i < 30; i++ {
fmt.Printf("Adding inside: %s\n", channelID)
s = append(s, intTest{i, 0})
time.Sleep(300 * time.Millisecond)
}
res <- s
}
func main() {
channelA := make(chan []intTest)
channelB := make(chan []intTest)
channelC := make(chan []intTest)
go modify1("A", channelA)
go modify2("B", channelB)
go modify3("C", channelC)
b := append(<-channelA, <-channelB...)
b = append(b, <-channelC...)
fmt.Println(b)
}
Output:
Adding inside: C
Adding inside: A
Adding inside: B
..snip..
Adding inside: C
Adding inside: C
Adding inside: C
[{0 0} {1 0} {2 0} {3 0} {4 0} {5 0} {6 0} {7 0} {8 0} {9 0} {10 0} {11 0} {12 0} {13 0} {14 0} {15 0} {16 0} {17 0} {18 0} {19 0} {20 0} {21 0} {22 0} {23 0} {24 0} {25 0} {26 0} {27 0} {28 0} {29 0}]
However, I would like to achieve something like this: https://play.golang.org/p/qvC88LwkanY
Output:
Adding inside: C
Adding inside: A
Adding inside: B
..snip
Adding inside: B
Adding inside: A
Adding inside: C
[{0 0} {1 0} {2 0} {3 0} {4 0} {5 0} {6 0} {7 0} {8 0} {9 0}]
but as shown the functions modify2 & modify3 visually seems like they never get added.
Is this possible, or is the top sample more feasible?
I would like to achieve something like this
channelA := make(chan []intTest)
go modify1("A", channelA)
go modify2("B", channelA)
go modify3("C", channelA)
Is this possible, or is the top sample more feasible?
Yes: you can use a single channel across multiple goroutines -- that's what channels are designed for.
but as shown the functions modify2 & modify3 visually seems like they never get added.
The problem you have there is that you only called the receive operator once:
b := append(<-channelA)
fmt.Println(b)
The other two goroutines were either blocked waiting to send their result, or still building their result, when your main goroutine exited.
If you change your main() function to this, you can see that all three workers will send their results over the channel if another routine is ready to receive (because you used an unbuffered channel, sends will block until a receiver is ready):
func main() {
ch := make(chan []intTest)
go modify1("A", ch)
go modify2("B", ch)
go modify3("C", ch)
fmt.Println(<-ch)
fmt.Println(<-ch)
fmt.Println(<-ch)
}
Which outputs:
Adding inside: C
Adding inside: A
Adding inside: B
Adding inside: A
Adding inside: A
Adding inside: B
Adding inside: C
Adding inside: A
Adding inside: B
Adding inside: A
Adding inside: A
Adding inside: B
Adding inside: C
Adding inside: A
Adding inside: A
Adding inside: B
Adding inside: A
Adding inside: C
Adding inside: A
Adding inside: B
[{0 0} {1 0} {2 0} {3 0} {4 0} {5 0} {6 0} {7 0} {8 0} {9 0}]
Adding inside: C
Adding inside: B
Adding inside: B
Adding inside: C
Adding inside: B
Adding inside: C
Adding inside: B
[{10 0} {11 0} {12 0} {13 0} {14 0} {15 0} {16 0} {17 0} {18 0} {19 0}]
Adding inside: C
Adding inside: C
Adding inside: C
[{20 0} {21 0} {22 0} {23 0} {24 0} {25 0} {26 0} {27 0} {28 0} {29 0}]
You can then change that code to append received elements to a single list prior to output, or otherwise format the received elements in whatever way you like.
I thought it's almost like popping onto a stack and then pulling the whole stack
With an initialized, not-closed, buffered channel, a send statement puts one element onto a queue, and a receive operation pops one element off the queue and returns it. It's first in first out (FIFO) order, so it's a queue rather than a stack. In the examples above the channel is unbuffered, so a send has to wait for a goroutine ready to receive, and a receive has to wait for a goroutine ready to send. Main is a goroutine as well.

panic: runtime error: slice bounds out of range

I'm following this tutorial: https://gobyexample.com/slices
I was in the middle:
package main
import "fmt"
func main() {
s := make([]string, 3)
fmt.Println("emp:", s)
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
c := make([]string, len(s))
copy(c, s)
fmt.Println("copy:", c)
l := s[2:5]
fmt.Println("sl1:", l)
}
when I suddenly encountered this error:
alex#alex-K43U:~/golang$ go run hello.go
emp: [ ]
set: [a b c]
copy: [a b c]
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
main.main()
/home/alex/golang/hello.go:19 +0x2ba
goroutine 2 [syscall]:
created by runtime.main
/usr/lib/go/src/pkg/runtime/proc.c:221
exit status 2
What does it mean? Is the tutorial mistaken? What can I do to fix it?
Your code omits these lines from the original example:
s = append(s, "d")
s = append(s, "e", "f")
Without these lines, len(s) == 3.
You forgot the append part that grows s.
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd:", s)

Resources