feddelegrand7 / fabricerin

Create Easily Canvas in Shiny and RMarkdown Documents
https://ihaddadenfodil.com/post/fabricerin-a-tutorial/
Other
52 stars 5 forks source link

add to canvas with a loop #10

Open hansvt opened 3 years ago

hansvt commented 3 years ago

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

feddelegrand7 commented 3 years ago

Hi,

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

Best.

feddelegrand7 commented 3 years ago

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

hansvt commented 3 years ago

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 < notifications@github.com>

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 .

feddelegrand7 commented 3 years ago

Good luck :)

hansvt commented 3 years ago

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 < notifications@github.com>:

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 .

feddelegrand7 commented 3 years ago

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

hansvt commented 3 years ago

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 < notifications@github.com>:

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 .