在交互式文档中,是否可以使用一组闪亮的代码来隐藏/显示降价?
我想做的一个简单例子是:
---
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变量。
答案 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
。