Home > OS >  How to transfer my files to R Projects, and then to GitHub?
How to transfer my files to R Projects, and then to GitHub?

Time:03-20

I have 3 r scripts;

  1. data1.r
  2. data2.r
  3. graph1.r

the two data files, run some math and generate 2 separate data files, which I save in my working directory. I then call these two files in graph1.r and use it to plot the data.

How can I organise and create an R project which has;

  • these two data files - data1.r and data2.r
  • another file which calls these files (graph1.r)
  • Output of graph1.r

I would then like to share all of this on GitHub (I know how to do this part).

Edit -

Here is the data1 script

df1 <- data.frame(x = seq(1,100,1), y=rnorm(100))
save(df1, file = "data1.Rda")

Here is the data2 script

df2 <- data.frame(x = seq(1,100,1), y=rnorm(100))
save(df2, file = "data2.Rda")

Here is the graph1 script

load(file = "data1.Rda") 
load(file = "data2.Rda") 
library(ggplot2)
ggplot() geom_point(data= df1, aes(x=x,y=y)) geom_point(data= df2, aes(x=x,y=y))

Question worded differently -

How would the above need to be executed inside a project?

I have looked at the following tutorials -

CodePudding user response:

I have broken my answer into three parts:

  • The question in your title
  • The reworded question in your text
  • What I, based on your comments, believe you are actually asking

How to transfer my files to R Projects, and then to GitHub?

From RStudio, just create a new project and move your files to this folder. You can then initialize this folder with git using git init.

How would [my included code] need to be executed inside a project?

You don't need to change anything in your example code. If you just place your files in a project folder they will run just fine.

An R project mainly takes care of the following for you:

  • Working directory (it's always set to the project folder)
  • File paths (all paths are relative to the project root folder)
  • Settings (you can set project specific settings)

Further, many external packages are meant to work with projects, making many task easier for you. A project is also a very good starting point for sharing your code with Git.

What would be a good workflow for working with multiple scripts in an R project?

One common way of organizing multiple scripts is to make a new script calling the other scripts in order. Typically, I number the scripts so it's easy to see the order to call them. For example, here I would create 00_main.R and include the code:

source("01_data.R")
source("02_data.R")
source("03_graph.R")

Note that I've renamed your scripts to make the order clear.

In your code, you do not need to save the data to pass it between the scripts. The above code would run just fine if you delete the save() and load() parts of your code. The objects created by the scripts would still be in your global environment, ready for the next script to use them.

If you do need to save your data, I would save it to a folder named data/. The output from your plot I would probably save to outputs/ or plots/.

When you get used to working with R, the next step to organize your code is probably to create a package instead of using only a project. You can find all the information you need in this book.

  • Related