Week 11 - Preparation: Computer set-up and data imporation

CHC9012 | National Taiwan Normal University

???? Goals

Prepare your computer:

  • Projects: How to set-up a project in RStudio?
  • Advanced scripts: How to use Markdown documents to share better scripts?
  • Data importation: Where to find your PCIbex results file, and how to import it to RStudio?

???? Part 1: Prepare your computer: R projects

Why?

We want to be organized and not messy!

  • Section 1.1: Prepare the folder in your computer.
  • Section 1.2: Create the related R project.

1.1 Setting up your folders

  1. Navigate to your Documents (or ???件) folder.
  1. Create your Root Folder: CHC9012_Spring2025
  1. Add the Project Subfolders:
    • ???? Slides: Presentation files.
    • ???? PCIbex_design: Scripts & materials.
    • ???? PreprocessingAnalysesR: Datasets & R code.

You should obtain something like this

Pay attention to the names

Generally, it is better not to use any space when naming folders and files!

1.2 Initializing Your R Project

1. Launch New Project Wizard
Click the blue cube icon in the top-left corner of RStudio.


2. Select project type
Choose “Existing Directory” to link R to your folder structure.


3. Finalize Connection
Browse to PreprocessingAnalysesR, check “Open in a new session”, and click Create Project.

Benefit: An .Rproj file sets your “Working Directory” automatically!

Other advantages of creating projects

  1. One RStudio window = One project
  2. Switch between them without worrying about the working directory of trying to find the right files!
  3. List of your projects on the top-right corner of RStudio. Here is mine for example:

???? In-class exercise!



Task list:

???? Part 2: Better and reproducible scripts with R Markdown

From raw script to functional: R Markdown

1. Create the file ????
Go to New File > R Markdown. This moves you from raw code to a “living” document.

2. Configure (YAML) ???�?
The header between --- controls your title, author, and output format (HTML/PDF).

3. Text + Code chunks ????
Write text normally; insert Code Chunks ({r}) for analysis.

4. Run your codes ??��??
Use the green Play Button to test individual steps.

5. The Final “Knit” ??��
Click Knit to stitch everything into a polished report automatically!

???? In-class exercise!



Task list:

???? Part 3: Download your PCIbex results and import them into RStudio

Fake experiment for this class

Try the experiment

The code of the fake experiment is given on the website

Fake results!

An AI robot did the experiment several times. I also pretended that I was a “bad” participant several times. The results are just to show you how to analyze data from PCIbex, they do not have other value!

  • On the PCIbex platform, click on the Results button.

  • Then, click on Download.

  • Finally, place the file in the folder you just created on your computer!
read.pcibex <- function(filepath, auto.colnames=TRUE, fun.col=function(col,cols){cols[cols==col]<-paste(col,"Ibex",sep=".");return(cols)}) {
  n.cols <- max(count.fields(filepath,sep=",",quote=NULL),na.rm=TRUE)
  if (auto.colnames){
    cols <- c()
    con <- file(filepath, "r")
    while ( TRUE ) {
      line <- readLines(con, n = 1, warn=FALSE)
      if ( length(line) == 0) {
        break
      }
      m <- regmatches(line,regexec("^# (\\d+)\\. (.+)\\.$",line))[[1]]
      if (length(m) == 3) {
        index <- as.numeric(m[2])
        value <- m[3]
        if (is.function(fun.col)){
         cols <- fun.col(value,cols)
        }
        cols[index] <- value
        if (index == n.cols){
          break
        }
      }
    }
    close(con)
    return(read.csv(filepath, comment.char="#", header=FALSE, col.names=cols))
  }
  else{
    return(read.csv(filepath, comment.char="#", header=FALSE, col.names=seq(1:n.cols)))
  }
}
data <- read.pcibex("results_FAKE_FOR_CHC9012.csv")
head(data, 20)

str(data)

summary(data)

???? In-class exercise!



Task list:

???? Assignment for next week

No assignment for next week! Please carefully review the content of these past two weeks, and finalize the data collection of your own project.