“edit” and “read.table” on R for Mac


I am new to R, and just trying to follow and practice what I am learning in class. We learnt the other day “edit” and “read.table” functions. However, I am using macOS High Sierra and both functions didn’t work on R for MAC. Is there anyone professionally using R for Mac?

Demographics <- data.frame(Race=character(0),Age=numeric(0),Gender=character(0),Education=numeric(0))

Data1<-read.table("Demographics.txt", header=TRUE, sep=" ")

The above code worked just fine on R for windows, but not on R for Mac. Can anyone please help me with that? Thank you

Have you loaded the necessary libraries? One of the nice things about making a reprex (short for minimal reproducible example) is that it helps you double check that your code is self-contained. It also helps us help you, since we can be sure we're all working with/looking at the same stuff.

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

Demographics <- data.frame(Race=character(0),Age=numeric(0),Gender=character(0),Education=numeric(0))

Data1<-read.table("Demographics.txt", header=TRUE, sep=" ")

I think, you have two different problems here.

One, when you call edit you are probably getting something along the lines of Error in check_for_XQuartz() : X11 library is missing: install XQuartz from xquartz.macosforge.org (that's what I'm getting on mac as well). The solution to your problem is in error message.

Second problem comes from the fact that your edit command didn't work correctly. If it did, you would've probably saved your table as Demographics.txt that read.table would then read. Since it failed, you don't have the file and it fails to read it in.

Thank you for the reply. I tried the same code on R for windows and everything was alright. It's only not executing on R for mac. Thanks

Did you read what I wrote? edit works on Windows and probably opens up Notepad. On mac it doesn't work, so if you want it to work you need to install the missing library.

I got that, Not sure what library you are talking about tho. Also,

has not been mentioned in your previous response. So, I am not sure what made you think I did not thoroughly go through your previous note. Thank you again. I appreciate your help.

@r_beginner - this is the part I'm talking about. Is this the error you are getting?

Hi r_beginner. I faced the exact same problems with the "edit" and "read.table" functions as you in my R class using macOS High Sierra. In order to get the "edit" function to work, you need to first download XQuartz from www.xquartz.org. Starting with Mountain Lion, it is no longer included by default and needs to be obtained separately.

As for the "read.table" function, try entering something like:

Data1<-read.table("/Users/_username_/Documents/Demographics.txt", header=TRUE, sep=" ")

I tried something similar myself and it worked. I hope that I have been able to help, and that this is still relevant for you.

A quick way to install XQuartz is with homebrew via homebrew cask. The advantage of homebrew is that it's a full-on package manager, so it not only makes it easy to install things all in one place, but also makes it easy to keep them updated. And let's face it: Unless it's really easy, there's no way any of us will possibly keep git or curl or other things that don't self-update up-to-date, and for the sake of security we usually should.

If you're just getting started with homebrew, install it by copying the terminal command in the first link above. (It runs a script from the internet, so I won't copy it here so you at least know you're getting it from the source.) Once homebrew is installed, to add cask, just run

brew tap homebrew/cask

(only once ever) in the terminal and then to install XQuartz, just

brew install xquartz

More basic, useful commands:

brew help    # see available commands
brew update    # update internal list of packages and versions (including cask)
brew search git    # search for utilities and applications with "git" in the name
brew info git    # show package version, homepage URL, dependencies, options, etc.
brew install git    # install git
brew upgrade    # upgrade all outdated command-line utilities

brew cask help    # see available commands
brew cask install rstudio-preview    # install preview version of RStudio
brew cask upgrade    # upgrade outdated applications installed with cask

brew cleanup    # delete old, unneeded files downloaded by homebrew

Since brew and brew cask are so parallel, it's pretty easy to remember how they work (or find out if you forget). Everything is pretty reasonably named, too, although there is more of a difference between update and upgrade than the names imply.

Even if the command line still makes you nervous, homebrew is a pretty gentle (and useful!) introduction, and with cask, you can install easier-to-see things, e.g. iterm2 or flycut or font-fira-code or most any non-Apple app you have on your computer. Applications' self-update features work as usual, so for frequently-updated apps like firefox, it's more just a way to install, as the app will independently keep itself up-to-date.

Maybe my favorite part of brew is that it installs everything in /usr/local/Cellar, but symlinks everything to usr/local/bin, which is on $PATH by default, so you don't have to worry about managing it or ending up confused because the new tool you installed doesn't seem to be installed. If an install is complicated, the messages it generates will tell you if you may also want to take some other action. Basically, homebrew just makes life easier.


@tbradley - The code in my post will not run the way it looks now. What I had typed was not code, in the strictest sense. There should be no underscores before and after 'username'. I had it italicized, and I meant it to be filled in by the individual user. It looks nicer now, but there seems to be an unexpected result. I'm not quite sure how to fix it as I am new to the RStudio Community. Can you or anyone else please help? Thanks.

Hi! @tbradley added special formatting characters to format your code as code. We ask people to do that as much as possible around here because it is difficult to read code that isn’t formatted like code. Here’s more on how that works:

Unfortunately, you can’t use italics inside code blocks, since special characters are all interpreted literally and other formatting (colors, etc) are applied automatically via a JavaScript syntax highlighting engine. It’s common for people to write something like /Users/your_username_here/Documents/ when they mean to say that the path should be customized before using it. Even with the user name filled in, that path wouldn’t work for the poster without further editing if they’re storing their input file somewhere other than the Documents folder (or in a folder in a folder in the Documents folder...).

It sounds like your overall advice was to try using an absolute path rather than a relative path? If the file the OP is trying to read is in their working directory, then the relative path they used should work (and will be less fragile over time). But I agree that using an absolute path can at least get the import working, while you figure out how to organize your files and your project so that you don’t have to use absolute paths anymore.


@jcblum - Thank you so much for the helpful post! I know I still have a lot left to learn, and I appreciate your clear explanation. In several less-technical guides on the Web, I have seen that they italicize 'username', so I thought I would use that convention. In any event, I thought your post was very useful and well-informed. Thanks again!

1 Like