gpt4 book ai didi

r - 通过 Shiny 编织文件的进度条

转载 作者:行者123 更新时间:2023-12-01 23:08:05 25 4
gpt4 key购买 nike

我试图在我 Shiny 的 downloadHandler() 周围放置一个进度条。进度条应该显示 rmarkdown HTML 的渲染状态

我在 GitHub ( https://github.com/rstudio/shiny/issues/1660 ) 上找到了此信息,但无法使其正常工作。如果我没有定义环境,则无法编织该文件。

app.R

library(shiny)
library(rmarkdown)

ui <- fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
downloadButton("report", "Generate report"),
textOutput("checkrender")
)
server <- function(input, output, session) {
output$checkrender <- renderText({
if (identical(rmarkdown::metadata$runtime, "shiny")) {
TRUE
} else {
FALSE
}
})

output$report <- downloadHandler(
filename = "report.html",
content = function(file) {

tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)

params <- list(n = input$slider)

rmarkdown::render(tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}

shinyApp(ui = ui, server = server)

报告.Rmd
---
title: "Dynamic report"
output: html_document
params:
n: NA
---

```{r}
params$n
```

A plot of `params$n` random points.

```{r}
plot(rnorm(params$n), rnorm(params$n))
```

最佳答案

您的解决方案非常接近!

我看到你的代码有两个问题:

  • 您在 withProgress 代码
  • 中遗漏了 downloadHandler 调用
  • 测试您是否在 Shiny 的环境中运行 if (identical(rmarkdown::metadata$runtime, "shiny")) 需要进入您的 .Rmd 文件。您在此测试中包含任何用于递增/设置进度条的调用,否则 .Rmd 代码将产生类似 Error in shiny::setProgress(0.5) : 'session' is not a ShinySession object.
  • 的错误

    您的代码的以下返工应该有效:

    app.R
    library(shiny)
    library(rmarkdown)

    ui <- fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate report"),
    textOutput("checkrender")
    )
    server <- function(input, output, session) {
    output$checkrender <- renderText({
    if (identical(rmarkdown::metadata$runtime, "shiny")) {
    TRUE
    } else {
    FALSE
    }
    })

    output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
    withProgress(message = 'Rendering, please wait!', {
    tempReport <- file.path(tempdir(), "report.Rmd")
    file.copy("report.Rmd", tempReport, overwrite = TRUE)

    params <- list(n = input$slider)

    rmarkdown::render(
    tempReport,
    output_file = file,
    params = params,
    envir = new.env(parent = globalenv())
    )
    })
    }
    )
    }

    shinyApp(ui = ui, server = server)

    报告.Rmd
    ---
    title: "Dynamic report"
    output: html_document
    params:
    n: NA
    ---

    ```{r}
    params$n

    if (identical(rmarkdown::metadata$runtime, "shiny"))
    shiny::setProgress(0.5) # set progress to 50%
    ```

    A plot of `params$n` random points.

    ```{r}
    plot(rnorm(params$n), rnorm(params$n))

    if (identical(rmarkdown::metadata$runtime, "shiny"))
    shiny::setProgress(1) # set progress to 100%
    ```

    关于r - 通过 Shiny 编织文件的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55469928/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com