Git Product home page Git Product logo

fabricerin's Introduction

fabricerin

Travis build status CRAN_time_from_release CRAN_latest_release_date metacran downloads metacran downloads License: AGPL v3 R badge

The fabricerin (spelled fabrikerine) package allows you to create easily canvas elements within your Shiny app and RMarkdown documents. Thanks to Garrick Aden-Buie, you can also use it within your xaringan slides. You can use the canvas to render shapes, images and text. You can also create a canvas for drawing/taking notes purposes. Under the hoods, fabricerin relies on the fabricjs JavaScript library.

Installation

You can install fabricerin from CRAN with:

install.packages("fabricerin")

You can install the development version from GitHub with:

# install.packages("remotes")

remotes::install_github("feddelegrand7/fabricerin")

Examples:

First of all, I’d like to state that all the example provided apply the same way to Shiny and Rmd documents. fabricerin is not an R wrapper for the fabricjs library. The package doesn’t cover all the capabilities of the library. The fabricerin package relies only on some specified features that according to me will help Shiny/Rmd users. Of course, if you need some improvement, feel free to create a Pull Request.

fabric_drawing() Create a canvas for taking notes


fabric_drawing() is pretty useful when you want to teach something and write some notes at the same time, below I provide an example using the xaringan package. Inside a xaringan slide you can just (for example) run R code in the left and take notes in the right:

Important: When you change a color, make sure that the erase box is not checked.

fabric_drawing() can be used the same way in Shiny:

library(shiny)
library(fabricerin)


ui <- fluidPage(
  
  
  fabric_drawing(cid = "canvas123")

)

server <- function(input, output){}

shinyApp(ui, server)

fabric_shape() Render shape objects in canvas


Currently, fabricerin supports three types of shapes: Rectangle, Triangle, Circle and Polygon. The user can interact with the shape and modify its position, size and rotation. If you want to disable this interactivity, you can set selectable =FALSE

library(shiny)
library(fabricerin)


ui <- fluidPage(
  

  fabric_shape(cid = "canvaId", # canvas id
               cfill = "orange", # canvas color
               cwidth = 800, # the width of the canvas
               cheight = 600, # the height of the canvas
               shapeId = "shapeId", # shape id
               shape = "Rect", 
               fill = "red", # shape color
               width = 400, 
               height = 400, 
               left = 100, # the position of the shape from the left relative to the canvas
               top = 100, # the position of the shape from the top relative to the canvas
               strokecolor = "darkblue", 
               strokewidth = 5, 
               selectable = TRUE)
  
)

server <- function(input, output){}

shinyApp(ui, server)

You can add as many shape as you want to an existing canvas using the fabric_shape_add() function. Don’t forget to reference the preexisting canvas with its ID:

library(shiny)
library(fabricerin)


ui <- fluidPage(
  
  
  fabric_shape(cid = "canvaId", 
               shapeId = "cr1", 
               shape = "Circle", 
               radius = 30, 
               left = 100), 
  
  fabric_shape_add(cid = "canvaId", 
                   shapeId = "cr2", 
                   shape = "Circle", 
                   radius = 30, 
                   left = 200),
  
  fabric_shape_add(cid = "canvaId", 
                   shapeId = "cr3", 
                   shape = "Circle", 
                   radius = 30, 
                   left = 300),
  
  fabric_shape_add(cid = "canvaId", 
                   shapeId = "cr4", 
                   shape = "Circle", 
                   radius = 30, 
                   left = 400)
  
)

server <- function(input, output){}

shinyApp(ui, server)

fabric_image() Render images in canvas


You can insert an image within a canvas a play with it using the fabric_image() function. Note that this function accepts only URL external images.

ui <- fluidPage(

fabric_image(cid = "cimage",
              cfill = "lightblue",
              imgId = "Rimg",
              imgsrc = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/724px-R_logo.svg.png")

              )

server <- function(input, output) {}


shinyApp(ui = ui, server = server)

Similar to shapes, you can add images to preexisting canvas using the fabric_image_add() function:

library(shiny)
library(fabricerin)

ui <- fluidPage(

 fabric_image(cid = "cimage",
              imgId = "Rimg",
              imgsrc = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/724px-R_logo.svg.png",
              imgheight = 200,
              imgwidth = 200),
 fabric_image_add(cid = "cimage",
                  imgId = "rstudioimg",
                  imgsrc = "https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/dplyr.png",
                  imgwidth = 200,
                  imgheight = 200,
                  left = 400)
                  )

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

fabric_text() Render text elements in canvas


The fabric_text() function has many arguments, feel free to check them out:

ui <- fluidPage(

fabric_text(cid = "cId",
          textId = "text",
          text = " 'But A Hero Is A Guy Who Gives Out The Meat To Everyone Else. \\n I Want To Eat The Damn Meat!' \\n Monkey D. Luffy",
          cfill = "#DD5347",
          left = 120,
          shadowCol = "blue",
          fontSize = 20,
          fontWeight = "bold",
          lineHeight = 3
          )
)
server <- function(input, output) {}

shinyApp(ui = ui, server = server)

Here also, we can use the fabric_text_add() function to incorporate a text object within a canvas element:

library(shiny)
library(fabricerin)


ui <- fluidPage(

fabric_shape(cid = "canvas123",
              cfill = "lightblue",
              cwidth = 1000,
              shapeId = "tri1",
              shape = "Triangle",
              fill = "darkblue"),

fabric_text_add(cid = "canvas123",
                 textId = "txt1",
                 text = "This is a darkblue Triangle !",
                 left = 350
                 )

                 )

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

fabric_curtail() Add a background or an overlay image to a canvas

You can set an image as a background or as a foreground (overlay) for a canvas as follows:

Note that due to security reasons, you won’t be able to replicate the following example on some images’ sources.

ui <- fluidPage(

 fabric_shape(cid = "canvas123",
              shapeId = "tri1",
              shape = "Triangle",
              fill = "lightblue"),

fabric_curtail(cid = "canvas123",
              imgsrc = "https://st.depositphotos.com/1642482/1904/i/950/depositphotos_19049237-stock-photo-leaf.jpg",
              type = "background"

              )

)

server <- function(input, output) {}


shinyApp(ui = ui, server = server)

Code of Conduct

Please note that the fabricerin project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

fabricerin's People

Contributors

feddelegrand7 avatar gadenbuie avatar imgbotapp avatar z3tt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fabricerin's Issues

Access the drawing in Shiny server

Great package!

Is there a way to access what is drawn Shiny Server? For example, say I draw some lines. Then I want to add some image processing (like blurring using Image Magick). Is there a plan to include that kind of workflow in fabricerin?

Sketch over text or image?

I'm thoroughly enjoying this package! My lecture notes this week have a scratch pad for sketching in one slide.

How difficult would it be to have a fabric_drawing_add() function that can be added after a text or image? The text would need to be in a fixed position so that the pencil was the interactivity.

Export in fabric_drawing()

There's an export button connected to the canvas produced by fabric_drawing(), and I can see in the source code that the click method invokes saveAs(). But I don't know how to get saveAs() to save the blob, or if it is saving it, where that is. I'm not a JavaScript programmer, which is precisely why tools like this are so helpful. I've looked through various forums about saveAs(), some suggesting that one needs to implement the function in the document (which I've tried, creating a stub that indeed gets called when clicking "export." Others suggest that saveAs() is built-in to the browser, but you need to set certain permissions to use it.

I wonder if you might include an example in the package that allows drawings to be saved or copied to the clipboard or a Shiny object or whatever. This would be extremely helpful in the application I'm developing for letting statistics and math students draw on top of graphs and hand in their work.

How to include in Xaringan slides

The readme states that

you can also use it within your xaringan slides

Awesome, but how? I looked at the original tweet and an associated issue: yihui/xaringan#204

Thanks for a fantastic package. FYI, it works great with a Wacom drawing tablet and pen 🖊️ 🎉

add to canvas with a loop

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

Render image inside dynamically created UI?

Would it be be possible to render image inside a UI element that is dynamically created?

Seems like a little javascript would need to be changed.

I'm looking for something like this:

library(fabricerin)

ui <- fluidPage(
  actionButton("render", "Put image on Canvas!"),
  uiOutput("gen.in.server")
  
)

server <- function(input, output) {
  
  observeEvent(input$render,{

    output$gen.in.server <- 
      renderUI({
        
        fabric_image(cid = "cimage",
                     cfill = "lightblue",
                     imgId = "Rimg",
                     imgsrc = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/724px-R_logo.svg.png")
      })
  })
  
}


shinyApp(ui = ui, server = server)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.