R Markdown help

1,796 views
Skip to first unread message

TE...@csumb.edu

unread,
Feb 1, 2015, 2:54:31 PM2/1/15
to montere...@googlegroups.com
A little background: I am using a Mac computer, and my professor uses a PC
I am starting to play around with R Markdown in my stats class, and my professor posted our lab in a .Rmd format. I tried to complete the lab, but when I try to knit it, it doesn't prompt me to load a dataset as it does when my professor does it. Another student in the class (she has a Mac as well) tried to do it and had the same problem. Does anyone know how to fix this?

Bill Evans

unread,
Feb 1, 2015, 3:26:38 PM2/1/15
to TE...@csumb.edu

Some info you sent in the private email included this, valuable if somebody else is having the similar problem:

Error in file.choose(): file choice cancelled. Calls: <Anonymous> ... eval -> eval -> read.csv -> read.table -> file.choose

So, the issue here is that the knitting of a document is meant (and enforced) to be a completely automated thing with no human interaction. That means functions that typically interact with the user no longer work. Examples: browser, readline, and yes, file.choose. There are many things at play for the why of this, but long story short, there is no way around it (for now, at least): you may not rely on code that requires user input to work properly.

The workaround for this is to use the correct path. Depending on the OS, this may be obscure or confusing (in the case of Windows and the inane use of backslashes (\)) or simply obscure (in the case of Mac, trying to keep the user from needing to know what’s going on … to the point of sheltering). To find out what the actual path is, before you try to knit, you can run file.choose() by itself, navigate to the file, and the full path (in a format with which R is comfortable and happy) will be returned.

## this fails in a knit
read.csv(file.choose())

file.choose()                         # I'll click on the file I want
## [1] "C:\\Users\\r2\\Documents\\Projects\\speedchase.csv"

## now edit the Rmd file and change file.choose() to that string
read.csv("C:\\Users\\r2\\Documents\\Projects\\speedchase.csv")

Hope this answers your question!

-bill

Bill Evans

unread,
Feb 1, 2015, 3:51:18 PM2/1/15
to montere...@googlegroups.com, TE...@csumb.edu

For the record, this can actually be done when not using RStudio … but RStudio is by far the better Rmd-editing method at the moment, and the workarounds bring a bit of baggage. So, my statement that it was a knitr issue were not factual, it’s an rstudio thing.

NB: regardless, there are two reasons why I believe it is not a good idea to use file.choose() in a Rmd file (for analytical reporting):

  1. For “true reproducibility” (internally), if you try to recreate this document at a later date and don’t have this version of the data available, then you cannot reproduce the report; and

  2. For “true reproducibility” (externall), if your version of a CSV file (my assumption) is different than my version, than our results may be different. Or even better, if you give me your Rmd file without a CSV file, I can’t really use it. (One valid argument against this is that you be sharing your PDF, DOCX, or HTML report and not the source Rmd file.)

Alternatives run the gamut of convenience and purity:

  1. Include the raw data in the file, verbatim. Using dput and structure within the code block will ensure that the data is in the original form. This is often problematic, especially for large datasets, and can make a report rather cluttered. Though I can’t help with the size problem, it does not need to clutter your rendered report. Have one of your first code blocks include on the initial line (immediately after the 3 backticks): {r echo=FALSE, include=FALSE}. This evaluates the code but includes neither the source code (echo) nor its output (include) in the document. Handy.

  2. Save the “then current” version of the CSV file as a time-date stamped file, so that you are less likely to modify it later and regenerate the report. I have found that scripts work great until I decide to reformat the spreadsheet a little and export as the same CSV file that I’ve been using in my reports. When you look at it this way, you might immediately notice that one slip-up will break your coding assumptions of the CSV file, not to mention potentially change the data. If you are reading your data from a file named analoga.csv, make a copy of it (or just rename it, up to you) to analoga-20150201-1245.csv and hard-code that date-time-stamped file into your Rmd. If you later modify the format of analoga.csv, perhaps to filter out data, fix known anomalies, etc, then copy/rename it to a newer file such as analoga-20150205-2341.csv (you were working late!). At some later time, you should check to make sure that your code that uses analoga-20150201-1245.csv still works with the newer file, at which time you can make the conversion.

Actually, more options exist, but I think these first two are good for now …

-bill

vil...@gorener.com

unread,
May 11, 2017, 12:15:17 PM5/11/17
to Group of R Users in the Monterey Peninsula (GRUMPs), TE...@csumb.edu
Hello Bill, 
I have tried your solution but I still get the same error...Have tried several other suggestions on the web and getting little frustrated it is taking sooo long to find a solution and finish my assignment...
Do you have any other recommendations?

thank you

Vildan

Bill Evans

unread,
May 11, 2017, 11:59:13 PM5/11/17
to Group of R Users in the Monterey Peninsula (GRUMPs), TE...@csumb.edu, vil...@gorener.com
Vildan,

I have no idea what may be causing your problem. In order to help, I need to see the code you are using and the errors that you get.

Bill
Reply all
Reply to author
Forward
0 new messages