1

I'm new to R and I'm having trouble transferring an analysis in an R Script file to an R Markdown file. I'm using a MacBook Pro so my working directory right now is my default computer folder with my first&last name. When I use the import dataset button in the environment or files windows, this is the code chunk i get

library(readr)
hotel_bookings <- read_csv("Library/Mobile Documents/com~apple~CloudDocs/Desktop/data analysis resources/datasets/csv data sets/hotel_bookings.csv")

When I first tried to knit this into a R Markdown HTML I got the error that the dataset did not exist in my current working directory. Now I though that this was because I changed the location of my files from iCloud to my actual computer but this isn't the case...

library(readr)
hotel_bookings <- read_csv("Desktop/data analysis resources/datasets/csv data sets/hotel_bookings.csv")

I ran this new code and still got the same error message. After playing around for a little bit I realized it was trying to get it from my username directory, so I added this piece of code to fix it.

library(readr)
hotel_bookings <- read_csv("/Users/mikalabarbour/Desktop/data analysis resources/datasets/csv data sets/hotel_bookings.csv")

I guess my main concern is why doesn't my computer automatically pull this code chunk when I try and import the dataset from it's location and is there a way to fix this? My working directory has been the same this entire time, and I even moved my files to my computer so that they would be included in my directory. The code chunk works fine when I execute it in my console but I have to edit it like the code chunk above for it to work within an R Markdown doc. Any help would be very appreciated! Thank you.

  • 2
    Few hints: 1. Use R-projects in separate folders. 2. check out `getwd()` and `setwd()` for getting and setting the current working directory. 3. If you really need to read something from a different folder use `file.path()` instead of copying it directly or pasting it. – tester Feb 04 '22 at 00:14

0 Answers0