Table of Contents

  1. Shiny Dashboard Development using LLMs (Shiny Assistant)

    How we can use LLM to build Shiny Dashboard?

  2. State-of-the-art Generative AI powered Shiny Dashboard (Sidebot)

    What can you do in R using LLM models?

  3. Hand-on session to run a chatbot in Rstudio (elmer package)

    1. Getting an API key,
    2. Install a package,
    3. Setup the API to work with the package
    4. Run a chatbot

Shiny Assistant

You Can Now Build Shiny Applications with GPT and GenerativeAI12

It is built on Claude 3.5 Sonnet model from Anthropic, currently the most popular (not qualified) coding model in the market.

Shiny Assistant is currently (previously) in an invite-only closed beta stage:

Image 1 - Link: https://gallery.shinyapps.io/assistant/ (please use Chrome or Edge for file interaction)

Start page - Shiny Assistance

Image 2 - Shiny Assistant start page

Response type = “Code Only”, “Concise”, “Verbose”

R / Python code: The Shiny assistant is better at R Shiny coding tasks. Most of the Python Shiny coding tasks (based on my own testing) fail, possibly because there are fewer examples to learn from.

[Cog wheel] - Insert your own Anthropic key

Chatbot Layout - Shiny Assistant

Image 3 - Shiny Assistant chatbot layout

[ <-Sidebot->] [<-Coding panel->][<-Live app->]

Top right options:

-Load project from a directory on disk (not working)

-Save all project files (not working)

-Download project files (download file to local computer)

-Open files in new editor window (open another window for coding)

-Create Share Link ( Create code link and application link)

Shiny Assistant Pros

  • It’s free - Yup, it knows as much about Shiny as the developers behind it, but won’t cost you a dime. You can add your Anthropic API key which will cost you on a per-request basis, but the tool itself is free of charge (at least as of September 2024).

  • It supports both R and Python - Shiny Assistant can build Shiny apps in R and Python. We haven’t covered the latter in today’s article, but plan to do so in the future. It will be interesting to compare the two since R’s version of Shiny has been around the block for approximately 10 years longer.

  • It supports a wider R Shiny ecosystem - This pro is possibly limited to R Shiny, but nevertheless, the Assistant knows how to work with packages frequently used with Shiny, such as Shiny Modules, Rhino, R6, and so on.

  • It reduces friction in building Shiny apps - The Assistant can’t do everything for you, but if you have some fundamental knowledge it can do most of the heavy lifting. In our example, it’s clear that we’re holding the Assistant by hand most of the time as we know exactly what we want. When our prompts were more open to interpretation, so were the results. Overall, if you have a general idea of what you want to accomplish, Shiny Assistant can create it, fast.

  • It increases your learning speed - You can leverage Shiny Assistant to learn new concepts and packages in a non-conventional way. Instead of going through package documentation, simply ask the Assistant to show you how to incorporate the needed functionality. You learn faster and develop faster - win-win!

  • Great for creating starter code - We found Shiny Assistant to be great for getting you 70% to the goal. The remaining 30% boils down to fixing bugs the Assistant can’t solve and fine-tuning and polishing application aspects that are faster to implement by hand.

  • Less Googling - R is a widely used language, but it still has a smaller community than Python or JavaScript. Googling R or R Shiny errors might not always result in a solution you can use out of the box. Shiny Assistant can fix most of the bugs, saving you time manually searching and custom tailoring a solution.

Generative AI Cons

  • It shortens your attention span - If AI can write Shiny applications from scratch, there’s no need for you to invest too much concentration into the project. Writing text prompts in plain English is less involved than writing code. Everything is fine until AI can’t solve the bugs you’re running into, and you’re left examining hundreds of lines of code you haven’t written in the first place, not knowing where to start.

  • It’s just a matter of time before you hit a wall - When working with LLMs, the focus shifts from building to delegating and correcting mistakes. Delegating is still hard, but it’s the easier of the two. Fixing bugs can be frustrating and time-consuming, and it often requires a deeper understanding of Shiny.

  • It doesn’t save you that much time - This argument is twofold. First, you need to write and refine the prompts before submitting them to an AI model. It took us close to two hours to write the prompts for this app! And second, if you reach a point where AI can’t solve bug(s), you don’t only have to solve them yourself, but you also need to understand potentially messy AI-generated code.

  • Sensitive data - You should never share sensitive information about your company and security credentials with an AI tool. Shiny Assistant is no exception. Masking sensitive parts of your code takes time, both going in and out.

Shiny Assistant Cons

  • Assistant modes - Right now, you can switch between “Code only”, “Concise”, and “Verbose”, with “Concise” being the default. We’ve opted for “Code only” for this article, but Shiny Assistant still generated a lot of explainer text. It’s not a biggie, but it’s a bug we have to mention. It looks like a basic prompting error, so it shouldn’t take the Posit’s team too much time to fix it.

  • Live preview sometimes breaks - You’ve seen it in the screenshots of the previous section. Again, it’s not a deal-breaker, but copying or downloading the code to a local machine takes time and breaks the seamlessness of the Assistant.

  • Code generation - Shiny Assistant always writes the entire application code from scratch. Since the model running in the background generates it token by token, it takes time, even for the simplest of prompts. You might consider this a real issue for more complex apps, but in our case, it was just a minor inconvenience.

  • It’s currently just a web app - We’d love to see Shiny Assistant as a plugin or an integral part of RStudio and/or Positron.

Shiny Assistant Discussion

Sidebot Demo

Sidebot Discussion

Elmer - A wrapper over common llm providers

Installation

install.packages("pak")

library(pak)

pak::pak("tidyverse/elmer")

library(elmer)

Get a token from Github

Head over to: https://github.com/marketplace

Image 5. Github Market place

Image 6. Get a key for gpt-4o

Image 7. Get a FREE developer key

Github token as an LLM API key

Image 8. Generate a github token

Generate new token (it doesn’t matter whether it is beta or classic)

You’ll need to set the appropriate environment variable in your ~/.Renviron (an easy way to open that file is to call usethis::edit_r_environ()

Insert the following line in your .Renviron file

GITHUB_API_KEY={new github token}

Restart R session to take effect …

Interactive chat console

library(elmer)
chat <- chat_github( model = "gpt-4o", api_key = Sys.getenv("GITHUB_API_KEY"), system_prompt = "You are a friendly but terse assistant.", echo = TRUE )
live_console(chat)

Image 10. Console output

Browser console

library(elmer)
chat <- chat_github( model = "gpt-4o", api_key = Sys.getenv("GITHUB_API_KEY"), system_prompt = "You are a friendly but terse assistant.", echo = TRUE )
live_browser(chat)

Image 11. Live Browser Console

Other founctions

Interactive method call

chat$chat("What preceding languages most influenced R?") 
#> R was primarily influenced by the S programming language, particularly S-PLUS. 
#> Other languages that had an impact include Scheme and various data analysis 
#> languages.

Vision (image input)

If you want to ask a question about an image, you can pass one or more additional input arguments using content_image_file() and/or content_image_url().

chat$chat(
  content_image_url("https://www.r-project.org/Rlogo.png"),
  "Can you explain this logo?"
)
#> The logo of R features a stylized letter "R" in blue, enclosed in an oval shape that resembles the letter "O,"
#> signifying the programming language's name. The design conveys a modern and professional look, reflecting its use
#> in statistical computing and data analysis. The blue color often represents trust and reliability, which aligns
#> with R's role in data science.

Discussion & Questions

Connect with me on Linkedin: