Shiny:RMarkdown在闪亮应用程序内的If语句

时间:2016-06-16 07:39:56

标签: r shiny r-markdown

我很难弄清楚如何在.Rmd文件中使用if语句。我在stackoverflow上找不到任何东西......

我要解释这个闪亮的应用程序的例子:

library(shiny)
library(markdown)
library(knitr)
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('report.Rmd')
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)

      out <- rmarkdown::render('report.Rmd',
                               params = list(text = input$text),
                               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"),
  tabPanel("Data", 
           radioButtons('filter', h3(strong("Auswahlkriterien:")),
                        choices = list("WerkstoffNr" = 1, 
                                       "S-Gehalt" = 2),
                        selected = 1,inline=TRUE),
           conditionalPanel(
             condition = "input.filter == '1'", 
             column(6,
                    h4("WerkstoffNr auswaehlen:"),
                    selectInput("select", " ", 
                                choices = seq(1,100,10))),
             column(6,
                    h4("Abmessung auswaehlen:"),
                    selectInput("abmfrom", "Von:",choices=as.list(seq(20,110,10))),
                    selectInput("abmto", "Bis:",choices=as.list(seq(20,110,10))),
                    actionButton("button1", "Auswaehlen"))),
           conditionalPanel(
             condition = "input.filter == '2' ", 
             column(6,h4("S-Gehalt auswaehlen:"),
                    selectInput("sgehalt", "Von:",choices=seq(1,100,10)),
                    selectInput("sgehalt2", "Bis:",choices=seq(1,100,10))),
             column(6,h4("Abmessung auswaehlen:"),
                    selectInput("abmfrom2", "Von:",choices=as.list(seq(20,110,10))),
                    selectInput("abmto2", "Bis:",choices=as.list(seq(20,110,10)))))
  ),

  flowLayout(radioButtons('format', 'Document format', c('PDF','HTML', 'Word'),
                          inline = TRUE),
             downloadButton('downloadReport'))

)

shinyApp(ui = ui, server = server)

report.Rmd(就在此刻):

---
title: "Parameterized Report for Shiny"
output: html_document
params:
  text: 'NULL'
---

  # Some title

`r params[["text"]]`

我想在我的RMarkdown报告中获得这部分闪亮应用的输入:

tabPanel("Data", 
               radioButtons('filter', h3(strong("Auswahlkriterien:")),
                            choices = list("WerkstoffNr" = 1, 
                                           "S-Gehalt" = 2),
                            selected = 1,inline=TRUE),
               conditionalPanel(
                 condition = "input.filter == '1'", 
                 column(6,
                        h4("WerkstoffNr auswaehlen:"),
                        selectInput("select", " ", 
                                    choices = seq(1,100,10))),
                 column(6,
                        h4("Abmessung auswaehlen:"),
                        selectInput("abmfrom", "Von:",choices=as.list(seq(20,110,10))),
                        selectInput("abmto", "Bis:",choices=as.list(seq(20,110,10))),
                        actionButton("button1", "Auswaehlen"))),
               conditionalPanel(
                 condition = "input.filter == '2' ", 
                 column(6,h4("S-Gehalt auswaehlen:"),
                        selectInput("sgehalt", "Von:",choices=seq(1,100,10)),
                        selectInput("sgehalt2", "Bis:",choices=seq(1,100,10))),
                 column(6,h4("Abmessung auswaehlen:"),
                        selectInput("abmfrom2", "Von:",choices=as.list(seq(20,110,10))),
                        selectInput("abmto2", "Bis:",choices=as.list(seq(20,110,10)))))
      )

我们可以看到里面有一个If语句(关于过滤选项)。因此,它取决于用户希望用于过滤数据的选项。我想在我的报告中有这个选项。简单地说就像:

if input.filter == 1 
Werkstoffnummer: input$select 
Abmessung: von input$abmfrom bis input$abmto

else

S : von sgehalt bis sgehalt2 
Abmessung: von input$abmfrom2 bis input$abmto2

因此在报告中只会打印(如果input.filter == 1):

Werkstoffnummer:1

Abmessung:von 20 bis 30

非常感谢!

2 个答案:

答案 0 :(得分:1)

可能我不完全理解你,但你可以使用类似的东西 (示例在输入过滤器上打印不同的文本)

---
title: "Untitled"
runtime: shiny
output: html_document
---


```{r eruptions, echo=FALSE}
radioButtons('filter', h3(strong("Auswahlkriterien:")),
                        choices = list("WerkstoffNr" = 1, 
                                       "S-Gehalt" = 2),
                        selected = 1,inline=TRUE)

 conditionalPanel(
             condition = "input.filter == '1'", 
             column(6,
                    h4("WerkstoffNr auswaehlen:")
                            ))

            conditionalPanel(
             condition = "input.filter == '2' ", 
             column(6,h4("S-Gehalt auswaehlen:")))

```

或使用服务器端(渲染UI,如here

但你不能像静态html文件那样分享它:

*“注意:如果您熟悉R Markdown,您可能希望RStudio在工作目录中保存交互式文档的HTML版本。但是,这仅适用于静态HTML文档。每个交互式文档必须由管理文档的计算机。因此,交互式文档不能作为独立的HTML文件共享。“

更新

如果你想下载静态html 例子

report.rmd

---
title: "Untitled"
output: html_document
---

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



```{r ,echo=FALSE}
if(input$filter==1){
  h1(paste("1",input$ii))
}else{
  h1(paste("2",input$ii))
}

```

闪亮

library(shiny)

ui=shinyUI(fluidPage(
  radioButtons('filter', h3(strong("Auswahlkriterien:")),
               choices = list("WerkstoffNr" = 1, 
                              "S-Gehalt" = 2),
               selected = 1,inline=TRUE),
  numericInput("ii","1",0),
  downloadButton('downloadReport')
))

server=shinyServer(function(input, output) {



  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.',  'html'    )
    },

    content = function(file) {
      src <- normalizePath('report.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, 'report.Rmd')

      library(rmarkdown)
      out <- render('report.Rmd',  html_document())
      file.rename(out, file)
    }
  )

})

shinyApp(ui,server )

报告将包含1或2个单选按钮和ii输入

答案 1 :(得分:0)

听起来你想要的是一个模板生成报告。 R Markdown是一种用于漂亮打印报告的格式,而不是生成它们。

对于报告生成,有‹brew›。它允许您使用简单的模板语言生成任何文件(包括R Markdown)。在您的情况下,您可以执行以下操作:

<% if (input.filter == 1) { %>
… normal R Markdown code here!
<% } %>

将其另存为report.rmd.brew或类似内容;然后,在报告生成代码中,在呈现模板之前需要brew模板:

brew::brew('report.rmd.brew', 'report.rmd')

默认情况下,它会从当前环境中找到变量(可以配置)。