LLM graded Weekly Coding Checkup 2

A Semester Project made by Custom Designed Activities

We continue working on the Dr. B & Class project. During the entire semester you will apply what we learned in class in a set of tasks and scenarios custom designed for you. Please remember that it is important that the code that you submit is your own code and not somebody else work. It is fine to make mistakes but only by practicing in R you can get a better grasp of the software. I also want you to try building your document as an official report for a potential company (Dreaming Diamonds LLC) for which you are getting to know and explore the diamonds dataset (e.g., spend time on storytelling, commenting results and providing insights and conclusions when possible).

Summary of this week tasks:

This week we enter the world of data manipulations. In an ideal world, you would receive a dataset ready for analysis. However, this is almost never the case. Cleaning and wrangling data are critical skills of data scientists and should be performed before moving to modeling. So, let’s practice what we covered this week: - Getting to know your data - Manipulating your data using the 5 + 1 dplyr functions

TIP: Use the diamonds dataset to solve the below tasks.

Q1: Load the tidyverse package. (1 point)

Q2: Explore the diamonds dataframe using the “Get to Know your Data” functions covered in class. (1 point)

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 600
library(shiny)
library(bslib)
library(httr2)
library(jsonlite)

ui <- fluidPage(
  titlePanel("Code Explainer"),
  sidebarLayout(
    sidebarPanel(
      textInput("user_code", "Paste your R code:", value = ""),
      actionButton("explain_btn", "Explain Code")
    ),
    mainPanel(
      verbatimTextOutput("api_feedback")
    )
  )
)

server <- function(input, output, session) {
  feedback <- reactiveVal("")

  observeEvent(input$explain_btn, {
    req(input$user_code) # Ensure code is not empty

    # API call
    api_key <- Sys.getenv("OPENAI_API_KEY")
    api_url <- "https://api.openai.com/v1/chat/completions"

    request_body <- list(
      model = "gpt-4",
      messages = list(
        list(
          role = "user",
          content = paste(
            "Explain the following R code:\n", input$user_code,
            "\nProvide detailed feedback for educational purposes."
          )
        )
      ),
      temperature = 1,
      max_tokens = 512,
      top_p = 1,
      frequency_penalty = 0,
      presence_penalty = 0
    )

    response <- tryCatch({
      req <- request(api_url) %>%
        req_headers(
          "Authorization" = paste("Bearer", api_key),
          "Content-Type" = "application/json"
        ) %>%
        req_body_json(request_body) %>%
        req_perform()

      resp_body_json(req)$choices[[1]]$message$content
    }, error = function(e) {
      paste("Error:", e$message)
    })

    feedback(response)
  })

  output$api_feedback <- renderText({
    if (is.null(feedback())) {
      "Press 'Explain Code' to get feedback."
    } else {
      feedback()
    }
  })
}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

Feedback: Run your code to receive feedback!

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Error: No code submitted.

Please run your code to receive feedback.

🛑 Don’t Click Submit Just Yet 🚧

Please read carefully the below information:

  • Once you have completed all the coding questions, and your confident in your work, copy and paste your responses from the chunk into the form fields below each question.

  • You are responsible for correctly coping and pasting only the required code to solve each question. We will grade only what you have submitted!

  • We will only grade 1 submission per student so do not click Submit until you are confident in your responses.

  • By submitting this form you are certifying that you have followed the academic integrity guidelines available in the syllabus. The code and answers submitted are the results of your work and your work only!

  • Make sure you have completed all the questions and included all the required personal information (e.g., full name, email, zid) in the respective form’s fields.

  • Now you are ready to click the above “Submit” button. Congrats you have completed your weekly coding check up!!!