我闪亮的应用程序不会在降价中呈现 R 代码

My shiny app does not render R code in markdown

我有一个问题,我想通过我的 Shiny 应用程序呈现 markdown 文件。我是 运行 RStudio 服务器,在 Linux Red Hat 上。我以前成功使用过下面的代码,但是由于某些原因现在它不起作用。

问题是 R 代码没有呈现,见下文。当直接通过 .Rmd 文件渲染时,它按预期工作。 app.Rprot.Rmd 都位于同一文件夹中。

通过 Shiny 应用呈现的 Markdown 在 RStudio 中呈现的 Markdown

闪亮的应用程序

library(shiny)
library(rmarkdown)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Knit"),

    downloadButton("prot", "Render file")
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$prot <- downloadHandler(
        
        # For PDF output, change this to "report.pdf"
        filename = "prot.html",
        
        content = function(file) {
            
            report_path <- tempfile(fileext = ".html")
            file.copy("prot.Rmd", report_path, overwrite = T)
            
            
            rmarkdown::render(report_path,
                              output_file = file,
                              envir = new.env(parent = globalenv())
            )
            
        }
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

Rmd-文件

---
title: "tst2"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

会话信息

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux 8.5 (Ootpa)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblas-r0.3.12.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

我认为问题在于,您正在将 prot.Rmd 复制到带有 fileext = ".html" 的临时文件中 - 它应该是 ".Rmd".

请尝试以下操作:

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Knit"),

    downloadButton("prot", "Render file")
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$prot <- downloadHandler(
      
        # For PDF output, change this to "report.pdf"
        filename = "prot.html",
        
        content = function(file) {
            
            report_path <- tempfile(fileext = ".Rmd")
            file.copy("prot.Rmd", report_path, overwrite = TRUE)
            
            
            rmarkdown::render(report_path,
                              output_file = file)
            
        }
    )
}

# Run the application 
shinyApp(ui = ui, server = server)