posit-dev / posit-dev/querychat

Querychat drops factor levels? - affects charts

Open
#243 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
212
Forks
29
Avg merge
22h 24m
Merged PRs (30d)
27

Description

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)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided QueryChat$new(gss_cat) example and inspect the data returned by qc_vals$df() before it reaches ggplot. Run the toy Shiny app and compare factor levels and chart order with the direct gss_cat version. Done means querychat preserves the original factor levels used by the chart.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.