用户是否可以选择是否要在Shiny中查看代码?

huangapple go评论89阅读模式
英文:

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(&quot;ShowCode&quot;, label = &quot;Do you want to see the code?&quot;,
              choices = c(&quot;TRUE&quot;, &quot;FALSE&quot;), selected = &quot;FALSE&quot;)
)
ShowC &lt;- 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包、aceEditorshinyAce。这是一个使用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: &quot;Untitled&quot;
    author: &quot;St&#233;phane Laurent&quot;
    date: &quot;2023-07-14&quot;
    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 &lt;- &#39;
    inputPanel(
      selectInput(&quot;n_breaks&quot;, label = &quot;Number of bins:&quot;,
                  choices = c(10, 20, 35, 50), selected = 20),
      
      sliderInput(&quot;bw_adjust&quot;, label = &quot;Bandwidth adjustment:&quot;,
                  min = 0.2, max = 2, value = 1, step = 0.2)
    )
    
    renderPlot({
      hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
           xlab = &quot;Duration (minutes)&quot;, main = &quot;Geyser eruption duration&quot;)
      
      dens &lt;- density(faithful$eruptions, adjust = input$bw_adjust)
      lines(dens, col = &quot;blue&quot;)
    })
    &#39;
    ```
    
    
    ```{r, echo=FALSE}
    inputPanel(
      checkboxInput(&quot;show&quot;, &quot;Show code&quot;)
    )
    inputPanel(
      conditionalPanel(
        condition = &quot;input.show&quot;,
        monaco(
          contents = code,
          language = &quot;r&quot;,
          width = &quot;800px&quot;, height = &quot;400px&quot;
        )
      )
    )
    inputPanel(
      selectInput(&quot;n_breaks&quot;, label = &quot;Number of bins:&quot;,
                  choices = c(10, 20, 35, 50), selected = 20),
      
      sliderInput(&quot;bw_adjust&quot;, label = &quot;Bandwidth adjustment:&quot;,
                  min = 0.2, max = 2, value = 1, step = 0.2)
    )
    
    renderPlot({
      hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
           xlab = &quot;Duration (minutes)&quot;, main = &quot;Geyser eruption duration&quot;)
      
      dens &lt;- density(faithful$eruptions, adjust = input$bw_adjust)
      lines(dens, col = &quot;blue&quot;)
    })
    ```

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/2GzN9.gif

</details>



huangapple
  • 本文由 发表于 2023年7月13日 00:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定