Skip to content

Instantly share code, notes, and snippets.

@tbbooher
Created April 27, 2025 02:11
Show Gist options
  • Save tbbooher/3dabd696a4b35bb24c926cbb3c61b1b4 to your computer and use it in GitHub Desktop.
Save tbbooher/3dabd696a4b35bb24c926cbb3c61b1b4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env Rscript
# Set working directory
setwd('/Users/tim/code/tesla')
# 1) Load libraries (assume already installed)
library(googlesheets4)
library(dplyr)
library(magrittr)
library(ggplot2)
# 2) Authenticate with service account
sa_file <- "google_creds.json"
if (!file.exists(sa_file)) {
stop("Could not find ", sa_file, " in working directory.")
}
gs4_auth(
path = sa_file,
scopes = "https://www.googleapis.com/auth/spreadsheets.readonly"
)
# 3) Read the sheet into a dataframe
sheet_url <- "https://docs.google.com/spreadsheets/d/1HW2JvwuwVBMcM8cnOm5UeOcfZIypP7WTeMUhCgLPTCs"
df <- read_sheet(sheet_url, sheet = "data")
# 4) View first few rows (optional debug)
print(head(df))
# 5) Write out a TSV
write.table(
df,
file = "tesla_data.tsv",
sep = "\t",
row.names = FALSE,
quote = FALSE
)
# 6) Now 'df' is ready for modeling
# 1) Plot Price vs Mileage
ggplot(df, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", se = TRUE, color = "blue") +
labs(
title = "Price vs Mileage",
x = "Mileage",
y = "Price"
) +
theme_minimal()
# 2) Plot Price vs Year
ggplot(df, aes(x = Year, y = Price)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", se = TRUE, color = "green") +
labs(
title = "Price vs Year",
x = "Year",
y = "Price"
) +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment