
Generate a thematic code list using the OpenAI API
Source:R/07_03_open_ended_coding_theme_gpt.R
theme_gpt.RdThis function analyzes open-ended survey responses and automatically generates a set of thematic codes with descriptions.
Additional custom instructions can be supplied through the instructions argument.
Arguments
- data
A data frame containing the survey data.
- x
The open-ended variable to analyze.
- n
Integer; number of themes to return. If
NULL(default), the model determines an appropriate number of themes based on the responses.- sample
Optional integer specifying the number of responses to sample for analysis. If
NULL, all valid responses are used.- model
Character string; the OpenAI model to use. Defaults to
gpt-4o.- instructions
Optional string; additional instructions for coding.
Value
A table containing the generated thematic code list and their description. Standard codes (Other, None, Don't know) are included automatically.
Details
Requires an OpenAI API key, which can be generated at https://platform.openai.com/, to be set in your R session using Sys.setenv(OPENAI_API_KEY="...").
The output is a tibble with three columns:
Code: A unique numeric code for each theme (standard codes 97–99 are added automatically).Bin: Short label for the theme, written in sentence case.Description: A one-sentence summary describing the theme's content.
Standard codes are included automatically:
97=Other98=None99=Don't know
Use this function to create a theme_list for input into code_gpt(), or copy and paste it into an Excel coding workbook.
Note: It’s best to review and refine the generated codes before using them in code_gpt().