나는 shiny modules를 사용하여 다른 하나를 추가 할 원, 아주 좋다. 다음 모듈은 plot 함수와 인수의 반응식 버전을 입력으로 사용합니다. 결국 do.call(plotfun, args())
은 플롯을 만드는 데 사용됩니다.
library(shiny)
cachePlot <- function(input, output, session, plotfun, args, width = 480, height = 480,
dir = tempdir(), prefix = "cachedplot", deleteonexit = TRUE){
hash <- function(args) digest::digest(args)
output$plot <- renderImage({
args <- args()
if (!is.list(args)) args <- list(args)
imgpath <- file.path(dir, paste0(prefix, "-", hash(args), ".png"))
if(!file.exists(imgpath)){
png(imgpath, width = width, height = height)
do.call(plotfun, args)
dev.off()
}
list(src = imgpath)
}, deleteFile = FALSE)
if (deleteonexit) session$onSessionEnded(function(){
imgfiles <- list.files(tempdir(), pattern = prefix, full.names = TRUE)
file.remove(imgfiles)
})
}
cachePlotUI <- function(id){
ns <- NS(id)
imageOutput(ns("plot"))
}
우리가 볼 수 있듯이
이 모듈은 필요한 경우 생성 된 이미지 파일을 삭제하고 (내 실제 유스 케이스에서와 같이) 지속적인 캐시가 필요한 경우 사용자 정의 캐싱 디렉토리를 사용 할 수있는 옵션을 제공합니다.
예를 들어, Stedy와 마찬가지로 hist(faithful[, 2])
예제를 사용합니다.
histfaithful <- function(bins, col){
message("calling histfaithful with args ", bins, " and ", col)
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = bins + 1)
hist(x, breaks = bins, col = col, border = 'white')
}
shinyApp(
ui = fluidPage(
inputPanel(
sliderInput("bins", "bins", 5, 30, 10, 1),
selectInput("col", "color", c("blue", "red"))
),
cachePlotUI("cachedPlot")
),
server = function(input, output, session){
callModule(
cachePlot, "cachedPlot", histfaithful,
args = reactive(list(bins = input$bins, col = input$col))
)
}
)
'? renderImage'의 예제를 보면 아이디어를 얻을 수 있습니다. 기본적으로 PNG 파일을 반환하는 메모 기능을 원합니다. renderImage를 사용하여 메모 한 함수를 호출하십시오. –
감사합니다. renderImage를 통해 렌더링하는 정적 이미지의 크기를 자동으로 조절할 수있는 방법에 대한 아이디어. – Abhi