Repo from Rstudio to Github

Initialise GitHub repo from RStudio without Using Command Line

Solved using a bit of google and a bit of Hansel Palencias suggestion:

Start by creating an empty GitHub repository on your repo page then enter the following commands:

# Clone existing github repo using url and linking this to your project path
clone(url = "https://github.com/user/name_of_repo.git", local_path = "path")

setwd("path")

# Assign variable repo as current local repository
repo <- repository()

# Configure access rights with github username and profile
config(repo, user.name='user name', user.email='email address')

# Add a PAT in local renviron folder
edit_r_environ()

# Save PAT as cred token
cred <- cred_token()

# Stage changs to commit
add(repo, "file.R")
commit(repo, "Commit message")

# Push changes
push(repo, 'origin', 'refs/heads/master', credentials = cred)

Bit of a hack but solves the problem. Will attempt using remotes() command as suggested as a more effective solution.

How to upload local RStudio Project to Github including commit history?

All you should need would be to:

  • add an origin to your local repository referencing the new (empty) GitHub repository
  • push

That is:

git remote add origin https://github.com/<you>/<newRepo>
git push -u origin master
# or
git push --mirror

Rstudio: Changing origin for git version control of project

Git, Github and Rstudio are different things. You could use git as local version control tools. You might connect your local repo to Github account which is based on git by push/pull. Rstudio just makes a user interface for git and supplies the function to push the repo into remote server based on git to make version control(not only Github, but also Gitlab).

So for your issue, if you do not want to pay for github for a private repo, all of your code would be public and I don't think it is good before your finally finished your thesis. But version control could be made locally with git only. Just use git shell to control the version.

However, as a student, github could support private repo here for you. Just register and find your student package. Then just remove the url for remote repo after you cd to your workdir in command line, use the following code to find your remote url(mostly you might fing origin):

git remote -v

Then use this to remove them:

git remote rm origin

Now you could use version control locally. If you want to connect this repo to your remote github private repo, use this:

git remote add origin https://github.com/[YourUsername]/[YourRepoName].git

RStudio would find this information about git and support your following operation. Project in RStudio is different with git, although project support git as version control tool. So you need git in command line or shell to solve your problem.

Repo from RStudio to Github

The only way you could create a repository on github directly from your computer, without having to create it with their website first, would be to create a remote branch directly from git on your system. This is possible on some git installation, but not on Github.

However, Github provides an API that allows to create the repository from the command line, via a call to curl for example. You will find information on how to do it in this answer (not tested) :

curl -u 'USER:PASS' https://api.github.com/user/repos -d '{"name":"REPO"}'
git remote add origin git@github.com:USER/REPO.git
git push origin master

But I don't think you will be able to do it directly from RStudio : you will need to put your project under version control, and then to execute the three commands provided in the answer in a shell.

How to download entire repository from Github using R?

Overview

You can download an entire repository from GitHub using R in three steps:

  1. Copy the .zip URL from the Clone or download button on the GitHub repository of interest. Be sure to copy the link address from Download ZIP and not the HTTPS URL.

Note: This step assumes you are interested in the main branch of the GitHub repository of interest. If this is not the case, be sure to navigate to the branch you are interested in downloading. Please note that main is now the default name of the main branch of a GitHub repository. For more context, please read this article by Alexis Moody hosted on DEV and this article by GitHub.

Sample Image


  1. Paste the .zip URL into the url parameter of download.file() to download the .zip file of interest. Since this is a GitHub repository, it is helpful to assign the destfile parameter the same name as the repository of interest (in this case, destfile = "meetingsR-master"). The "-master" portion of the destfile parameter name comes from declaring the branch name of the repository of interest that you wish to download.

    • Note: please see note in step one to understand why you might need to replace "master" with the term "main".
  2. Use unzip() to unzip the downloaded .zip file.

Reproducible Example

Be mindful to change the file paths when using the code down below.

# set working directory so I know where the .zip file will be located
setwd(dir = "/some/path/")

# download a .zip file of the repository
# from the "Clone or download - Download ZIP" button
# on the GitHub repository of interest
download.file(url = "https://github.com/jumpingrivers/meetingsR/archive/master.zip"
, destfile = "meetingsR-master.zip")

# unzip the .zip file
unzip(zipfile = "meetingsR-master.zip")

# set the working directory
# to be inside the newly unzipped
# GitHub repository of interest
setwd(dir = "/some/path/meetingsR-master/")

# examine the contents
list.files()
# [1] "_book"
# [2] "_output.yml"
# [3] "01-events.Rmd"
# [4] "02_useR_groups_aaa.Rmd"
# [5] "02_useR_groups_asia.Rmd"
# [6] "02_useR_groups_europe.Rmd"
# [7] "02_useR_groups_middle_east_africa.Rmd"
# [8] "02_useR_groups_north_america.Rmd"
# [9] "02_useR_groups_oceania.Rmd"
# [10] "02_useR_groups_south_america.Rmd"
# [11] "03-Rladies.Rmd"
# [12] "css"
# [13] "deploy.sh"
# [14] "DESCRIPTION"
# [15] "docs"
# [16] "index.Rmd"
# [17] "inverse.png"
# [18] "logo.png"
# [19] "Makefile"
# [20] "NAMESPACE"
# [21] "R"
# [22] "README.md"
# [23] "Rmeetings.Rproj"

# end of script #

with rstudio and github, issue with renamed repo

I'm hesitant to throw this out as an answer, but: you can manually edit the ./.git/config file to update the [remote ...] section to change the remote URL. I have done this confidently enough with an empty repo ...

Check for presence of the tag with grep -rli tags.git .git/*; if all you get is .git/config, then you're good to edit and move on. If you find other files, though, I don't know for certain that they will be updated as you continue with your git remote work. In that case, it might be helpful to look at https://help.github.com/en/articles/changing-a-remotes-url in order to formally change the URL.



Related Topics



Leave a reply



Submit