```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
Basic operations in R:
- Download (zip) files
- Extract compressed files to a folder
- Check & Create a folder or subfolders
- List & Read files in a folder
# Download
```{r}
download.file("https://github.com/tpetric7/tpetric7.github.io/archive/refs/heads/main.zip",
"d:/Users/teodo/Downloads/tpetric7-master.zip")
Check & create directory
pot <- "d:/Users/teodo/Downloads/tpetric7-master"
exist <- dir.exists(pot)
exist
ifelse(exist == FALSE,
dir.create(pot, showWarnings = TRUE, recursive = TRUE),
"directory already exists")
Unzip
unzip("d:/Users/teodo/Downloads/tpetric7-master.zip", exdir = pot)
Create subfolders
subfolder_names <- c("a","b","c","d")
for (i in 1:length(subfolder_names)){
folder <- dir.create(paste0(pot, "/", subfolder_names[i]))
}
List files
seznam <- list.files(pot, pattern = "\\.txt$", recursive = TRUE, full.names = TRUE)
seznam
Read files
library(tidyverse)
alltxt <- seznam %>% map(read_lines)
head(alltxt[[5]], 12)
cat(head(alltxt[[5]], 12))
Base R:
alltxt <- lapply(seznam, readLines)
head(alltxt[[5]], 12)
cat(head(alltxt[[5]], 12))