英文:
Is it possible to allow the user to choose if they want to see the code in Shiny?
问题
我通常与不关心分析底层代码的人合作。因此,通常我会隐藏代码。但我想允许用户在需要时查看代码。
我已经尝试过:
{r, echo=FALSE}
inputPanel(
selectInput("ShowCode", label = "Do you want to see the code?",
choices = c("TRUE", "FALSE"), selected = "FALSE")
)
ShowC <- renderText({input$ShowCode})
ShowC
在下一个代码块中,我设置了选项:
{r, echo = ShowC}
#任意代码
第一个块正常允许用户选择TRUE或FALSE,第二个块显示或不显示代码,取决于默认选择的选项,但不会根据用户的选择而更改。
英文:
I usually collaborate with people who is not interested in the underlying code of analyses. So usually I hide the code. But I'd like to allow the user to see the code if they want.
I have tried:
{r, echo=FALSE}
inputPanel(
selectInput("ShowCode", label = "Do you want to see the code?",
choices = c("TRUE", "FALSE"), selected = "FALSE")
)
ShowC <- renderText({input$ShowCode})
ShowC
In the next code chunk I set as options:
{r, echo = ShowC}
#Whatevercode
The first chunk properly allows the user to choose TRUE or FALSE, the second one shows the code or not depending on the option selected by default, but doesn't change with the user choice.
答案1
得分: 0
不使用代码块来显示代码,您可以在Shiny应用程序中显示它,这样可以通过ConditionalPanel
轻松显示/隐藏。要显示漂亮的代码,您可以使用monaco包、aceEditor或shinyAce。这是一个使用monaco的示例:
---
title: "Untitled"
author: "Stéphane Laurent"
date: "2023-07-14"
output: html_document
runtime: shiny
---
要了解更多信息,请参阅[交互式文档](http://rmarkdown.rstudio.com/authoring_shiny.html).
```{r, include=FALSE}
library(shiny)
library(monaco)
code <- '
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
'
inputPanel(
checkboxInput("show", "Show code")
)
inputPanel(
conditionalPanel(
condition = "input.show",
monaco(
contents = code,
language = "r",
width = "800px", height = "400px"
)
)
)
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
[![enter image description here][1]][1]
请注意,上面的示例代码包含了Markdown和R代码,用于创建一个Shiny应用程序,以漂亮的方式显示代码。
<details>
<summary>英文:</summary>
Instead of using a chunk to display the code, you can display it in the Shiny app, in this way it is easy to show/hide with the help of `ConditionalPanel`. To display a stylish code, you can use the **monaco** package, or **aceEditor**, or **shinyAce**. Here is an example with **monaco**:
---
title: "Untitled"
author: "Stéphane Laurent"
date: "2023-07-14"
output: html_document
runtime: shiny
---
To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).
```{r, include=FALSE}
library(shiny)
library(monaco)
code <- '
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
'
```
```{r, echo=FALSE}
inputPanel(
checkboxInput("show", "Show code")
)
inputPanel(
conditionalPanel(
condition = "input.show",
monaco(
contents = code,
language = "r",
width = "800px", height = "400px"
)
)
)
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/2GzN9.gif
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论