R + Shiny:二维码生成和下载

R + Shiny: qr code generation and download

我想创建一个应用程序,用户可以在其中输入 link 或一些文本,然后将相应的二维码下载为 pdf 格式。 我已经有了基本的构建块,但我无法将它们粘合在一起。 比如纯二维码生成部分

library(qrcode)



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

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

reprex package (v2.0.1)

于 2022-01-04 创建

用于在 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.
静态 R Markdown 文档不支持闪亮的应用程序

reprex package (v2.0.1)

于 2022-01-04 创建

并在 Shiny

中将绘图保存为 pdf
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.
静态 R Markdown 文档不支持闪亮的应用程序

reprex package (v2.0.1)

于 2022-01-04 创建

谁能帮我把所有这些放在一起?

谢谢!

执行此操作的方法如下:

UI:

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

textInput 采用参数 inputIdlabelvalue.

  1. inputId 是您将在代码中引用的输入。
  2. label 表示将在输入字段上写入的内容。这是用户可以看到并确定在该字段中输入的内容。
  3. 'value` 是您输入字段的默认值。可以为空。

服务器:

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
    } 
  )
}

请注意,我在 reactive 字段中使用了 qr_code,以便您可以在输出中进一步使用它。 当您在输入字段中不断输入时,闪亮的应用程序现在会显示 QR 码。由于它是反应式的,它会对您的输入做出反应。

下载功能也按预期工作。