Home > front end >  R Shiny: qr code generation and download
R Shiny: qr code generation and download

Time:01-05

I want to create an app where the user can input a link or some text and download the corresponding QR code as a pdf. I have already the fundamental building blocks, but I cannot glue them together. For instance, for the pure QR code generation part

library(qrcode)



qr <- qr_code("https://www.wikipedia.org/")

pdf("qr_code.pdf")
plot(qr)
dev.off()
#> png 
#>   2

Created on 2022-01-04 by the reprex package (v2.0.1)

for inputting text in Shiny

library(shiny)


ui <- fluidPage(
  textInput("caption", "Caption", "Your link/text here"),
  verbatimTextOutput("value")
)



server <- function(input, output) {
    output$value <- renderText({ input$caption })

}



shinyApp(ui, server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents

Created on 2022-01-04 by the reprex package (v2.0.1)

and for saving a plot as a pdf in Shiny

library(shiny)
library(tidyverse)


df <- tibble(x=seq(10), y=seq(10))


ui <- fluidPage(
 sidebarLayout(
        sidebarPanel(

 downloadButton("save", "Download plot"),
 ),
 
mainPanel(
     plotOutput("tplot" ) 
)


)

)


server <- function(input, output) {

      tplot <- reactive({

plot(df$x, df$y)
          
      })

        output$tplot <- renderPlot({
tplot()
    })





# downloadHandler contains 2 arguments as functions, namely filename, content
  output$save <- downloadHandler(
    filename =  function() {
      paste("myplot.pdf")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file) {

        pdf(file) # open the pdf device
      plot(x=df$x, y=df$y) # draw the plot
        dev.off()  # turn the device off
    
    } 
)

    
    
}










shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents

Created on 2022-01-04 by the reprex package (v2.0.1)

Can anyone help me put all of this together?

Thanks!

CodePudding user response:

Here's how you can do this:

UI:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      
      textInput("link", "Enter Link here", "www.google.com"),
      downloadButton("save", "Download QR")
    ),
    mainPanel(
      plotOutput("tplot" ) 
    )
  )
)

textInput takes arguments inputId, label, and value.

  1. inputId is what you'll refer to the input inside your code.
  2. label tells what will be written over the input field. It is something that user can see and identify what to enter in the field.
  3. 'value` is the default value that your input field will have. It can be blank.

Server:

server <- function(input, output) {
  tplot <- reactive({
    qr <- qr_code(input$link)
    plot(qr)
    
  })
  output$tplot <- renderPlot({
    tplot()
  })
  
  # downloadHandler contains 2 arguments as functions, namely filename, content
  output$save <- downloadHandler(
    filename =  function() {
      paste("myplot.pdf")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file) {
      pdf(file) # open the pdf device
      plot(qr_code(input$link)) # draw the plot
      dev.off()  # turn the device off
    } 
  )
}

Notice that I've used qr_code inside the reactive field so that you can use it further in output. The shiny app will now, show the QR code as you keep typing inside the input field. Since it is reactive, it reacts to your input.

The download functionality also works as expected.

  •  Tags:  
  • Related