将图像上传到 R shiny 可下载为 pdf
Upload an image to R shiny downloadable to pdf
我正在尝试将图像上传到 R shiny 并将图像下载到报告中
app.r
但是当我编织报告时,它给我的是图像数据而不是图像
关于我在这里做错了什么有什么想法吗?一如既往地提前谢谢你
library(shiny)
ui <- fluidPage(
titlePanel('Upload Image Test'),
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "file1",
label = "Upload Image",
accept = c('image/png', 'image/jpeg','image/jpg')
),
tags$hr()),
mainPanel(
textOutput("filename"),
imageOutput(outputId = "uploaded_image"),
downloadButton("report", "Generate report")
)
)
)
server <- function(input, output) {
re1 <- reactive({gsub("\\", "/", input$file1$datapath)})
output$uploaded_image <- renderImage({list(src = re1())})
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(n = input$slider, image = input$file1)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
shinyApp(ui, server)
report.Rmd
---
title: "report"
author: "Author"
date: "4/14/2019"
output: pdf_document
params:
image: "Null"
n: "Null"
---
```{r}
params$image
params$n
问题是 RenderImage 会在使用后删除文件,除非 deleteFile = False。这是要更改的行,删除它:
re1 <- reactive({gsub("\\", "/", input$file1$datapath)})
添加这个:
output$uploaded_image <- renderImage({list(src = input$file1$datapath)}, deleteFile = FALSE)
我正在尝试将图像上传到 R shiny 并将图像下载到报告中 app.r 但是当我编织报告时,它给我的是图像数据而不是图像
关于我在这里做错了什么有什么想法吗?一如既往地提前谢谢你
library(shiny)
ui <- fluidPage(
titlePanel('Upload Image Test'),
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "file1",
label = "Upload Image",
accept = c('image/png', 'image/jpeg','image/jpg')
),
tags$hr()),
mainPanel(
textOutput("filename"),
imageOutput(outputId = "uploaded_image"),
downloadButton("report", "Generate report")
)
)
)
server <- function(input, output) {
re1 <- reactive({gsub("\\", "/", input$file1$datapath)})
output$uploaded_image <- renderImage({list(src = re1())})
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(n = input$slider, image = input$file1)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
shinyApp(ui, server)
report.Rmd
---
title: "report"
author: "Author"
date: "4/14/2019"
output: pdf_document
params:
image: "Null"
n: "Null"
---
```{r}
params$image
params$n
问题是 RenderImage 会在使用后删除文件,除非 deleteFile = False。这是要更改的行,删除它:
re1 <- reactive({gsub("\\", "/", input$file1$datapath)})
添加这个:
output$uploaded_image <- renderImage({list(src = input$file1$datapath)}, deleteFile = FALSE)