FrankRoss
FrankRoss

Reputation: 47

How to include reactive output text Shiny

I am using a multi-file setup so I have both ui.r and server.r files. I am using the dataset below (small sample) which looks at UFO sightings in different states/provinces in U.S./Canada in 2016.

        Date        Time           AM.PM   Country       City   State   Shape
1   12/21/2016     7:15:00          PM     USA      Waynesboro    VA    Sphere
2   12/21/2016     12:00:00         AM     USA      Louisville    KY    Unknown
3   12/20/2016     10:30:00         PM     USA      Santa Rosa    CA    Sphere
4   12/20/2016     7:00:00          PM     USA      Fresno        CA    Circle
5   12/19/2016     9:53:00          PM     USA      Reymert       AZ    Circle
6   1/11/2016      8:15:00          PM     CANADA   Mississauga   ON    Circle

In my app so far, I have created a main panel which displays a bar graph that has all of the different Shapes observed in the "Shape" column on the x-axis and the number of observations on the y-axis. I have a widget on the side which allows the user to select if they want to look at U.S. or Canada data and the plot will change accordingly. What I want to do is have text outputted in the main panel under the plot that will say "Shape with the most observations:" and then print the name of the shape that has the highest y-value. The hard part for me is that the name of the shape that is printed needs to change depending on what data is currently being displayed on the graph. Any help is appreciated!

UI File:

library(shiny)
library(ggplot2)
library(dplyr)

shinyUI(fluidPage(

    # Application title
    titlePanel("Exploring UFO Sightings"),

    sidebarLayout(
        sidebarPanel(
            selectInput("Country",
                        "Country to Display:",
                        choices = list("USA" = 'USA',
                                       "Canada" = 'CANADA'),
                        ),
            checkboxGroupInput("type",
                               "Select Desired Shapes Observed:",
                               choices = unique(ufo_data$Shape),
                               selected = unique(ufo_data$Shape)),
        ),

        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("ufoPlot"),
           # textOutput("Shape with the most observations:")
        )
    )
))

Server File:

library(shiny)
library(ggplot2)
library(dplyr)


shinyServer(function(input, output) {

    output$ufoPlot <- renderPlot({
        
        ufo_data_filter <- filter(ufo_data, Country == input$Country & Shape %in% input$type)
        ggplot(data = ufo_data_filter) +
            geom_bar(mapping = aes(x = Shape, fill = Shape))+
            labs(
                title = "Number of Different Shaped UFO Observations in Selected Country",
                x = "Shape of UFO Sighted",
                y = "Number of Observations in 2016"
            )
    })

})

Upvotes: 1

Views: 477

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388797

For the select subset of data, you can use count to find the count of each Shape and select the highest occurring one.

library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  # Application title
  titlePanel("Exploring UFO Sightings"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Country",
                  "Country to Display:",
                  choices = list("USA" = 'USA',
                                 "Canada" = 'CANADA'),
      ),
      checkboxGroupInput("type",
                         "Select Desired Shapes Observed:",
                         choices = unique(ufo_data$Shape),
                         selected = unique(ufo_data$Shape)),
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("ufoPlot"),
      textOutput("text")
    )
  )
)

server <- function(input, output) {
  
  output$ufoPlot <- renderPlot({
    
    ufo_data_filter <- filter(ufo_data, Country == input$Country & Shape %in% input$type)
    ggplot(data = ufo_data_filter) +
      geom_bar(mapping = aes(x = Shape, fill = Shape))+
      labs(
        title = "Number of Different Shaped UFO Observations in Selected Country",
        x = "Shape of UFO Sighted",
        y = "Number of Observations in 2016"
      ) 
  })
  
  output$text <- renderText({
    text_data <- ufo_data %>%
      filter(Country == input$Country & Shape %in% input$type) %>%
      count(Shape, sort = TRUE)
    paste0('Shape with the most observations: ', text_data$Shape[1])
  })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions