2017-07-25 82 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中的字符串變量。 –