2018-10-26
閱讀量:
1365
R實現(xiàn)shiny高級技巧--發(fā)送圖片
發(fā)送圖片
當(dāng)您想讓R生成一個繪圖并將其發(fā)送到客戶機瀏覽器時,renderPlot()函數(shù)在大多數(shù)情況下會完成這項工作。但是,當(dāng)您需要更好地控制流程時,您可能需要使用renderImage()函數(shù)。
關(guān)于renderPlot()
renderPlot()在R使用其正常的圖形設(shè)備系統(tǒng)生成圖像的任何時候都很有用。換句話說,任何通常介于png()和dev.off()之間的繪圖生成代碼都可以在renderPlot()中使用。如果以下代碼在控制臺工作,那么它應(yīng)該在renderPlot()中工作:
png()
# Your plotting code here
dev.off()
# This would go in shinyServer()
output$myPlot
<-
renderPlot({
# Your plotting code here
})
###可以使用renderImage()發(fā)送圖像文件。傳遞給renderImage()的表達式必須返回一個包含名為src的元素的列表,src是文件的路徑。下面是一個非常簡單的應(yīng)用程序示例,該應(yīng)用程序的輸出生成一個繪圖并通過renderImage()發(fā)送:
shinyServer(function(input, output, session) {
output$myImage <- renderImage({
# A temp file to save the output.
# This file will be removed later by renderImage
outfile <- tempfile(fileext='.png')
# Generate the PNG
png(outfile, width=400, height=300)
hist(rnorm(input$obs), main="Generated in renderImage()")
dev.off()
# Return a list containing the filename
list(src = outfile,
contentType = 'image/png',
width = 400,
height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
})
shinyUI(pageWithSidebar(
headerPanel("renderImage example"),
sidebarPanel(
sliderInput("obs", "Number of observations:",
min = 0, max = 1000, value = 500)
),
mainPanel(
# Use imageOutput to place the image on the page
imageOutput("myImage")
)
))####
每次重新執(zhí)行這個輸出對象時,它都會創(chuàng)建一個新的PNG文件,將一個繪圖保存到其中,然后返回一個包含文件名和其他一些值的列表。
因為deleteFile參數(shù)為TRUE,所以當(dāng)src元素發(fā)送數(shù)據(jù)后,shine將刪除文件(由src元素指定)。這適用于這樣的情況,即圖像是動態(tài)創(chuàng)建的,但在應(yīng)用程序發(fā)送預(yù)呈現(xiàn)的圖像時就不合適了。






評論(0)


暫無數(shù)據(jù)
推薦帖子
0條評論
0條評論
0條評論