fabricerin icon indicating copy to clipboard operation
fabricerin copied to clipboard

add to canvas with a loop

Open hansvt opened this issue 5 years ago • 7 comments

Hi,

How can I add objects to the canvas in a loop?

Let say I have a logics: <- c(TRUE, FALSE, TRUE) and I want to add a shape to the canvas if logic is TRUE.

When I try to do something like this in my UI then nothing is displayed:

for (logic in logics) { if (logic== TRUE) { fabric_shape(cid = "canvaId", shapeId = "shape1", shape = "Rect", left = 130, top = 200) } }

I am new at R programming, but I can't see why this doesn't work..

thanks, Kind regards, Hans

hansvt avatar Jan 18 '21 17:01 hansvt

Hi,

it's not working because the canvas id is the same. You need to change it while looping.

Best.

feddelegrand7 avatar Jan 18 '21 18:01 feddelegrand7

you need also the make sure that left and top are modified in each loop, otherwise you would have shapes that overlap.

feddelegrand7 avatar Jan 18 '21 18:01 feddelegrand7

Great, I will do that. I tried to update the left and top, and I think I tried to update the ID , but I guess that this didn’t work as it should. I will try again and let you know if this solves it.

Many thanks for your response! Hans

Op ma 18 jan. 2021 om 19:41 schreef Ihaddaden Mohamed El Fodil < [email protected]>

you need also the make sure that left and top are modified in each loop, otherwise you would have shapes that overlap.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/feddelegrand7/fabricerin/issues/10#issuecomment-762415327, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASQKDNFD6MIAZXMYVL3VZ2DS2R6GZANCNFSM4WHUMFEA .

hansvt avatar Jan 18 '21 20:01 hansvt

Good luck :)

feddelegrand7 avatar Jan 18 '21 21:01 feddelegrand7

Hi,

Probably I do something wrong, but when I create the most simple app I can think of. And try to loop through a vector and print text on the canvas in that loop it fails...

I just see the text I initially put on the canvas, and not the text that I add later in the loop.

See below the code I use:

library(shiny) library(fabricerin)

create input data

visits <- c("Visit 1", "Visit 2", "Visit 3", "Visit 4", "Visit 5") leftpos <- 200 n <- 1

ui <- fluidPage( fabric_text(cid = "canvaId", # canvas id cwidth = 800, # the width of the canvas cheight = 700, # the height of the canvas textId = "text_visith", text = "Visit", fontSize = 20, left = 100, # the position of the shape from the left relative to the canvas top = 50 ), for (visit in visits) { print(n) print(leftpos) id <- paste0("visitID",n) print(id) fabric_text_add(cid = "canvaId", textId = id, text = visit, fontSize = 20, left = leftpos, # the position of the shape from the left relative to the canvas top = 50) n = n + 1 leftpos = leftpos + 100 } )

server <- function(input, output){}

shinyApp(ui, server)

thanks, Hans

Op ma 18 jan. 2021 om 22:05 schreef Ihaddaden Mohamed El Fodil < [email protected]>:

Good luck :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/feddelegrand7/fabricerin/issues/10#issuecomment-762470060, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASQKDNAKFNGURIWAR4WP4MLS2SPARANCNFSM4WHUMFEA .

hansvt avatar Jan 19 '21 09:01 hansvt

Interesting ... I don't know why it's not working, will check it out.

feddelegrand7 avatar Jan 19 '21 09:01 feddelegrand7

I did some further exploration and it looks like this issue occurs because of the missing comma between the different 'fabric_add' functions. I just don't know how to solve this.

When I do several 'if statements' instead of the for loop it does work. But having to specify if statements for every potential length of the vector is not really a good idea of course.

Op di 19 jan. 2021 om 10:43 schreef Ihaddaden Mohamed El Fodil < [email protected]>:

Interesting ... I don't know why it's not working, will check it out.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/feddelegrand7/fabricerin/issues/10#issuecomment-762724932, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASQKDNB332OCTFG62WBOZ6LS2VH2JANCNFSM4WHUMFEA .

hansvt avatar Jan 20 '21 10:01 hansvt