Chat with LLMs on your R environment

Chat with LLM’s on R

LLM provides many advantages to the users, especially for coding. Once user had to switch the windows from the coding environment to the browser to search for the solution. But now, thanks to the newly advancements, users can chat with the LLM and get the solution for their queries on the same coding environment in R.

There are several packages publicly available in that manner on CRAN and GitHub such as chattr, tidyllm etc. Among these packages, ellmer is the one of most comprehensive one such that you can chat with almost all languages available.

ellmer is a package that creates an interface to chat with different language models. The full list alongside the key function is given below.

  • Anthropic’s Claude: chat_claude().
  • AWS Bedrock: chat_bedrock().
  • Azure OpenAI: chat_azure().
  • Databricks: chat_databricks().
  • DeepSeek: chat_deepseek().
  • GitHub model marketplace: chat_github().
  • Google Gemini: chat_gemini().
  • Groq: chat_groq().
  • Ollama: chat_ollama().
  • OpenAI: chat_openai().
  • OpenRouter: chat_openrouter().
  • perplexity.ai: chat_perplexity().
  • Snowflake Cortex: chat_snowflake() and chat_cortex_analyst().
  • VLLM: chat_vllm().

In this tutorial, I’ll summarize the steps of using Gemini in R using ellmer package since Google allows to use an api key for free.

There is no free account for API keys of OpenAI, the service costs by amount of data that you use. Thus, I am proceeding with Google Gemini in this tutorial, however I also provide the funcitons and instructions that you can also use other language models.

Step 1: Install the package from CRAN

install.packages("ellmer")
library(ellmer)

Step 2: Set Up your Gemini Api Key

In order to get an api key from Google Gemini you need to create an account on Gemini first, which is most probably something you have. Then, you can visit Google AI Studio and create your api key for free of charge.

After your api key, you employ Sys.setenv() function to set your api key as follows.

google_api_key = "your_api_key"
Sys.setenv(GOOGLE_API_KEY = google_api_key)

If you would like to use other language models, the syntax that you need to use is given below.

  • For Claude models
Sys.setenv(ANTHROPIC_API_KEY = "YOUR-ANTHROPIC-API-KEY")
  • For OpenAI
Sys.setenv(OPENAI_API_KEY = "YOUR-OPENAI-API-KEY")
  • For Google Gemini
Sys.setenv(GOOGLE_API_KEY = 'YOUR-GOOGLE-API-KEY')
  • For Mistral
Sys.setenv(MISTRAL_API_KEY = "YOUR-MISTRAL-API-KEY")
  • For groq
Sys.setenv(GROQ_API_KEY = "YOUR-GROQ-API-KEY")
  • For Perplexity
Sys.setenv(PERPLEXITY_API_KEY = "YOUR-PERPLEXITY-API-KEY")

Step 3: Chat with your model.

After setting up your enviroment with your api key, you should create a chat object by using a function that is related to your model. In this tutorial, I’ll use Google Gemini, thus I’ll use chat_gemini() function.

If I used OpenAI, I would use chat_openai() function.

There are three ways of getting a response from the model for your query, but these three ways have one common point; you should create a chat object first.

chat <- chat_gemini()
## Using model = "gemini-1.5-flash".
chat
## <Chat turns=0 tokens=0/0>

With this function, you activate your language model to chat. If you would like to start to get response for your query, you should call the activated model by $ function.

chat$chat("how to create a heatmap in ggplot2", echo = FALSE)
## [1] "Creating a heatmap in `ggplot2` involves using the `geom_tile()` function.  The key is to have your data in a \"long\" format, where you have separate columns for the x-axis variable, the y-axis variable, and the value that determines the color of the tile.\n\nHere's a breakdown of how to create a heatmap in `ggplot2`, along with explanations and examples:\n\n**1. Data Preparation:**\n\nYour data needs to be in a long format. Let's assume you have a matrix or data frame where rows represent one variable and columns represent another, and the cell values represent the heatmap intensity.  We'll need to convert this to a long format using `tidyr::pivot_longer()`.\n\n```R\n# Example data (replace with your own)\ndata <- matrix(rnorm(100), nrow = 10, ncol = 10)\ncolnames(data) <- paste0(\"col\", 1:10)\nrownames(data) <- paste0(\"row\", 1:10)\n\nlibrary(tidyr)\nlibrary(ggplot2)\n\n# Convert to long format\ndata_long <- as.data.frame(data) %>% \n  rownames_to_column(\"row\") %>%\n  pivot_longer(cols = -row, names_to = \"col\", values_to = \"value\")\n\n#Alternatively, if your data is already in long format:\n#data_long <- data.frame(\n#  x = rep(letters[1:5], each = 5),\n#  y = rep(letters[1:5], 5),\n#  value = rnorm(25)\n#)\n\nhead(data_long)\n```\n\n**2. Creating the Heatmap:**\n\nNow, use `ggplot2` to create the heatmap:\n\n```R\nggplot(data_long, aes(x = col, y = row, fill = value)) +\n  geom_tile() +\n  scale_fill_gradient2(low = \"blue\", mid = \"white\", high = \"red\", midpoint = 0) + # Adjust colors as needed\n  labs(title = \"Heatmap Title\", x = \"Column Variable\", y = \"Row Variable\", fill = \"Value\") +\n  theme_minimal() # Optional: Use a minimal theme for cleaner look.  Other themes are available.\n```\n\nThis code does the following:\n\n* `ggplot(data_long, aes(x = col, y = row, fill = value))`:  Sets up the plot with your data and maps the x and y variables to the tile positions, and the `value` variable to the fill color.\n* `geom_tile()`: Creates the tiles.\n* `scale_fill_gradient2()`:  Specifies the color gradient.  You can customize this significantly:\n    * `low`: Color for low values.\n    * `mid`: Color for the midpoint.\n    * `high`: Color for high values.\n    * `midpoint`: Value that corresponds to the `mid` color.  Useful for centering the color scale around zero.  Omit for a non-centered scale.  Other options like `limits` can further customize the range.\n* `labs()`: Adds labels to the title, axes, and legend.\n* `theme_minimal()`:  Applies a clean theme.  Consider other themes like `theme_bw()` or `theme_classic()` for different aesthetics.\n\n\n**3.  Advanced Customization:**\n\n* **Discrete Variables:** If your x or y variables are categorical (factors),  `ggplot2` will handle them automatically.\n* **Color Palette:** Explore different color palettes from packages like `RColorBrewer`, `viridis`, or `scico` for better visual representation.  For example:\n\n```R\nlibrary(viridis)\nggplot(data_long, aes(x = col, y = row, fill = value)) +\n  geom_tile() +\n  scale_fill_viridis(option = \"D\") + # Use viridis palette\n  labs(title = \"Heatmap Title\", x = \"Column Variable\", y = \"Row Variable\", fill = \"Value\") +\n  theme_minimal()\n```\n\n* **Annotations:** Add text or other annotations to the tiles using `geom_text()`.\n\n* **Clustering:** If you want to cluster rows or columns based on their similarity, you'll need to perform clustering beforehand (using functions like `hclust()` or packages like `factoextra`) and then incorporate the cluster assignments into your plot using `aes()` and possibly `facet_wrap()`.\n\n* **Missing Values:** Handle `NA` values appropriately.  `scale_fill_gradientn()` allows you to specify a color for `NA`.  Or, you could impute missing values before creating the heatmap.\n\n\nRemember to replace the example data with your own and adjust the labels and color scales to suit your specific data and requirements.  Experiment with different themes and color palettes to find the most effective visual representation.\n"

Step 4: Get your answers from an interface.

As you may see above, chat object provides an answer on your R console, which is not in tidy format so that difficult to follow. If you would like to get your answer in a tidy format, you can use live_browser function.

Also see live_console()

live_browser() function opens an interactive dashboard on your viewer screen and enables you to chat with your language as on your browser. To be able to use this function, it is enough to insert your chat object as input to the function as given below.

`live_browser(chat)

Then, write your query and get your answer.

Step 5: Close your chat object

To close your browser, you may either press Esc or click on the red stop button on your viewer screen.