Skip to content

Return code generated by the assistant to evaluate it in a shiny app? #15

@useReconomist

Description

@useReconomist

How could I return the code generated by the assistant and evaluate it in a shiny application for a plotoutput and textoutput?

I'm trying to return the code generated by assistant inside shinyapp so that I can view the plot and code but I have not been successful, I would appreciate any observations.

library(shiny)
library(shinychat)
library(tidyverse)
library(elmer)

prompt_chat = "You are an expert in data manipulation and visualization in the R programming language and the use of tidyverse libraries and with a lot of experience in ggplot. 
Return only and exclusively the R code that answers the question you are asked without giving any other explanation, 
the database you have at your disposal is called df_base, the ggplot library is already in the environment so it is not necessary to call it."

df_base = as_tibble(iris)

ui <- fluidPage(
  theme = bs_theme(version = 5),
  chat_ui("chat"),
  textOutput("code_ia"),
  plotOutput("plot_ia")
)

server <- function(input, output, session) {
  chat <- chat_github( model = "gpt-4o-mini",
                       system_prompt = prompt_chat,
                       api_key = token_github,
                       echo = TRUE
  )
  
  observeEvent(input$chat_user_input, {
    # Add user message to the chat history
    chat_append("chat", chat$stream_async(input$chat_user_input))
  })
  
  output$code_ia = renderText({
  })
  
  output$plot_ia = renderPlot({
    
  })
  
}

shinyApp(ui, server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions