How to link radio button to display histogram

Hi can someone help me understand how when I click radio button and select option as "Yes" to display "myhist" and when option is "No" to display "mydata".
Data.frame - data
Names Value (Names- char, values- numeric)
John 50
Andy 20
Sam 30 ....

#library(DT) # when selecting multiple columns need to install this package
library(shiny)
library(ggplot2)
library(tidyverse)

ui <- fluidPage(
  titlePanel (""),
  sidebarLayout(
    sidebarPanel( (""),
                  selectInput("Names","Name",choices = c(data$Names),
                              selectize = FALSE, multiple = TRUE, selected = "John"),
                  radioButtons("Button", "Do you want to display the chart", 
                               list("Yes","No"),"") ) ,
    mainPanel( 
     tabsetPanel(type="tab",
                 tabPanel("Histogram", plotOutput("myhist")),
                  tabPanel("Data", tableOutput("mydata"))
      ) ) ) )

server <- function(input, output) {
  test <- reactive({
    if(input$Button == 'Yes'){
  output$myhist <- renderPlot({
    barplot(Data$Values[Data$Names %in% input$Names], xlab = "Count", names = input$Names) }
else { output$mydata <- renderTable({
    subset(Data, Data$Names == input$Name)
  })  }  }  
})  }
shinyApp(ui, server)
library(shiny)
 
library(tidyverse)

Data <- tribble(~Names,~Values,
                "John" ,50,
                   "Andy", 20,
                   "Sam", 30 )
ui <- fluidPage(
  titlePanel (""),
  sidebarLayout(
    sidebarPanel( (""),
                  selectInput("Names","Name",choices = c(Data$Names),
                              selectize = FALSE, multiple = TRUE, selected = "John"),
                  radioButtons("Button", "Do you want to display the chart", 
                               list("Yes","No"),"") ) ,
    mainPanel( 
      tabsetPanel(type="tab",
                  tabPanel("Histogram", plotOutput("myhist")),
                  tabPanel("Data", tableOutput("mydata"))
      ) ) ) )

server <- function(input, output) {
  observe({
    if(req(input$Button) == 'Yes'){
      output$myhist <- renderPlot({
        barplot(Data$Values[Data$Names %in% req(input$Names)], xlab = "Count", names = input$Names) }
   ) 
      output$mydata<-NULL
      }   else { output$mydata <- renderTable({
          subset(Data, Data$Names == req(input$Names))
      }) 
      output$myhist <-NULL}  }  
  )  }
shinyApp(ui, server)
1 Like

Sorry its working, i had a typo error....thanks for your help.

Awesome radio bottom and I have found great tips that function well

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

Thanks for your help. It worked.

I am noticing this error when I select one or two in a sequence, but when I try to select not in sequence, I am getting this error.
longer object length is not a multiple of shorter object length
Warning in Data$Names == req(input$Names) :

I used %in% too but yet getting the same error.

Do you mean that you select multiple names ?
If so == shouldn't be used, rather %in%

are you sure you have this ?

  subset(Data, Data$Names %in% req(input$Names))