Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QR code for URL #3

Closed
Yang-Tang opened this issue Feb 7, 2016 · 2 comments
Closed

QR code for URL #3

Yang-Tang opened this issue Feb 7, 2016 · 2 comments

Comments

@Yang-Tang
Copy link

Thank you for this nice shiny package. It would be more insteresting to add an option to generate a URL QR code of current status of shiny app.

@aoles
Copy link
Owner

aoles commented Feb 10, 2016

Hi @Yang-Tang,
thank you for your feedback!

I've just updated shinyURL to provide an easy way of capturing the URL inside the app's script. Starting from version 0.0.30, the function shinyURL.server returns a reactive expression evaluating to the app's URL. This gives direct access to the string containing the URL, making it easy to customize how it is displayed to the user.

A possible use case is to take the URL and use it to generate a QR code which will be displayed to the user instead of the default shinyURL.ui widget. The QR code itself can be created, for example, with the help of the R package qrcode, as illustrated in the following example. The size of the code can be controlled by setting scale.

library("shiny")
library("shinyURL")
library("qrcode")

shinyApp(
  ui = fluidPage(
    titlePanel("Old Faithful Geyser Data"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
        plotOutput("qrcode")
      ),
      mainPanel(
        plotOutput("plot")
      )
    )
  ),
  server = function(input, output) {
    url = shinyURL.server()

    output$plot <- renderPlot({
      x <- faithful[, 2]
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })

    qrcode = reactive( t(qrcode_gen(url(), plotQRcode = FALSE, dataOutput = TRUE)) )
    nc = reactive( ncol(qrcode()) )
    nr = reactive( nrow(qrcode()) )
    scale = 4

    output$qrcode <- renderPlot({
      par(mar=c(0,0,0,0))
      image(1L:nc(), 1L:nr(), qrcode(), xlim = 0.5 + c(0, nc()),
            ylim = 0.5 + c(nr(), 0), axes = FALSE, xlab = "", ylab = "",
            col = c("white", "black"), asp = 1)
    }, width = function() scale*nc(), height = function() scale*nr())
  }
)  

For an application in an interactive R markdown document, see the example distributed with the package.

@Yang-Tang
Copy link
Author

Thank you @aoles ! The code and documents are very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants