根据闪亮的输入有选择地显示标记格式的文本

问题描述 投票:0回答:1

我正在尝试创建一个Rmarkdown,该Rmarkdown可以根据向闪亮应用程序中的用户输入来显示不同数量的文本。我在下面做了一个基本的例子。有2个文本输入和一个复选框。如果复选框为false,则仅将第一个文本框的值打印到markdown上。如果该复选框为true,则将同时打印两个值。我希望两个文本框的输出都像第一个文本框输出一样。

Ui:

library(shiny)
library(shinyjs)

ui <- fluidPage(


  titlePanel("Hello Shiny!"),

  sidebarLayout(

    sidebarPanel(

      textInput(inputId = "text1", 
                label =  "1st text", value = "1st text"),

      checkboxInput(inputId = "checkBox", label = "Checkbox"),

      textInput(inputId = "input2", 
                  label =  "2nd text", value = "2nd text"),

      downloadButton("download", "Download button")

    ),

    mainPanel(

      verbatimTextOutput("checkBoxValue")

    )
  )
) 

服务器:

library(shiny)
library(shinyjs)

server <- function(input, output) {

output$checkBoxValue <- renderText(input$checkBox)


Text1Value <- reactive({input$text1})
BoxValue<- reactive(input$checkBox)
Input2Value <- reactive({input$input2})

output$download <- downloadHandler(
  filename = "Test.docx",
  content = function(file) {
    tempReport <- file.path(tempdir(), "TestRMD.Rmd")
    file.copy("TestRMD.Rmd", tempReport, overwrite = TRUE) 


    params = list(


      Text1Value = Text1Value(),
      BoxValue = BoxValue(),
      Input2Value = Input2Value()

    )

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

Rmarkdown:

---
title: "Test"
output: word_document
params: 
    Text1Value: NA
    BoxValue: NA
    Input2Value: NA
---

```{r echo= FALSE, message = FALSE, include = FALSE}

library(shiny)
library(knitr)
library(latex2exp)


Text1Value<- params$Text1Value
BoxValue<- params$BoxValue
Input2Value<- params$Input2Value

```

# Value for text 1 is `r Text1Value`

```{r, echo=FALSE}
if(BoxValue == TRUE){
"Value for input 2 is `r Input2Value`"
}

```

当前,我可以从第二个文本输出中获取该文本,使其有条件地显示,但是它看起来像R代码。我希望它以与第一个文本输入的输出相同的格式显示。

我将如何完成?

r shiny r-markdown
1个回答
0
投票
```{r, echo=FALSE, results='asis'} if(BoxValue == TRUE){ cat("# Value for input 2 is", Input2Value) } ```
© www.soinside.com 2019 - 2024. All rights reserved.