'Show a chart based on a condition

I have the following code, where you can enter a string, click a button and get a result depending on the string entered:

# ui.R

ui <- fluidPage(
    conditionalPanel(
        "input.btn_input === 0",
        textInput("caption", "Caption", "abc"),
        actionButton("btn_input", "Enter")
    ),
    conditionalPanel(
        "input.btn_input !== 0",
        style = "display: none;",
        verbatimTextOutput("value")
    )
)

# server.R

server <- function(input, output) {
    
    Response <- eventReactive(input$btn_input, {
        v <- input$caption
        if (v == "xxx") {
            # Allow to use the chart
        } else {
            "Error"
        }
    })
    
    output$value <- renderPrint({
        Response()
    })
}

Now, instead of # Allow to use the chart I want to insert the chart contained in the Shiny example, that is:

ui <- fluidPage(

    titlePanel("Old Faithful Geyser Data"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        mainPanel(
           plotOutput("distPlot")
        )
    )
)

server <- function(input, output) {

    output$distPlot <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}
 
shinyApp(ui = ui, server = server)

Can anyone suggest me how to do it? The best I have been able to do is show the sidebar panel. Thanks in advance.



Solution 1:[1]

We can use shinyjs package to show and hide elements based on conditions.

For example, we can wrap the sidebarLayout with a div to give it a unique id parameter. Here's an example code:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  titlePanel("Old Faithful Geyser Data"),
  #initialize shinyjs
  shinyjs::useShinyjs(),
  textInput("caption", "Caption", "abc"),
  hidden(verbatimTextOutput("value")),
  shinyjs::hidden(div(
    id = "sidebar_part",
    sidebarLayout(
      sidebarPanel(
        sliderInput("bins",
          "Number of bins:",
          min = 1,
          max = 50,
          value = 30
        )
      ),
      mainPanel(
        plotOutput("distPlot")
      )
    )
  ))
)

server <- function(input, output) {

  # condition to show the sidebarLayout
  observeEvent(input$caption,
    {
      if (input$caption == "xxx") {
        shinyjs::hide("caption")
        shinyjs::show("sidebar_part")
        shinyjs::hide("value")
      } else {
        shinyjs::show("value")
        output$value <- renderText({
          "error"
        })
      }
    },
    ignoreInit = TRUE
  )

  output$distPlot <- renderPlot({
    x <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "darkgray", border = "white")
  })
}

shinyApp(ui = ui, server = server)

enter image description here

Edit:

Using an actionButton and hiding everything except the error when the incorrect answer is provided.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  titlePanel("Old Faithful Geyser Data"),
  # initialize shinyjs
  shinyjs::useShinyjs(),
  div(
    id = "caption_and_send",
    textInput("caption", "Caption", "abc"),
    actionButton("send", "Send")
  ),
  hidden(verbatimTextOutput("value")),
  shinyjs::hidden(div(
    id = "sidebar_part",
    sidebarLayout(
      sidebarPanel(
        sliderInput("bins",
          "Number of bins:",
          min = 1,
          max = 50,
          value = 30
        )
      ),
      mainPanel(
        plotOutput("distPlot")
      )
    )
  ))
)

server <- function(input, output) {

  # condition to show the sidebarLayout
  observeEvent(input$send,
    {
      if (input$caption == "xxx") {
        shinyjs::hide("caption_and_send")
        shinyjs::show("sidebar_part")
      } else {
        shinyjs::hide("caption_and_send")
        shinyjs::show("value")
        output$value <- renderText({
          "error"
        })
      }
    },
    ignoreInit = TRUE
  )

  output$distPlot <- renderPlot({
    x <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "darkgray", border = "white")
  })
}

shinyApp(ui = ui, server = server)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1