Created
June 19, 2019 17:39
-
-
Save andrewbtran/0d34405de79341d745f2d9ef7e760e3a to your computer and use it in GitHub Desktop.
gsiterator template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(googlesheets) | |
# Authorize user on Google | |
gs_auth(new_user = TRUE) | |
# Naming the sheet | |
name_of_googlesheet <- "Testing out gsiterator" | |
# Test array | |
states <- state.name | |
# Generic loop | |
for (i in 1:length(states)) { | |
# Creating a random dataframe row | |
state <- states[i] | |
important_number <- sample(1:100, 1) | |
row <- data.frame(state, important_number) | |
if (i == 1) { | |
# Creating a big dataframe base | |
df <- row | |
# Creating a new google sheet | |
gs_new <- gs_new(name_of_googlesheet, input=df) | |
} else { | |
# Binding the row to the big dataframe | |
df <- rbind(df, row) | |
# Appending the new row to the google sheet | |
gs_new <- gs_new %>% | |
gs_add_row(input=row) | |
} | |
# Every 10 rows, write it out as a csv locally | |
if (i %% 10 == 0) { | |
write.csv(df, "combined_all.csv") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment