2017-07-25 78 views
0

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

的想什么,我做了一个简单的例子是:

--- 
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

下面是应为你工作的代码:

--- 
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")}}) 
``` 

如果我理解正确的话,您想按actionButton后得到的2 + 2的解决方案,因此我已经使用了if...else...声明称,如果价值actionButton == 0,应该返回NULL,否则应该打印文本:The answer is 4

+0

谢谢,这确实是一个可行的解决方案。问题是,在我的真实应用程序中,答案可能会很长,而不是一句话。所以我想保留它作为普通的markdown,而不是将它变成R中的字符串变量。 –