Skip to content

Querychat drops factor levels? - affects charts #243

Description

@garrettgman

When I use querychat, charts that display factors begin to order levels alphabetically. This makes me think that querychat drops factor levels when processing the data. Here is a toy example.

Image

A shiny app

library(shiny)
library(bslib)
library(forcats)
library(ggplot2)
library(dplyr)

relig_choices <- c("All", levels(gss_cat$relig))

ui <- page_sidebar(
  title = "GSS Marital Status",
  sidebar = sidebar(
    selectInput("relig", "Religion", choices = relig_choices, selected = "All")
  ),
  plotOutput("marital_plot")
)

server <- function(input, output, session) {
  filtered_data <- reactive({
    if (input$relig == "All") {
      gss_cat
    } else {
      filter(gss_cat, relig == input$relig)
    }
  })

  output$marital_plot <- renderPlot({
    ggplot(filtered_data(), aes(x = marital, fill = marital)) +
      geom_bar() +
      theme(
        axis.text  = element_text(size = 16),
        axis.title = element_text(size = 18)
      )
  })
}

shinyApp(ui, server)

The querychat version

library(shiny)
library(bslib)
library(forcats)
library(ggplot2)
library(dplyr)
library(querychat)

qc <- QueryChat$new(gss_cat, greeting = "Hello")


ui <- page_sidebar(
  title = "GSS Marital Status",
  sidebar = qc$sidebar(),
  plotOutput("marital_plot")
)

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

  qc_vals <- qc$server()

  filtered_data <- reactive({
    qc_vals$df()
  })

  output$marital_plot <- renderPlot({
    ggplot(filtered_data(), aes(x = marital)) +
      geom_bar()
  })
}

shinyApp(ui, server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions