As we get further along in using R, we will want to share our work with others. This can be hard, as the installation of R on our laptops is not going to match another person’s computer.

Before you start this, you should be comfortable with managing files/folders on your computer.

Creation and publishing walkthrough

We will generally want to use a project for your submissions.

Follow these steps to create your project, load the data files, publish it online, and submitted on eCampus.

Further explanation

If you follow the process above, you’ll generally be able to get your project set up properly. However, it’s useful to understand what is actually happening during this process. The below sections explain the key ideas.

Working Directory.

R uses a default starting point on your computer whenever it tries to find a file. Run getwd() to find your computer’s working directory.

This isn’t the same folder as your code!

getwd()

You can set the working directory with setwd(). Be sure to use slashes going bottom-left to upper-right.

s <- getwd() # s is the current directory.

# Give setwd a string value 
setwd(s)

getwd() # test to make sure that the new path works!

When you create a project, it automatically sets the working directory to that folder. That allows you to use relative files paths, which are explained in the next sections.

File paths

We work with files using either a partial or full path. Here are some valid examples,

  • file.csv
  • folder/file.csv
  • ./folder/../file.csv
  • c:/folder/file.csv

One tricky part is that the slash goes in differnt directions on PCs and Macs. In R, you should always use a forward-slash (as shown above).

Below are some samples of opening a file in a different folder.

# Sample tibble
test_t <- tibble(x = c(1, 2, 3, 4))


# Same folder
# 
# Write to the working directory and then open with a relative path
write_csv(test_t, 'test.csv')
loaded_t <- read_csv('test.csv')

# Write a subfolder in our project
# Do not start the path with a slash!
write_csv(test_t, 'test/test.csv')
loaded_t <- read_csv('test/test.csv')

# Relative folder
# 
# You can start with a ./
# If you want to go up a folder, you can also use ../
# Below starts at the wd, goes into the test folder, and walks back up.
# So, it's a lot of effort to do the same thing as test.csv 
write_csv(test_t, './test/../test.csv')
loaded_t <- read_csv('./test/../test.csv')

# Full path
# 
# Full paths are generally a bad idea. That means that you can't use the code
# on another computer.
# 
# If you use a full path, be sure to use c:/folder/file.csv
# Note that you use a slash from bottom-left to upper-right
write_csv(test_t, 'C:/Users/ndg00008/Dropbox/wvu/Courses/course_eda/test.csv')
loaded_t <- read_csv('C:/Users/ndg00008/Dropbox/wvu/Courses/course_eda/test.csv')

When you use a project, try to only use a relative file path (i.e., “data.csv”)

Rmd Markdown

A Rmd file uses markdown. You don’t need to know all of the syntax. But, you should start each section with a #, and label sub-sections with ## or ###

Each section of code can be inserted with the insert new code chunk button on the upper-right corner of your screen (it looks like a green C).

You can set various options in the header. The two most useful are:

  • eval: show the results of the code (TRUE or FALSE)
  • echo: show or hide the R code (TRUE or FALSE)

You can manually type these into the top line of a code block, or click the gear icon on the upper-right corner.

I’ve had issues with the ‘setup’ block, and usually remove it. Pulling out this line of code ‘knitr::opts_chunk$set(include = FALSE)’ is generally helpful in debugging problems.

If you have troubles rendering, click the dropdown on the top button bar and change it to knit to html. Then, try running each block in your document.

Samples

A block with ‘{r include=FALSE}’ has no output or code displayed, but it does run.

A block with ‘{r echo=FALSE}’ shows shows results, but not any code.

## [1] 1

A block with ’{r echo=TRUE}` shows both the code and its output.

# Print our tibble, showing the code
print(1)
## [1] 1

Application problem

See problems on GitHub