Home > Mobile >  Directory changes when importing file in R
Directory changes when importing file in R

Time:03-06

In RMarkdown, I've set my root directory and console working directory but I can't get read.pnm to import from the correct directory. This is what I have:

knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = "C:/Users/customdir/.../folder")

library(pixmap)

getwd()
img = read.pnm("~/folder2/img.pgm")

From the output of getwd(), I can tell it's in the right directory but I get the error:

Warning in file(file, open = "rb") :
  cannot open file 'C:/Users/user/Documents/folder2/img.pgm': No such file or directory
Error in file(file, open = "rb") : cannot open the connection

I don't know why it's looking in "C:/Users/user/Documents/" since that's not the directory I specified.

Is there any other directory I need to change?

CodePudding user response:

The "~" part of the file name points to your personal home directory (not your current working directory). When you use a "~" you are giving an absolute path, not a path relative to your working directory. If you want to find the file in your current working directory, use

img = read.pnm("folder2/img.pgm")
  • Related