Untitled

 avatar
unknown
plain_text
8 days ago
4.7 kB
8
Indexable
---
title: 'PS3: rmarkdown, files'
author: "Jonas Oco"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## 1 Markdown

#### 1. Problem Set Formatting

Done

#### 2. Create a bullet list of (at least 3) colors

-   Red
-   Blue
-   Green

#### 3. Create a numbered list of (at least 3 cities)

1.  Seattle
2.  San Francisco
3.  New York

#### 4. Create a pre-formatted text

```         
This is a preformatted text
by Jonas Oco
Feburary 2nd, 2025
```

#### 5. Write a sentence that includes bold and italic text

This is a sentence with **bold** and *Italic* text!

#### 6. Include a image in text

![This is the University of Washington Quad!](Campus%20Drone%20Fall-1170x500.jpg)

## 2 Working with files

### 2.1 Working Directory

#### 1.

```         
Using getwd() in the console I find that my working directory is in "/Users/jonasoco"
```

#### 2.

```         
R Markdown documents are knit in a separate R process
The working directory for knitting is set to where the .Rmd file is located
Each process (Console vs Knitting) has its own working directory
```

#### 3.

```         
The folder is INFO 201 where my .rmd file is saved.
```

#### 4.

```{r}
getwd()
```

```         
When I run the getwd() in markdown it gives me the correct directory but is not the same path given when running getwd() in the console.

When you run getwd() in a markdown file during knitting, it results in the directory where your .Rmd file is saved, which may differ from the working directory shown in the RStudio console.

Even though the paths appear similar or close, they are not exactly the same because knitting is a separate process with its own isolated R environment and working directory.

This matters because the different working directories can cause file path issues, meaning that code or file references that work in the RStudio console might not work exactly the same way when the document is knitted.
```

### 2.2 List files

#### 1. Now create a folder on your Desktop where you put

-   At least 3 pdf files
-   At least 3 picture files
-   At least 3 other files
-   At least 3 directories (you can create new empty directories)

```{r}
list.files("../../../jonasoco/Desktop/List Files INFO 201/")
```

#### 2. Sketch

![](SketchINFO.jpg)

```         
- The path where my rmarkdown file is saved is Users/Jonasoco/Documents/Info 201
- The path where I created a new folder on my desktop is Users/Jonasoco/Desktop/List Files INFO 201
```

#### 3. Relative path of the folder created on desktop with respect to the rmarkdown working directory

1.  Up(into Documents)
2.  Up(into Jonas Oco)
3.  Down(into Desktop)
4.  Down(into List Files INFO 201)

#### 4.

![](SSpath.png) \#### 5.

```{r}
Q5 <- list.files("../../../jonasoco/Desktop/List Files INFO 201/")
cat(Q5)
```

#### 6. Do you see the same files as in the image?

```         
Yes
```

#### 7. Do you see the complete file name (including extensions) in the file explorer image?

```         
Yes
```

### 2.3 How big are the files?

#### 1.

(a) 

```{r}
#(a)
dir_path <- "../../../jonasoco/Desktop/List Files INFO 201/"
files <- list.files(path = dir_path, full.names = FALSE)    

# Loop over each file
for (file in files) {
  file_path <- file.path(dir_path, file)  # Create full file path
  info <- file.info(file_path)  # Get file info
  
#(b)
  if (info$isdir) {
    # If it's a directory, print a message
    print(paste(file, "is a directory"))
  } else {
    # If it's not a directory, get and print the file size
#(c)
    file_size <- prettyNum(info$size, big.mark = ",")
#(d)
    print(paste(file, ":", file_size))
  }
}
```

### 2.4 Display the files(extra credit, 5pt)


## 3 Control Structures

#### 1.

```{r}
dir_path <- "../../../jonasoco/Desktop/List Files INFO 201/"

# List files in the directory
files <- list.files(path = dir_path, full.names = FALSE)  

# Categorize files using nested ifelse() with endsWith()
file_types <- ifelse(endsWith(files, ".jpg") | endsWith(files, ".jpeg") | 
                     endsWith(files, ".png") | endsWith(files, ".gif"),
                     paste(files, "is an image file"),
                     ifelse(endsWith(files, ".pdf"),
                            paste(files, "is a pdf file"),
                            paste(files, "is something else")
                     )
)

# Print the result
print(file_types)


```

#### 2 & 3(extra credit)

```{r}
num_png <- sum(endsWith(files, ".png"))

# Print the correct sentence based on count
if (num_png == 0) {
  print("There are no png files")
} else if (num_png == 1) {
  print("There is a single png file")
} else {
  print(paste("There are", num_png, "png files"))
}
```
Leave a Comment