Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I commit changes to GitHub from within a R script?

I am trying to automate some basic git operations from within a R script. I am using Rstudio on Windows OS. This may be helpful for example if you wished to update GitHub when a script finishes performing some automated task.

I wrote some simple functions that utilize R's shell() function and the Window's & pipe operator to send a chain of commands to the OS terminal:

# Git status.
gitstatus <- function(dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = "git status"
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

# Git add.
gitadd <- function(dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = "git add --all"
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

# Git commit.
gitcommit <- function(msg = "commit from Rstudio", dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = paste0("git commit -am ","'",msg,"'")
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

# Git push.
gitpush <- function(dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = "git push"
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

My gitstatus, gitadd, and gitpush functions work. The gitcommit function does not work. It generates the following error:

fatal: Paths with -a does not make sense.
Warning message:
In shell(cmd) : 'd: & cd D:/Documents/R/my_path & git commit -am 'commit from Rstudio'' execution failed with error code 128

The gitpush function works because if you switch to the terminal or git within Rstudio, you can commit changes and then successfully call gitpush.

Any ideas on how to fix this issue?

...

Note: I have Git bash installed, and I can successfully use git from the Windows command terminal and Rstudio. I also tried an alternative strategy which was to have R write a temporary .bat file and then execute this, but this strategy also gets hung up on the commit step.

like image 822
twb10 Avatar asked Mar 30 '19 14:03

twb10


1 Answers

Solution

The answer lie within Dirk Eddelbuettel's drat package function addrepo. It was also necessary to use git2r's config function to insure that git recognizes R. git2r's functions probably provide a more robust solution for working with git from an R script in the future. In the meantime, here's how I fixed the problem.

  • Install git2r. Use git2r::config() to insure git recognizes R.

  • From Dirk's code I modified the gitcommit() function to utilize sprintf() and system() to execute a system command:

# Git commit.
gitcommit <- function(msg = "commit from Rstudio", dir = getwd()){
  cmd = sprintf("git commit -m\"%s\"",msg)
  system(cmd)
}

Sprintf's output looks like this:

[1] "git commit -m\"commit from Rstudio\""

Example

#install.packages("git2r")
library(git2r)

# Insure you have navigated to a directory with a git repo.
dir <- "mypath"
setwd(dir)

# Configure git.
git2r::config(user.name = "myusername",user.email = "myemail")

# Check git status.
gitstatus()

# Download a file.
url <- "https://i.kym-cdn.com/entries/icons/original/000/002/232/bullet_cat.jpg"
destfile <- "bullet_cat.jpg"
download.file(url,destfile)

# Add and commit changes. 
gitadd()
gitcommit()

# Push changes to github.
gitpush()

Well, the pic looks wonky, but I think you get the point.

like image 191
twb10 Avatar answered Oct 18 '22 02:10

twb10