首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在r中使定制的可下载报告发亮?

如何在r中使定制的可下载报告发亮?
EN

Stack Overflow用户
提问于 2021-01-19 06:13:17
回答 1查看 511关注 0票数 0

因此,基本上,我的应用程序有10个不同的表和图表正在为用户生成结果。现在,我要做的是,为用户显示一个清单,然后要求用户选择他们想要包括在他们下载的报告中的项目。这将产生一个定制的报告,其中将只包括用户希望拥有的东西。因此,仅在选中复选框时,请帮助我完成将项添加到报表中的过程。我有这样的想法,我必须用r标记来生成这份报告,但我很想进一步澄清它。

app.R

代码语言:javascript
运行
复制
library(shiny)
library(rmarkdown)
server <- function(input, output) {
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      ))
    },
    content = function(file) {
      src <- normalizePath('test.Rmd')
      
      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'test.Rmd', overwrite = TRUE)
      
      out <- rmarkdown::render('test.Rmd',
                               params = list(text = input$text,outp=input$Try),
                               switch(input$format,
                                      PDF = pdf_document(), 
                                      HTML = html_document(), 
                                      Word = word_document()
                               ))
      file.rename(out, file)
    }
  )
}

ui <- fluidPage(
  tags$textarea(id="text", rows=20, cols=155, 
                placeholder="Some placeholder text"),
  
  flowLayout(radioButtons('format', 'Document format', c('HTML', 'Word'),
                          inline = TRUE),
             checkboxGroupInput("Try","Let's hope this works",choiceNames = list("include hi","include hey","include hello","include how are you"),choiceValues = list("HI","HEY","HELLO","HOW ARE YOU")),
             downloadButton('downloadReport'))
  
)

shinyApp(ui = ui, server = server)

test.Rmd

代码语言:javascript
运行
复制
---
title: "Parameterized Report for Shiny"
output: html_document
params:
  text: 'NULL'
  outp: 'NULL'
---

# Some title

`r params[["text"]]`
`r params[["outp"]]`

在这个过程中,我用id "Try“添加了checkboxGroupInput,并将它的值作为输出。所以,我已经到了一半,除了现在,我只需要得到作为输出的地块,而不是选择评价。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-19 12:52:09

你的代码看起来不错。只需将数据作为一个参数传递,并创建情节,就像在您闪亮的应用程序中一样。您可以使用默认的绘图函数,但也可以使用ggplot2plotly。若要包含/排除绘图/表,还应将复选框值input$Try作为参数传递给报表,并使用条件添加绘图/表,如下所示。

我只发布了Rmd和我在你闪亮的应用程序中改变的线条。其他一切看起来都很好。

代码语言:javascript
运行
复制
server <- function(input, output) {
    output$downloadReport <- downloadHandler(
        filename = function() {
            paste('my-report', sep = '.', switch(
                input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
            ))
        },
        content = function(file) {
            src <- normalizePath('test.Rmd')
            # temporarily switch to the temp dir, in case you do not have write
            # permission to the current working directory
            owd <- setwd(tempdir())
            on.exit(setwd(owd))
            file.copy(src, 'test.Rmd', overwrite = TRUE)
            
            include <- input$Try
            out <- rmarkdown::render('test.Rmd',
                                     params = list(tbl = mtcars, include = include),
                                     switch(input$format,
                                            PDF = pdf_document(), 
                                            HTML = html_document(), 
                                            Word = word_document()
                                     ))
            file.rename(out, file)
        }
    )
}

更新的R标记:

代码语言:javascript
运行
复制
---
title: "Parameterized Report for Shiny"
output: html_document
params:
  tbl: NA
  include: NA
---

```{r echo=FALSE}

图书馆(针织品)

tbl <- params$tbl

包含<- params$include

代码语言:javascript
运行
复制
```{r echo=FALSE, results='asis'}

如果(%中包含‘HI’%){

代码语言:javascript
运行
复制
kable(tbl[1:5,], caption= 'A table')

}

代码语言:javascript
运行
复制
```{r echo=FALSE, result='asis'}

如果(%中包含‘嘿’%){

代码语言:javascript
运行
复制
plot(tbl$mpg, tbl$cyl)

}

代码语言:javascript
运行
复制

我真正喜欢的是,您不必每次更改报告时都重新启动闪亮的应用程序,因为它独立于应用程序本身。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65786335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档