Histogram in RShiny

Hi there,

I have been trying to create a website using shiny and when trying to create a histogram I have encountered some problems.

So what I'm trying to do is ...

  1. To create a histogram that shows the relationship between abortion (x-axis) and parity (y-axis), and having abortion types being reactive to the user's selections

However, based on the graph that I have generated, it indicates parity and the frequency of each based on the infert dataset.

So, I was hoping that someone could please help me fix that

  1. To create a graph (prehaps a stacked barplot) that shows the relationship of levels of education (on the x-axis) and numbers of abortion (y-axis) and having that graph being reactive to the radioButton of selections on education levels

  2. To create a graph that indicates the relationship between parity (y-axis) and age (x-axis) and having age being reactive to the sliderInput

I guess what I'm struggling at the most is to have my graphs to depend on the inputs so if anyone can explain or show me how that is done, it would be much, much appreciated :slight_smile:

p.s not all 3 things need to be explained at once, individual answers for singular issues are helpful

here is my code and what I have done so far


### Define UI for application that draws a histogram
ui <- fluidPage(
  # Application title
  titlePanel("Infertility and Abortions:"),
   p("How does induced and spontaneous abortions affect women's infertility?"),
  # Sidebar
    # Sidebar panel for inputs ----
      # Select Input for Abortions ----
      selectInput("display_var", label = h4(strong("Abortion Type (valid for Histogram only):")), 
                  choices = list("Induced Abortion" = "induced", 
                                 "Spontaneous Abortion" = "spontaneous", 
                                 "Induced and Spontaneous Abortion" = c("induced, spontaneous")), 
                  selected = "induced"
     # Radio Buttons for Education Levels ----
      radioButtons("buttons", label = h4(strong("Education Level:")), 
                         choices = list("Low Levels of Education" = "0-5yrs", 
                                        "High Levels of Education" = "6-11yrs", 
                                        "Tertiary Levels of Education and Above" = "12+ yrs"),
                         selected = "6-11yrs"
      # Slider Input for ages in numbers ----
                  label = h4(strong("Age:")),
                  min = 20,
                  max = 50,
                  value = c(30, 40)

    # Main panel for displaying outputs ----
      # Output: Tab-set w/plots, summary
        tabsetPanel(type = "tabs",
                    tabPanel("Histogram", plotOutput("distPlot"), br(), 
                             radioButtons("filetype", "Select File Type To Download", 
                                          c("PDF", "JPEG")),
                             downloadButton("dwd", "Download Graph")),
                    tabPanel("Education vs. Abortion", plotOutput("barplot"),br()),
                    tabPanel("Age vs. Parity", plotOutput("barplot2")),
                    tabPanel("Summary", verbatimTextOutput("summary"))

### Define server logic to draw the above graphs

# First Install GBM Package

# Define server logic required to draw a histogram
server <- function(input, output) {
  # Histogram
  output$distPlot <- renderPlot({
    ## Set x-axis label depending on the value of display_var
    if (input$display_var == "induced") {
      xlabel <- "Number of Induced Abortions"
    } else if (input$display_var == "spontaneous") {
      xlabel <- "Number of Spontaneous Abortions"
    } else if (input$display_var == "induced, spontaneous") {
      xlabel <- "Number of Induced and Spontaneous Abortions"
    ## Create new data sets
    parity <- infert$parity
    abortion <- infert$induced + infert$spontaneous
    hist(parity, breaks = 10,
         col = "grey",
         xlab = xlabel,
         ylab = "frequency",
         main = "Histogram of Parity vs Abortion")
    ## Download file
    output$dwd <- downloadHandler(filename = function(){
     paste((str_c(input$display_var, collapse = " & ")),input$filetype, sep = ".")
    content = function(file){
      if (input$filetype == "pdf") {pdf(file)} 
      if (input$filetype == "jpeg") {jpeg(file)}
  # Education vs. Abortion
   output$barplot <- renderPlot({
     if (input$buttons == "0-5yrs") {
       xlab <- "Low Levels of Education"
     } else if (input$buttons == "6-11yrs") {
       xlab <- "High Levels of Education"
     } else if (input$buttons == "12+ yrs") {
       xlab <- "Tertiary Level of Education and Above"
     plot(infert$education, infert$induced, main="education vs. abortion", 
             xlab = xlab)

  # Age vs. Parity
   output$barplot2 <- renderPlot({
  # Summary
   output$summary <- renderText({"Summary:
     increased numbers of induced and spontaneous abortion will cause 
     decreased levels of fertility"})

# Run the application 
shinyApp(ui = ui, server = server)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.