knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE )
The searcher
package includes a powerful prompt management system for working
with AI assistants. This vignette explains how to use this system to create,
manage, and apply effective prompts when using AI search functions. It
complements the main vignette("using-ai-assistants-with-searcher", package = "searcher")
,
which provides a broader overview of using AI services with R.
Prompts are instructions that guide how an AI assistant should respond to your queries. A well-crafted prompt can dramatically improve the quality and relevance of AI responses for R programming tasks.
The searcher
package provides a multi-level prompt system:
ai_prompt()
, these apply across all AI servicesoptions()
or within function callsWhen you search with an AI assistant, we combine all relevant prompts in the following order:
For clarity, the prompt hierarchy is illustrated below:
The package comes with several pre-defined prompts optimized for different R programming scenarios:
# List available prompts ai_prompt_list()
This will show prompts like:
general
: Balanced for general R questions and errorsdebugging
: For identifying and fixing bugs in R codelearning
: For learning R concepts with progressive complexitypackage_selection
: For comparing R packages with balanced analysiscode_review
: For evaluating and improving R codestats_analysis
: For statistical analysis in Rvisualization
: For creating effective data visualizationsTo set a system-level prompt that will be used with all AI search functions:
# Use a built-in prompt ai_prompt("debugging") # Then search with any AI service ask_chatgpt("Error: object 'mtcrs' not found") # Note the typo
You'll see a message indicating which prompt is being used:
Using prompts: system (debugging) Searching query in a web browser...
The AI will receive both your prompt instruction and the query, helping it respond more effectively.
You can also use a custom prompt text directly:
# Set a custom prompt ai_prompt("As an R package developer, explain this error in terms of how R handles namespaces:") # Check the active prompt ai_prompt_active()
You can check the currently active prompt or clear it:
# Check currently active prompt ai_prompt() # or ai_prompt_active() # Clear the active prompt ai_prompt(NA) # or ai_prompt_clear()
You can add your own prompts to the library:
# Register a new prompt ai_prompt_register( "shiny_expert", "As a Shiny app developer, explain how to implement this UI feature or fix this reactive issue:" ) # Use your new prompt ai_prompt("shiny_expert")
To remove a prompt from the library:
ai_prompt_remove("shiny_expert")
In this section, we will explore more advanced usage of the prompt system.
This includes layering multiple prompts, creating a session prompt library,
and managing prompts in your .Rprofile
for persistent settings.
The prompt system supports layering multiple prompts:
# Set a system-level prompt ai_prompt("debugging") # Use with service-specific default prompt options(searcher.claude_prompt = "Focus on tidyverse solutions:") # Then use with a function-call prompt ask_claude("Error in filter(data, x > 0): object 'x' not found", prompt = "Explain in simple terms:")
This will use all three prompts in order (debugging, "Focus on tidyverse solutions", and "Explain in simple terms") before the query.
For more advanced usage, you can create a custom prompt library in your R session:
# Create custom prompts for different projects ai_prompt_register("my_package", "As an R package developer reviewing the 'mypackage' codebase.") ai_prompt_register("data_cleaning", "Analyzing the customer_data.csv dataset with missing values.") ai_prompt_register("reporting", "Create a Quarto document report for business stakeholders.") # Switch between contexts as you work ai_prompt("my_package") # ... work on package development ... ai_prompt("data_cleaning") # ... work on data cleaning ...
For persistent prompt management, you can add code to your .Rprofile
This file is executed every time you start R, allowing you to set up your environment.
Most users will have a .Rprofile
file in their home directory. You can create
or edit this file to include your prompt management code.
To create or edit the .Rprofile
file, you can use the following command in R:
# Creating or editing the file: file.edit("~/.Rprofile")
Then, you can add the following code to set up your prompt management system:
# In .Rprofile .First <- function() { if (requireNamespace("searcher", quietly = TRUE)) { # Register custom prompts searcher::ai_prompt_register( "work", "As an R analyst at XYZ company working with our sales database:", overwrite = TRUE ) # Set default prompts for different AI services options( searcher.chatgpt_prompt = "Provide R code with tidyverse packages:", searcher.claude_prompt = "Give me both base R and tidyverse solutions:" ) # Set a default system-level prompt if desired # searcher::ai_prompt("debugging") } }
The prompt management system in searcher
provides a flexible and powerful way to
create, manage, and apply prompts for AI search functions. Through using
system-level prompts, service-specific prompts, and custom prompts, you can
tailor the AI's responses to your specific needs and context. This
approach enhances the quality of AI-generated responses and, subsequently,
helps you save time and improve the efficiency of your R workflow.
The system presented in this vignette transforms the custom prompts described
in vignette("using-ai-assistants-with-searcher", package = "searcher")
from one-off tools
into a systematic library that can be maintained, shared, and reused. This
represents a shift from ad-hoc prompting to a more deliberate approach that
treats prompts as valuable assets in your R programming toolkit.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.