在闪亮的文档中显示文本

时间:2017-07-25 12:52:18

标签: r shiny

在交互式文档中,是否可以使用一组闪亮的代码来隐藏/显示降价?

我想做的一个简单例子是:

---
title: "Example"
output: html_document
runtime: shiny
---
What is $2+2$?

```{r reveal, echo=FALSE}
actionButton("button", "Reveal solution")
#Try (unsuccessfully) to comment out rest of document
renderUI(HTML(ifelse(input$button, "", ("<!--"))))
```

The answer is $4$.

在我的实际用例中,问题和答案很长,都涉及一些共享的随机生成的R变量。

1 个答案:

答案 0 :(得分:1)

以下是适用于您的代码:

---
title: "Example"
output: html_document
runtime: shiny
---
What is $2+2$?

```{r reveal, echo=FALSE}
library(shiny)
actionButton("button", "Reveal solution")
#Try (unsuccessfully) to comment out rest of document
renderText({if(input$button == 0) {NULL
}else{
print("The answer is 4")}})
```

如果我理解正确您希望在按下2 + 2后获得actionButton的解决方案,因此我使用if...else...语句说明actionButton == 0的价值,它应该返回NULL,否则应该打印文本:The answer is 4