How to show a drop down menu based on what user selects from radio button


#1

i have the following code. and i am struck. i want the first page to display just the radio buttons and based on which radio button user click, options should be displayed. for example, when user selects distributor wise, radio button, it should display drop down menu of the distributors and then display the graph based on what is selected in the drop down. i have coded the part of drop down menu, and to display graphs based on user input in the drop down, but unable to integrate the part of displaying specific menu based on radio button click from user.
my code is here

#install.packages("")
library(shiny)
library(gdata)
library(dplyr)
library(ggplot2)
library(shiny)


mydata1 = read.csv("C:\\Users\\BPO18\\Documents\\Book1.csv")

mydata<-c( "Distributor wise", "Outlet type wise", "Product name", "Product code wise", "Salesman",  "State")

# subset(mydata1, row.names(mydata1) %in% mydata) 

css <- "
#large .selectize-input { line-height: 40px; }
#large .selectize-dropdown { line-height: 30px; }"


ui <- fluidPage(
  
  tags$style(type='text/css', css),
  
  titlePanel("Hello User"),
    
    sidebarLayout(
      
      sidebarPanel(
      
        radioButtons("rb", "Please select an option",
                   choices = c(
                     "Distributor wise", "Outlet type wise", "Product name", "Product code wise", "Salesman",  "State")
      )
    ),
    mainPanel(
      uiOutput("radiobuttonChoiceOutput")
      
    )
  )
)

shinyServer <- function(input, output, session) {
   # plottype<- reactive({
   #   switch(input$type,
   #          "Distributor wise" = "s1", 
   #          "Outlet type wise" ="s2",
   #          "Product name" = "s3",
   #          "Product code wise" = "s4",
   #          "Salesman" = "s5",
   #          "State" = "s6")
   # })
   # 
   # output$plotoptions <- renderPrint({
   #   print(radiobuttons("type",
   #                      "choose : ",
   #                      c("Distributor wise", 
   #                        "Outlet type wise", 
   #                        "Product name", 
   #                        "Product code wise", 
   #                        "Salesman",  
   #                        "State"),
   #                      inline = TRUE
   #                      )
   #         )
   # })
  
  # plotOutput(outputId="salesplot"),   #placeholder
  # br(), br(),
  # plotOutput(outputId="salesplot1"),
  # br(), br(),
  # plotOutput(outputId="salesplot2"),
  # br(), br(),
  # plotOutput("results"),
  # br(), br(),
  # plotOutput("results1"),
  # br(), br(),
  # plotOutput("results2"))
  #   

   # if(plottype=="s1")
   # {
  
  output$radiobuttonChoiceOutput <- renderUI({
     tagList(
    checkboxGroupInput("distributorInput", 
                        h3("Distributor-wise"), 
                        choices = unique(mydata1$distributor_name),
                        selected = mydata1$distributor_name[1]),
   # }
   # if(plottype=="s2")
   # {
     checkboxGroupInput("outlettpyeInput", 
                        h3("Outlet type wise"), 
                        choices = unique(mydata1$outlet_type),
                        selected = mydata1$outlet_type[1]),
   # }
   # 
   # if(plottype=="s3")
   # {
     checkboxGroupInput("typeInput", 
                        h3("Product type"), 
                        choices = unique(mydata1$prod_name), 
                        selected = mydata1$prod_name[1]),
   # }
   # 
   # if(plottype=="s4")
   # {
     checkboxGroupInput("prodcodeInput",h3("Product code wise"), 
                        choices = unique(mydata1$prod_code),
                        selected = mydata1$prod_code[1]),
   # }
   # 
   # if(plottype=="s5")
   # {
     div (id="large",
          selectInput("salesmanInput", label="select the salesmen", 
                      choices=unique(mydata1$salesman), 
                      selected = mydata1$salesman[1])),
   # }
   # 
   # if(plottype=="s6")
   # {
     checkboxGroupInput("stateInput", 
                        h3("States"), 
                        choices = unique(mydata1$state),
                        selected = mydata1$state[1]))
  })
   # }
 
     output$results <- renderPlot({
       filtered <-
         mydata1 %>%
         dplyr :: filter(distributor_name == input$distributorInput
         )
       ggplot(filtered, aes(total_sales)) +
         geom_histogram(fill=I("red"),
                        col=I("green") )
     })
   
  
  output$results2 <- renderPlot({
       filtered <-
         mydata1 %>%
       dplyr :: filter(outlet_type == input$outlettpyeInput
         )
       ggplot(filtered, aes(total_sales)) +
         geom_histogram(fill=I("red"),
                        col=I("green") )
     })
  
 output$salesplot1 <- renderPlot({
            filtered <-
              mydata1 %>%
              
              filter(
                prod_name == input$typeInput
              )
            
            ggplot(filtered, aes(total_sales)) +
              geom_histogram(fill=I("blue"),
                             col=I("yellow") )
          })
        
    output$results1 <- renderPlot({
      filtered <-
        mydata1 %>%
        filter(prod_code == input$prodcodeInput
        )
      ggplot(filtered, aes(total_sales)) +
        geom_histogram(fill=I("red"),
                       col=I("green") )
    })
    
      output$salesplot <- renderPlot({
        
        filtered <-
          mydata1 %>%
          
          filter(salesman == input$salesmanInput
          )
        ggplot(filtered, aes(total_sales)) +
          geom_histogram(fill=I("darkolivegreen1"),
                         col=I("violet") )
      })
      

        output$salesplot2 <- renderPlot({
          
          filtered <-
            mydata1 %>%
            
            filter( 
              state == input$stateInput
            )
          ggplot(filtered, aes(total_sales)) +
            geom_histogram(fill=I("orange"), 
                           col=I("blue") )
        })
}

#         mainPanel(
#           
# }
  
shinyApp(ui = ui, server = shinyServer)

#2

i have the following code. and i am struck. i want the first page to display just the radio buttons and based on which radio button user click, options should be displayed. for example, when user selects distributor wise, radio button, it should display drop down menu of the distributors and then display the graph based on what is selected in the drop down. i have coded the part of drop down menu, and to display graphs based on user input in the drop down, but unable to integrate the part of displaying specific menu based on radio button click from user.
my code is here

#install.packages("")
library(shiny)
library(gdata)
library(dplyr)
library(ggplot2)
library(shiny)
mydata1 = read.csv("C:\\Users\\BPO18\\Documents\\Book1.csv")
mydata<-c( "Distributor wise", "Outlet type wise", "Product name", "Product code wise", "Salesman",  "State")
# subset(mydata1, row.names(mydata1) %in% mydata) 

css <- "
#large .selectize-input { line-height: 40px; }
#large .selectize-dropdown { line-height: 30px; }"

ui <- fluidPage(
  
  tags$style(type='text/css', css),
  
  titlePanel("Hello User"),
    
    sidebarLayout(
      
      sidebarPanel(
      
        radioButtons("rb", "Please select an option",
                   choices = c(
                     "Distributor wise", "Outlet type wise", "Product name", "Product code wise", "Salesman",  "State")
      )
    ),
    mainPanel(
      uiOutput("radiobuttonChoiceOutput")
      
    )
  )
)

shinyServer <- function(input, output, session) {
   # plottype<- reactive({
   #   switch(input$type,
   #          "Distributor wise" = "s1", 
   #          "Outlet type wise" ="s2",
   #          "Product name" = "s3",
   #          "Product code wise" = "s4",
   #          "Salesman" = "s5",
   #          "State" = "s6")
   # })
   # 
   # output$plotoptions <- renderPrint({
   #   print(radiobuttons("type",
   #                      "choose : ",
   #                      c("Distributor wise", 
   #                        "Outlet type wise", 
   #                        "Product name", 
   #                        "Product code wise", 
   #                        "Salesman",  
   #                        "State"),
   #                      inline = TRUE
   #                      )
   #         )
   # })
  
  # plotOutput(outputId="salesplot"),   #placeholder
  # br(), br(),
  # plotOutput(outputId="salesplot1"),
  # br(), br(),
  # plotOutput(outputId="salesplot2"),
  # br(), br(),
  # plotOutput("results"),
  # br(), br(),
  # plotOutput("results1"),
  # br(), br(),
  # plotOutput("results2"))
  #   

   # if(plottype=="s1")
   # {
  
  output$radiobuttonChoiceOutput <- renderUI({
     tagList(
    checkboxGroupInput("distributorInput", 
                        h3("Distributor-wise"), 
                        choices = unique(mydata1$distributor_name),
                        selected = mydata1$distributor_name[1]),
   # }
   # if(plottype=="s2")
   # {
     checkboxGroupInput("outlettpyeInput", 
                        h3("Outlet type wise"), 
                        choices = unique(mydata1$outlet_type),
                        selected = mydata1$outlet_type[1]),
   # }
   # 
   # if(plottype=="s3")
   # {
     checkboxGroupInput("typeInput", 
                        h3("Product type"), 
                        choices = unique(mydata1$prod_name), 
                        selected = mydata1$prod_name[1]),
   # }
   # 
   # if(plottype=="s4")
   # {
     checkboxGroupInput("prodcodeInput",h3("Product code wise"), 
                        choices = unique(mydata1$prod_code),
                        selected = mydata1$prod_code[1]),
   # }
   # 
   # if(plottype=="s5")
   # {
     div (id="large",
          selectInput("salesmanInput", label="select the salesmen", 
                      choices=unique(mydata1$salesman), 
                      selected = mydata1$salesman[1])),
   # }
   # 
   # if(plottype=="s6")
   # {
checkboxGroupInput("stateInput", 
                        h3("States"), 
                        choices = unique(mydata1$state),
                        selected = mydata1$state[1]))
  })
   # }
 
     output$results <- renderPlot({
       filtered <-
         mydata1 %>%
         dplyr :: filter(distributor_name == input$distributorInput
         )
       ggplot(filtered, aes(total_sales)) +
         geom_histogram(fill=I("red"),
                        col=I("green") )
     })
   
  
  output$results2 <- renderPlot({
       filtered <-
         mydata1 %>%
       dplyr :: filter(outlet_type == input$outlettpyeInput
         )
       ggplot(filtered, aes(total_sales)) +
         geom_histogram(fill=I("red"),
                        col=I("green") )
     })
  
 output$salesplot1 <- renderPlot({
            filtered <-
              mydata1 %>%
              
              filter(
                prod_name == input$typeInput
              )
            
            ggplot(filtered, aes(total_sales)) +
              geom_histogram(fill=I("blue"),
                             col=I("yellow") )
          })
        
    output$results1 <- renderPlot({
      filtered <-
        mydata1 %>%
        filter(prod_code == input$prodcodeInput
        )
      ggplot(filtered, aes(total_sales)) +
        geom_histogram(fill=I("red"),
                       col=I("green") )
    })
    
      output$salesplot <- renderPlot({
        
        filtered <-
          mydata1 %>%
          
          filter(salesman == input$salesmanInput
          )
        ggplot(filtered, aes(total_sales)) +
          geom_histogram(fill=I("darkolivegreen1"),
                         col=I("violet") )
      })
      

        output$salesplot2 <- renderPlot({
          
          filtered <-
            mydata1 %>%
            
            filter( 
              state == input$stateInput
            )
          ggplot(filtered, aes(total_sales)) +
            geom_histogram(fill=I("orange"), 
                           col=I("blue") )
        })
}

#         mainPanel(
#           
# }
  
shinyApp(ui = ui, server = shinyServer)
[/quote]

* 1. List item

library(shiny)

mydata1 = read.csv(“C:\Users\BPO18\Documents\Book1.csv”)

mydata&lt;-c( “Distributor wise”, “Outlet type wise”, “Product name”, “Product code wise”, “Salesman”,  “State”)

subset(mydata1, row.names(mydata1) %in% mydata)
css &lt;- “

#large .selectize-input { line-height: 40px; }

#large .selectize-dropdown { line-height: 30px; }”

ui &lt;- fluidPage(

tags$style(type=‘text/css’, css),

titlePanel(“Hello User”),

sidebarLayout(

  sidebarPanel(

    radioButtons("rb", "Please select an option",
               choices = c(
                 "Distributor wise", "Outlet type wise", "Product name", "Product code wise", "Salesman",  "State")
  )
),
mainPanel(
  uiOutput("radiobuttonChoiceOutput")

)

)

)

shinyServer &lt;- function(input, output, session) {

plottype&lt;- reactive({
switch(input$type,
“Distributor wise” = “s1”,
“Outlet type wise” =“s2”,
“Product name” = “s3”,
“Product code wise” = “s4”,
“Salesman” = “s5”,
“State” = “s6”)
})

output$plotoptions &lt;- renderPrint({
print(radiobuttons(“type”,
"choose : ",
c(“Distributor wise”,
“Outlet type wise”,
“Product name”,
“Product code wise”,
“Salesman”,
“State”),
inline = TRUE
)
)
})
plotOutput(outputId=“salesplot”),   #placeholder

br(), br(),
plotOutput(outputId=“salesplot1”),
br(), br(),
plotOutput(outputId=“salesplot2”),
br(), br(),
plotOutput(“results”),
br(), br(),
plotOutput(“results1”),
br(), br(),
plotOutput(“results2”))

if(plottype==“s1”)
{
output$radiobuttonChoiceOutput &lt;- renderUI({

tagList(

checkboxGroupInput(“distributorInput”,

h3(“Distributor-wise”),

choices = unique(mydata1$distributor_name),

selected = mydata1$distributor_name[1]),

}
if(plottype==“s2”)
{
 checkboxGroupInput("outlettpyeInput", 
                    h3("Outlet type wise"), 
                    choices = unique(mydata1$outlet_type),
                    selected = mydata1$outlet_type[1]),

}

if(plottype==“s3”)
{
 checkboxGroupInput("typeInput", 
                    h3("Product type"), 
                    choices = unique(mydata1$prod_name), 
                    selected = mydata1$prod_name[1]),

}

if(plottype==“s4”)
{
 checkboxGroupInput("prodcodeInput",h3("Product code wise"), 
                    choices = unique(mydata1$prod_code),
                    selected = mydata1$prod_code[1]),

}

if(plottype==“s5”)
{
 div (id="large",
      selectInput("salesmanInput", label="select the salesmen", 
                  choices=unique(mydata1$salesman), 
                  selected = mydata1$salesman[1])),

}

if(plottype==“s6”)
{
 checkboxGroupInput("stateInput", 
                    h3("States"), 
                    choices = unique(mydata1$state),
                    selected = mydata1$state[1]))

})

}
 output$results &lt;- renderPlot({
   filtered &lt;-
     mydata1 %&gt;%
     dplyr :: filter(distributor_name == input$distributorInput
     )
   ggplot(filtered, aes(total_sales)) +
     geom_histogram(fill=I("red"),
                    col=I("green") )
 })

output$results2 &lt;- renderPlot({

filtered &lt;-

mydata1 %&gt;%

dplyr :: filter(outlet_type == input$outlettpyeInput

)

ggplot(filtered, aes(total_sales)) +

geom_histogram(fill=I(“red”),

col=I(“green”) )

})

output$salesplot1 &lt;- renderPlot({

filtered &lt;-

mydata1 %&gt;%

          filter(
            prod_name == input$typeInput
          )

        ggplot(filtered, aes(total_sales)) +
          geom_histogram(fill=I("blue"),
                         col=I("yellow") )
      })

output$results1 &lt;- renderPlot({
  filtered &lt;-
    mydata1 %&gt;%
    filter(prod_code == input$prodcodeInput
    )
  ggplot(filtered, aes(total_sales)) +
    geom_histogram(fill=I("red"),
                   col=I("green") )
})

  output$salesplot &lt;- renderPlot({

    filtered &lt;-
      mydata1 %&gt;%

      filter(salesman == input$salesmanInput
      )
    ggplot(filtered, aes(total_sales)) +
      geom_histogram(fill=I("darkolivegreen1"),
                     col=I("violet") )
  })

    output$salesplot2 &lt;- renderPlot({

      filtered &lt;-
        mydata1 %&gt;%

        filter( 
          state == input$stateInput
        )
      ggplot(filtered, aes(total_sales)) +
        geom_histogram(fill=I("orange"), 
                       col=I("blue") )
    })

}

mainPanel(

}
shinyApp(ui = ui, server = shinyServer)

#3

Your code is very hard to read, and it’s not reproducible, but I’m pretty sure you need rewrite the bit that says output$radioButtonChoiceOutput so the ifs are inside the renderUI. Then it will just step through and go “oh, plottype is s3, so renderUI returns this”. At the moment you really don’t have any reactive process even looking at the if(plottype){} statements.

I’m sorry if that’s not right, it’s very hard to read so that’s the best I can come up with. If you can come up with a better formatted and preferably reproducible example I’m happy to have another go. It shouldn’t be too hard, in principle, to do.


#4
 #install.packages("magrittr")
 library(magrittr)
 library(shiny)
 library(gdata)
 library(plyr)
 library(ggplot2)
 library(shiny)

mydata1 = read.csv("C:\\Users\\BPO18\\Documents\\Book1.csv")

if (interactive()) {

 ui <- fluidPage(

 titlePanel ("Hello"),
   fluidRow(
     
     
     column(4, wellPanel(
       
       radioButtons("rb", "Please select an option",
                 choices = c("Distributor_wise", "Outlet_type_wise"))),
  
  
  wellPanel(
    
    uiOutput("radiobuttonChoiceOutput"),
    plotOutput("distributorOutput"),
    plotOutput("outlettypeOutput")
    ))
  
 ),

column(3,
       # tags$p("Distributor_wise:"),
      verbatimTextOutput("distributorInput"),
      # tags$p("Outlet_type_wise:"),
     verbatimTextOutput("outlettyepInput")

   )
        ) 

}   
      

shinyServer <- function(input, output, session) {

 output$radiobuttonChoiceOutput <- renderUI({
  switch(input$rb,
          Distributor_wise = checkboxGroupInput("distributorInput", 
                                                h3("Distributor-wise"), 
                                                choices = unique(mydata1$distributor_name),
                                                selected = mydata1$distributor_name[1]),
          Outlet_type_wise =  checkboxGroupInput("outlettypeInput", 
                                                 h3("Outlet type wise"), 
                                                 choices = unique(mydata1$outlet_type),
                                                 selected = mydata1$outlet_type[1])
   
 
)})

  output$distributorOutput <-  renderPlot({
   filtered <-
     mydata1 %>%
     dplyr :: filter(distributor_name == input$distributorInput)
    
   ggplot(filtered, aes(total_sales)) +
     geom_histogram(fill=I("red"),
                    col=I("green")) 
 }, height = 400, width = 800)


output$outlettypeOutput <- renderPlot({
  filtered <-
    mydata1 %>%
    dplyr :: filter(outlet_type == input$outlettypeInput
    )
  ggplot(filtered, aes(total_sales)) +
    geom_histogram(fill=I("blue"),
                   col=I("yellow"))
}, height = 400, width = 800)

}   


shinyApp(ui=ui, server= shinyServer)

#5

would this be ok? i have proceeded to a certain level. now, whenever a radio button is clicked, its respective drop down pops up and its graph displays. BUT, graph for the other radio button’s drop down also pops up, which i want to avoid. i only want to show up the drop down and graph of whichever radio button the user clicks on.


#6

In one renderPlot() put this at the top:

if(input$rb == "distributorOutput"){
  return()
}

And in the other

if(input$rb == "outlettypeOutput"){
  return()
}

That should suppress the unwanted output in each case


#7

YEAH. Finally. thank you so much. i was struggling to do that since 10 hours. thank you


#8

actually, sorry. i first got the output, but now, i am not getting it. here is the code,

 #install.packages("magrittr")
library(magrittr)
library(shiny)
library(gdata)
library(plyr)
library(ggplot2)
library(shiny)

mydata1 = read.csv("C:\\Users\\BPO18\\Documents\\Book1.csv")

if (interactive()) {

  ui <- fluidPage(
 
titlePanel ("Hello"),
fluidRow(
  
  
  column(4, wellPanel(
    
    radioButtons("rb", "Please select an option",
                 choices = c("Distributor_wise", "Outlet_type_wise"))),
    
    
    wellPanel(
      
        uiOutput("radiobuttonChoiceOutput")
        # plotOutput("distributorOutput"),
          # plotOutput("outlettypeOutput")
      ))
  
    ),

column(3,
       # tags$p("Distributor_wise:"),
       plotOutput("distributoroutput"),
       # tags$p("Outlet_type_wise:"),
       plotOutput("outlettypeoutput")
       
)
 ) 

}   


 shinyServer <- function(input, output) {

 output$radiobuttonChoiceOutput <- renderUI({
  switch(input$rb,
       Distributor_wise = checkboxGroupInput("distributorInput", 
                                             h3("Distributor-wise"), 
                                             choices = unique(mydata1$distributor_name),
                                             selected = mydata1$distributor_name[1]),
       Outlet_type_wise =  checkboxGroupInput("outlettypeInput", 
                                              h3("Outlet type wise"), 
                                              choices = unique(mydata1$outlet_type),
                                              selected = mydata1$outlet_type[1])
       
       
)})

output$distributorOutput <-  renderPlot({
if(input$rb == "distributorOutput"){
  return()
  filtered <-
  mydata1 %>%
  dplyr :: filter(distributor_name == input$distributorInput)

ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("red"),
                 col=I("green")) }
}, height = 400, width = 800)


output$outlettypeOutput <- renderPlot({
if(input$rb == "outlettypeOutput"){
  return()

filtered <-
  mydata1 %>%
  dplyr :: filter(outlet_type == input$outlettypeInput
  )
ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("blue"),
                 col=I("yellow"))}
}, height = 400, width = 800)

}   


shinyApp(ui=ui, server= shinyServer)

#9

So in the if statements you need the end curly bracket after the return(). Like this:

output$distributorOutput <-  renderPlot({
if(input$rb == "distributorOutput"){
  return()
}
  filtered <-
  mydata1 %>%
  dplyr :: filter(distributor_name == input$distributorInput)

ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("red"),
                 col=I("green")) 
}, height = 400, width = 800)

#10

I tried that before too, it didnt work. i cant see a problem. now none of the graphs are displayed.

#install.packages("magrittr")
library(magrittr)
library(shiny)                                                                                                           
 library(gdata)                                                                                             
 library(plyr)
 library(ggplot2)                                                                                                                                                                                                                  
   library(shiny)                                                                                                    
                                                                                                      
mydata1 = read.csv("C:\\Users\\Smriti\\Desktop\\sample.csv")

if (interactive()) {

 ui <- fluidPage(

titlePanel ("Hello"),
fluidRow(
  
  
  column(4, wellPanel(
    
    radioButtons("rb", "Please select an option",
                 choices = c("Distributor_wise", "Outlet_type_wise"))),
    
    
    wellPanel(
      
      uiOutput("radiobuttonChoiceOutput")
      # plotOutput("distributorOutput"),
      # plotOutput("outlettypeOutput")
    ))
  
),

column(3,
       # tags$p("Distributor_wise:"),
       plotOutput("distributoroutput"),
       # tags$p("Outlet_type_wise:"),
       plotOutput("outlettypeoutput")
       
)
  ) 

}   


 shinyServer <- function(input, output) {

 output$radiobuttonChoiceOutput <- renderUI({
    switch(input$rb,
       Distributor_wise = checkboxGroupInput("distributorInput", 
                                             h3("Distributor-wise"), 
                                             choices = unique(mydata1$distributor_name),
                                             selected = mydata1$distributor_name[1]),
       Outlet_type_wise =  checkboxGroupInput("outlettypeInput", 
                                              h3("Outlet type wise"), 
                                              choices = unique(mydata1$outlet_type),
                                              selected = mydata1$outlet_type[1])
       
       
)})

 output$distributorOutput <-  renderPlot({
if(input$rb == "distributorOutput"){
  return()
}
filtered <-
  mydata1 %>%
  dplyr :: filter(distributor_name == input$distributorInput)

ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("red"),
                 col=I("green")) 
 }, height = 400, width = 800)


 output$outlettypeOutput <- renderPlot({
if(input$rb == "outlettypeOutput"){
  return()
}
  filtered <-
    mydata1 %>%
    dplyr :: filter(outlet_type == input$outlettypeInput
    )
  ggplot(filtered, aes(total_sales)) +
    geom_histogram(fill=I("blue"),
                   col=I("yellow"))
}, height = 400, width = 800)

}   


shinyApp(ui=ui, server= shinyServer)

#11

Make a reproducible example with dput and I’ll have a go to fix


#12

I have tried using the dput. I don’t know if this will help. apart from that, i will just tell what it is. I have a CSV file with 3 columns namely “distributor_name”, “outlet_type”, “total_sales” . “distributor_name” and “outlet_type” are character strings. total_sales is a whole number. i am trying to plot graphs for distributor_name vs total_sales and outlet_type vs total_sales. in the original data, i have too many distributor_name and outlet_type, which are also repeated,which is the reason i have taken unique(distributor_name) and unique(outlet_type).

 #install.packages("magrittr")
 library(magrittr)
 library(shiny)                                                                                                           
 library(gdata)                     
 library(plyr)                                                                                                                                                               
 library(ggplot2)    
 library(shiny)                                                                                                    

mydata1 = read.csv("C:\\Users\\Smriti\\Desktop\\sample.csv")
dput(head(mydata1,4))                                                                                                            
structure(list(distributor_name = data.frame("Chhutu Enterprise" , "Durga Agency", "DeyS 
       Agency House", "S.P Traders"), 
outlet_type= data.frame("Grocer","Grocer", "Puja Outlet", "Self Service"), 
      total_sales = c(200,400, 100, 800, class = "factor")),
 .Names = c("distributor_name", "outlet_type ", "total_sales"), 
 row.names = c(NA, 4L), class = "data.frame")
                                                                                                                                                                                                                                                                  

if (interactive()) {

ui <- fluidPage(

titlePanel ("Hello"),
fluidRow(
  
  
  column(4, wellPanel(
    
    radioButtons("rb", "Please select an option",
                 choices = c("Distributor_wise", "Outlet_type_wise"))),
    
    
    wellPanel(
      
      uiOutput("radiobuttonChoiceOutput")
      # plotOutput("distributorOutput"),
      # plotOutput("outlettypeOutput")
    ))
  
),

column(3,
       # tags$p("Distributor_wise:"),
       plotOutput("distributoroutput"),
       # tags$p("Outlet_type_wise:"),
       plotOutput("outlettypeoutput")
       
)
) 

}   


shinyServer <- function(input, output) {

output$radiobuttonChoiceOutput <- renderUI({
switch(input$rb,
       Distributor_wise = checkboxGroupInput("distributorInput", 
                                             h3("Distributor-wise"), 
                                             choices = unique(mydata1$distributor_name),
                                             selected = mydata1$distributor_name[1]),
       Outlet_type_wise =  checkboxGroupInput("outlettypeInput", 
                                              h3("Outlet type wise"), 
                                              choices = unique(mydata1$outlet_type),
                                              selected = mydata1$outlet_type[1])
       
       
)})

output$distributorOutput <-  renderPlot({
if(input$rb == "distributorOutput"){
  return()
}
filtered <-
  mydata1 %>%
  dplyr :: filter(distributor_name == input$distributorInput)

ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("red"),
                 col=I("green")) 
  }, height = 400, width = 800)


output$outlettypeOutput <- renderPlot({
if(input$rb == "outlettypeOutput"){
  return()
}
filtered <-
  mydata1 %>%
  dplyr :: filter(outlet_type == input$outlettypeInput
  )
ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("blue"),
                 col=I("yellow"))
}, height = 400, width = 800)

}   


shinyApp(ui=ui, server= shinyServer)

#13

Can I point out that the choices in your radioButtons input don’t match the conditions in your if statements for output$radiobuttonChoiceOutput. For example, the choice in the radio buttons is “Distributor_wise”, but your condition is input$rb=="distributorOutput". It seems that this line should read input$rb=="Distributor_wise".


#14

Thank you for your response, but that doesn’t seem to help. none of the graphs display now.

#install.packages("magrittr")   
library(magrittr)
library(shiny)                                                                                                                
library(gdata)                                                                                             
library(plyr)  
 library(ggplot2)                    
 library(shiny)                                                                                                    

 mydata1 = read.csv("C:\\Users\\BPO18\\Documents\\Book1.csv")
# dput(head(mydata1,4))
# structure(list(distributor_name = data.frame("Chhutu Enterprise" , "Durga Agency", "DeyS Agency 
 House", "S.P Traders"), 
 #             outlet_type= data.frame("Grocer","Grocer", "Puja Outlet", "Self Service"), 
 #            total_sales = c(200,400, 100, 800))) 
#                                                                                                                                                                                                                                                                       

ui <- fluidPage(

titlePanel ("Hello"),
fluidRow(
  
  
  column(4, wellPanel(
    
    radioButtons("rb", "Please select an option",
                 choices = c("Distributor_wise", "Outlet_type_wise"))),
    
    
    wellPanel(
      
      uiOutput("radiobuttonChoiceOutput")
      
    )
  )
),

column(3,
       
       plotOutput("distributoroutput"),
       
       plotOutput("outlettypeoutput")
       
)
) 


shinyServer <- function(input, output) {

output$radiobuttonChoiceOutput <- renderUI({
switch(input$rb,
       Distributor_wise = checkboxGroupInput("distributorInput", 
                                             h3("Distributor-wise"), 
                                             choices = unique(mydata1$distributor_name),
                                             selected = mydata1$distributor_name[1]),
       Outlet_type_wise =  checkboxGroupInput("outlettypeInput", 
                                              h3("Outlet type wise"), 
                                              choices = unique(mydata1$outlet_type),
                                              selected = mydata1$outlet_type[1])
       
       
)
})

output$distributorOutput <-  renderPlot({
if(input$rb == "Distributor_wise"){
  return()
}
filtered <-
  mydata1 %>%
  dplyr :: filter(distributor_name == input$distributorInput)

ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("red"),
                 col=I("green")) 
      }, height = 400, width = 800)


output$outlettypeOutput <- renderPlot({
if(input$rb == "Outlet_type_wise"){
  return()
}
filtered <-
  mydata1 %>%
  dplyr :: filter(outlet_type == input$outlettypeInput
  )
ggplot(filtered, aes(total_sales)) +
  geom_histogram(fill=I("blue"),
                 col=I("yellow"))
 }, height = 400, width = 800)
}


shinyApp(ui=ui, server = shinyServer)

#15

The dput is all messed up. I don’t know if the actual data in your application is messed up. Running

str(mydata1)

Yields


> str(mydata1)
'data.frame':	4 obs. of  3 variables:
 $ distributor_name:'data.frame':	1 obs. of  4 variables:
  ..$ X.Chhutu.Enterprise.: Factor w/ 1 level "Chhutu Enterprise": 1
  ..$ X.Durga.Agency.     : Factor w/ 1 level "Durga Agency": 1
  ..$ X.DeyS.Agency.House.: Factor w/ 1 level "DeyS Agency House": 1
  ..$ X.S.P.Traders.      : Factor w/ 1 level "S.P Traders": 1
 $ outlet_type     :'data.frame':	1 obs. of  4 variables:
  ..$ X.Grocer.      : Factor w/ 1 level "Grocer": 1
  ..$ X.Grocer..1    : Factor w/ 1 level "Grocer": 1
  ..$ X.Puja.Outlet. : Factor w/ 1 level "Puja Outlet": 1
  ..$ X.Self.Service.: Factor w/ 1 level "Self Service": 1
 $ total_sales     : Named chr  "200" "400" "100" "800" ...
  ..- attr(*, "names")= chr  "" "" "" "" ...

#16

Hello. sorry for the confusion. i have a csv file that has all the data. i
will attach a sample data that resembles the original data and send. that
seems better.
Regards
Smriti


#17

So, based on the output of radio button, a drop down is displayed. and based on the drop down, graphs need to be plotted. in my code, just one of the graphs is getting plotted. my sample data is a csv file with 3 columns. the name of the csv file is mydata1 and the column names are “distributor_name”, “outlet_type” and “total_sales”. “total_sales” is a numeric values(numbers) and “distributor_name” and “outlet_type” are strings.

distributor_name <-data.frame("a","b","c","d")
outlet_type <- ("q","w","t", "y")
total_sales <- (10, 40, 30, 100)

it would be of great help if some one can help me.

#install.packages("plotly")
library(shiny)
library(gdata)
library(dplyr)
library(ggplot2)
library(plotly)
mydata1 = read.csv("C:\\Users\\BPO18\\Documents\\Book1.csv")

ui <- fluidPage(
 titlePanel("Dynamically generated user interface components"),
 fluidRow(

column(3, wellPanel(
  selectInput("input_type", "Input type",
              c("Distributor_wise", "Outlet_type_wise")
  )
)
),
column(3, wellPanel(
  uiOutput("ui")
)),
column(3,
       plotlyOutput("dynamic_value1"),
       plotlyOutput("dynamic_value2")
)
)
 )

Server <- function(input, output) {
 output$ui <- renderUI({
if (is.null(input$input_type))
  return()

# Depending on input$input_type, we'll generate a different
# UI component and send it to the client.
switch(input$input_type,
       "Distributor_wise" = checkboxGroupInput("dynamic", "Dynamic",
                                               choices = unique(mydata1$distributor_name),
                                               selected = mydata1$distributor_name[1]),
       "Outlet_type_wise" =  checkboxGroupInput("dynamic", "Dynamic",
                                                choices = unique(mydata1$outlet_type),
                                                selected = mydata1$outlet_type[1])
)
})
output$dynamic_value1 <-renderUI({

  if(input$dynamic == mydata1$distributor_name)
{
  output$dynamic_value1 <- renderPlotly({
    
    filtered <-
      mydata1 %>%
      dplyr :: filter(distributor_name == input$dynamic)
    
    ggplot(filtered, aes(total_sales)) +
      geom_histogram(fill=I("red"),
                     col=I("green"))
  })
 }

 if(input$dynamic == mydata1$outlet_type)
{
  output$dynamic_value1 <-  renderPlotly({
    
    filtered <-
      mydata1 %>%
      dplyr :: filter(outlet_type == input$dynamic)
    
    ggplot(filtered, aes(total_sales)) +
      geom_histogram(fill=I("blue"),
                     col=I("yellow"))
  })
}
})

}
shinyApp(ui = ui, server = Server)

#18

actually figured it out