我想創建一個應用程式,用戶可以在其中輸入鏈接或一些文本并以 pdf 格式下載相應的二維碼。我已經有了基本的構建塊,但我無法將它們粘合在一起。比如純二維碼生成部分
library(qrcode)
qr <- qr_code("https://www.wikipedia.org/")
pdf("qr_code.pdf")
plot(qr)
dev.off()
#> png
#> 2
由reprex 包(v2.0.1)于 2022 年 1 月 4 日創建
用于在 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 包(v2.0.1)于 2022 年 1 月 4 日創建
并在 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 包(v2.0.1)于 2022 年 1 月 4 日創建
誰能幫我把所有這些放在一起?
謝謝!
uj5u.com熱心網友回復:
您可以這樣做:
用戶界面:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("link", "Enter Link here", "www.google.com"),
downloadButton("save", "Download QR")
),
mainPanel(
plotOutput("tplot" )
)
)
)
textInput接受引數inputId, label, 和value。
inputId是您將在代碼中參考的輸入。label告訴將在輸入欄位上寫入的內容。它是用戶可以看到并確定要在欄位中輸入的內容。- '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
}
)
}
請注意,我已qr_code在reactive欄位內使用,以便您可以在輸出中進一步使用它。閃亮的應用程式現在將在您繼續在輸入欄位內輸入時顯示 QR 碼。由于它是被動的,它會對您的輸入做出反應。
下載功能也按預期作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/403203.html
標籤:
