Error reading file

I can't understand why it's stating there is no such file when it is sitting in the environment:

# Libraries
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union

# Thresholds
Thresh.Brix.min <- 15
Thresh.Brix.max <- 30

Thresh.Pol.min <- 50
Thresh.Pol.max <- 105

Thresh.Fibre.min <- 4
Thresh.Fibre.max <- 25

Thresh.Ash.min <- 0
Thresh.Ash.max <- 8

# Import the NIRS data (NIRPred.csv), a CSV file with variable names in the first row, comma (“,”) as field separator character, and dot (“.”) as decimal point character
NIRData<- read.table("NIRPred.csv", header=TRUE, sep = ",", dec= ".")
#> Warning in file(file, "rt"): cannot open file 'NIRPred.csv': No such file
#> or directory
#> Error in file(file, "rt"): cannot open the connection

# Display summary 
#> Error in data.frame(NIRData): object 'NIRData' not found

# Assign the DateTime variable to the POSIXct data type
NIRData$DateTime <- as.POSIXct(NIRData$DateTime, format = "%Y-%m-%d %H:%M:%S")
#> Error in as.POSIXct(NIRData$DateTime, format = "%Y-%m-%d %H:%M:%S"): object 'NIRData' not found

# Use transform() with the floor() function applied to ScanID to create a new variable called LabID in the NIRData table.
#> Error in eval(lhs, parent, parent): object 'NIRData' not found

Here's a screenshot:

1 Like

The file "NIRPred.csv" is not being located. Given a bare file name, read.table is looking in the current working directory for the file, and is not finding it. As such, NIRData isn't being assigned and all the other errors stem from this fact.

Try specifying the full path to the file just to get that working. Using file.exists should return TRUE if the file can be found at the location you specify.

# Specify full path to data, e.g /directory/path/to/NIRData.csv
path_to_my_data <- file.path("directory","path","to","NIRData.csv")

# Check if file exists at that path

Once you know the file exists at the location that you've specified on disk, then pass that path to read.table.

NIRData<- read.table(path_to_my_data, header=TRUE, sep = ",", dec= ".")
1 Like

Thank you. Just wondering how it can't find the file if it is view-able in the environment as per the screenshot above?

I ran the code and received an error:

# Specify full path to data, e.g /directory/path/to/NIRPred.csv
path_to_my_data <- file.path("cloud","project","Assignment 2 Part B","NIRPred.csv")

# Check if file exists at that path
#> [1] FALSE

Here's another screenshot:

As you can see the file is visible. Could it be corrupt? Or is it to do with the workspace?

Your directory name appears to begin with a capital 'C'. Also, "Assignment 2 Part B" is not a directory.

path_to_my_data <- file.path("Cloud","project","NIRPred.csv")

I find that the path to file on disk is better investigated with whatever file explorer your OS uses.

1 Like

I checked the source file:


I have never had this problem before...

Hi @ppines , it looks like your code was not formatted correctly to make it easy to read for people trying to help you. Formatting code allows for people to more easily identify where issues may be occurring, and makes it easier to read, in general. I have edited you post to format the code properly.

In the future please put code that is inline (such as a function name, like mutate or filter) inside of backticks (`mutate`) and chunks of code (including error messages and code copied from the console) can be put between sets of three backticks:

example <- foo %>%
  filter(a == 1)

This process can be done automatically by highlighting your code, either inline or in a chunk, and clicking the </> button on the toolbar of the reply window!

This will help keep our community tidy and help you get the help you are looking for!

For more information, please take a look at the community's FAQ on formating code

Additionally, it is extremely hard to help debug code and/or error messages when screenshots are posted. Posting screenshots, instead of actual code/error messages, is likely to decrease your chances of getting the help you are looking for in general. Instead of posting a screenshot, please copy and paste the code and error message and format the pasted code/error messages as shown above.


You're accessing RStudio remotely. That's useful to know. That means your local file browser won't be helpful for verifying the location of the data file.

What is the result of the following code for you?

caps_path <- file.path("Cloud","project","NIRPred.csv")

lower_path <- file.path("cloud","project","NIRPred.csv")

Looks like you're sourcing the R file directly. That should run it in the global env by default, and that may be why it's not reading the NIRData from the environment listed in the UI tab as you're expecting.


Thanks! Was advised to used "reprex". What you see is a reprex......

Thanks for the feedback, I also thought it would run it in the global env by default, as per my other files. I tried both codes as per your suggestion and received "FALSE" in both cases. I just find it odd that the file is in the global environment but can't be read/found.

Thank you all for your very helpful advice. I have deleted the file and started again as I believe the source file was corrupted. I have uploaded it once again and it was read into the global environment successfully.

Yes a reprex is important, and you were close! but there were some issues. Did you only have issues reading the file when trying to render the reprex? If so, this is because reprex can not read files from your computer. This is done, because if someone copies and pastes your code, they will not be able to read the file.

From there, everything else produced errors because the dataset did not exist. There are several ways to share data in a reprex but unfortunately, reading them from a file is not one of them.

As for the code formatting, that is a separate thing. That is strictly an aesthetic thing to make your code readable on this site. If you render a reprex with the reprex package than it should save the output to your clipboard which will be correctly formatted when you paste it here. However, if you copy and paste the output yourself than you will need to take care of the formatting as I mentioned in my previous comment!