How to create PowerPoint reports with R

How to create PowerPoint reports with R

File Manipulation, R
In my last post, we discussed how to create and read Word files with R's officer package. This article will expand on officer by showing how we can use it to create PowerPoint reports. Getting started Let's get started by loading officer. [code lang="R"] library(officer) [/code] Next, we'll create a PowerPoint object in R using the read_pptx function. [code lang="R"] pres <- read_pptx() [/code] To add a slide, we use the add_slide function. The first slide we'll create is the title slide. We specify the type of slide in the layout parameter. There's several other possibilities here including "Title and Content", "Blank", "Title Only", "Comparison", "Two Content", and "Section Header". Secondly, we use ph_with to add the title text. [code lang="R"] # add title slide pres <- add_slide(pres, layout =…
Read More
How to read and create Word Documents in R

How to read and create Word Documents in R

File Manipulation, R
Reading and creating word documents in R In this post we'll talk about how to use R to read and create word files. We'll primarily be using R's officer package. For reading data from Word Documents with Python, click here. Creating Word reports with the officer package The first thing we need to do is to install the officer package. [code lang="R"] install.packages("officer") [/code] We'll also be using the dplyr package, so you'll need to install that the same way if you don't have it already. Next, let's load each of these packages. [code lang="R"] library(officer) library(dplyr) [/code] Now, we'll get started creating a report! First, we will use the read_docx function to create an empty Word file. [code lang="R"] # create empty Word file sample_doc <- read_docx() [/code] Adding…
Read More

How to change a file’s last modified date with R

File Manipulation, R
This relatively quick post goes through how to change a file's last modified date with base R. How to change a file's modified time with R Let's say we have a file, test.txt. What if we want to change the last modified date of the file (let's suppose the file's not that important)? Let's say, for instance, we want to make a file have a last modified date back in the 1980's. We can do that with one line of code. First, let's use file.info to check the current modified date of some file called test.txt. [code lang="R"] file.info("test.txt") [/code] We can see above by looking at mtime that this file was last modified December 4th, 2018. Now, we can use a function called Sys.setFileTime to change the modified date…
Read More
10 R functions for Linux commands and vice-versa

10 R functions for Linux commands and vice-versa

File Manipulation, R, System Administration
This post will go through 10 different Linux commands and their R alternatives. If you're interested in learning more R functions for working with files like some of those below, also check out this post. How to list all the files in a directory Linux R What does it do? ls list.files() Lists all the files in a directory ls -R list.files(recursive = TRUE) Recursively lists all the files in a directory and all sub-directories ls | grep "something" list.files(pattern = "something") Lists all the files in a directory containing the regex "something" R [code lang="R"] list.files("/path/to/directory") list.files("/path/to/do/directory", recursive = TRUE) # search for files containing "something" in their name list.files("/path/to/do/directory", pattern = "something") # search for all CSV files list.files("/path/to/do/directory", pattern = ".csv") [/code] Linux [code lang="bash"] ls /path/to/directory…
Read More
R: How to create, delete, move, and more with files

R: How to create, delete, move, and more with files

File Manipulation, R, System Administration
Though Python is usually thought of over R for doing system administration tasks, R is actually quite useful in this regard. In this post we're going to talk about using R to create, delete, move, and obtain information on files. How to get and change the current working directory Before working with files, it's usually a good idea to first know what directory you're working in. The working directory is the folder that any files you create or refer to without explicitly spelling out the full path fall within. In R, you can figure this out with the getwd function. To change this directory, you can use the aptly named setwd function. [code lang="R"] # get current working directory getwd() # set working directory setwd("C:/Users") [/code] Creating Files and Directories…
Read More
File Manipulation with Python

File Manipulation with Python

File Manipulation, Python, System Administration
Getting started Python is great for automating file creation, deletion, and other types of file manipulations.  Two of the primary packages used to perform these types of tasks are os and shutil.  We'll be covering a few useful highlights from each of these. [code lang="python"] import os import shutil [/code] How to get and change your current working directory You can get your current working directory using os.getcwd: [code lang="python"] os.getcwd() [/code] Any actions you take without specifying a directory will be assumed to be associated with your current working directory i.e. if you create or search for a file without specifying a directory, Python will assume you're in the value of os.getcwd(). To change your working directory, use os.chdir: [code lang="python"] os.chdir("C:/path/to/new/directory") [/code] How to merge a directory name…
Read More