Category Archives: Things I Forget

Things I Forget: Push/Pull Greyed out in RStudio?!?

So, on more than one occasion I have set up a repository locally, then on GitHub and pushed to that repo from the shell. This works great, but this would always result in the Push and Pull buttons in RStudio getting greyed out. I could push just fine from the shell, but not from the GUI. Not a big problem, but always kind of annoyed me.

Today I took a bit of time to search for a solution and found my answer pretty quickly over on RStudio support.  All I had to do was simply push from the shell with the -u flag. This flag added an upstream reference.

Prior to fixing it my config looked like:

user.name=Jeff Hollister
user.email=jeff.w.hollister@gmail.com
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
remote.origin.url=https://github.com/jhollist/hkm.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

And after using git push -u origin master, my config had these two lines added at the bottom:

branch.master.remote=origin
branch.master.merge=refs/heads/master

I restarted RStudio and can now push and pull from the GUI.  Yeah!

Lastly, this really isn't something I have forgotten yet, I just looked it up. But, it does seem like something I would forget. I am just being proactive here.

Rstudio and makefiles: Mind your options!

I have written this post mostly for myself. I don’t want to waste 2 hours on this problem again at some point in the future.  Hopefully others might stumble on it too and save some aggravation.

So, the issue I had this morning was writing up a a makefile in RStudio.  I am new to make and makefiles, but have been able to get them running successfully in the past; however, the makefiles I’ve used were mostly borrowed from others and only minor edits were made.  This morning I was trying to add a new target and some dependencies.  As most of my work these days is with R, I prefer to do most of my writing and editing directly in RStudio.  Thus, I fire up RStudio, penned some great prose in R Markdown, and then went to add a new target to a makefile that was to take my R Markdown file, and create an output .pdf and .docx.  Seemed simple enough.

Here’s the makefile like the one I created:

all: file.md file.pdf file.docx

file.md: file.Rmd
  Rscript -e "library(knitr); knit('file.Rmd')"

file.pdf: file.md
  pandoc -H format.sty -V fontsize=12pt file.md -o file.pdf

file.docx: file.md
  pandoc -s -S --reference-docx=format.docx file.md -o file.docx

Looks pretty good!  Or so I thought.  I sauntered on over to my terminal window, and confidently type make and, it runs the first target fine, and then stops... Next step for me was Google and I was rewarded with A Brief Intro to Makefiles. Ah ha! My problem is the tabs. I then head back to RStudio, dutifully replace what I thought were spaces with tabs, run make. Same problem.

This next step took me a while of trying the same thing over and over again, but expecting something different. Say what you will about me. After this brief foray into insanity I then realized that I had my options in RStudio set to replace tabs with spaces. Again, I think I have the problem licked. In RStudio, I go to Tools:Global Options:Code Editing and click off my "Insert spaces for tab" option. That looks like:
GlobalOptions

After making this change, I once again replace my spaces with tabs, try again, and still no good.  At this point I am sure I know the problem is related to the spaces, but for the life of me I can't figure out why RStudio tabs are still getting input as spaces.  I give up and copy/paste everything into notepad++, reset my tab options there and go about replacing the tabs.  This time it works.

While I got my makefile working, it was a very unsatisfying solution.  I kept digging and am pleased to report RStudio is not to blame (I never thought it was. User error all along).  As is turns out there is an additional Rstudio Project level option that also controls the tabs.  I have now checked off Tools:Project Options: Code Editing: Insert spaces for tab. That looks like:
ProjectOptions

At long last my tabs are tabs and my makefile makes and it can all be done in RStudio, provided I take care of my options.

Things I Forget: Reading a Shapefile in R with readOGR

One of the more common ways that I read vector data into R is via shapefiles.  I tend to use these partly becuase of my own sordid past with Arc/INFO, ArcView and ArcGIS and partly due to their ubiquity.  In any event I have found the R package, rgdal, indespensible for this.  One of the workhorse functions for pulling in vector data is readOGR().  It has two required parameters dsn and layer. The part I never remember is how these relate to shapefiles. There is nothing especially tricky about it, I just tend to forget what the dsn is and what the layer is. In short the dsn is the directory (without a trailing backslash) and the layer is the shapefile name without the .shp.

So, here's the actual code so I don't have to look it up again.

If the shapefile you are reading is in your current working directory the dsn refers simply to that directory. So all you need is simply a ".". The layer is the name of shapefile without an extension. So it would look something like:

myShapeInR<-readOGR(".","myShapeFile")

Now, if that file resides elsewhere, the trick is to remember what the heck dsn refers to. Again it is simply the directory where the shapefile resides. So if I had a shapefile in a place like C:/data I would use the command like:

myShapeInR<-readOGR("C:/data","myShapeFile")

There, it is really quite simple, yet I always mess it up. Not any more.

I hope.

Things I Forget: Installing new python packages on Windows

I can never remember how to do simple things, be it in Python, R, or Java (hope I don’t have to do too much more work in Java…).   Given that, I plan to post these kind of things, when I remember of course, under the title of “Things I Forget”.  My primary aim for this is to help myself, but I do hope others with a similar memory affliction will also find these useful.

For this first installment: How do you add new python packages from a tarball (i.e. .tgz or .tar.gz file) on Windows?

The Steps:

  1. Hope there is a windows installer.  Given how unlikely this appears to be, move to step 2
  2. Download the tarball.  This is the thing you always see when you are expecting to find the windows installer.  It has some name followed by a .tar.gz or a .tgz.  For instance, if you want to add the ability to write out Excel files (by the way,  I am not advocating for Excel), then you could use the xlwt package.   I usually download these files directly into my into my site-packages directory (C:/Your Python Path/Lib/site-packages).
  3. Once you download the taball you need to find someway to extract the file.  On linux machines this is easy.  I had a bit more of challenge getting it to work on a windows machine.  There are a few options.  I have used PowerArchiver before, but it costs.  Cygwin can do it, but that may be overkill.  The one I have used most recently is the very simple TarTool.  It is free and doesn’t need to be installed, only extracted.  I also keep the TarTool.exe in my site-packages directory.  So, now that I have my tool to extract the file and the file itself, I simply do the following (click for full size):
    tartoolDOS
  4. Now you will have a directory for the package, so to install it is simply a matter of changing into that directory and running the setup.  Just like this (click for full size):
    setupPythonPackage
  5. Your python package is now ready to roll and can be imported (click for full size)!
    testPythonPackage

As an aside, anyone know of a wordpress trick to display a DOS command prompt?  Inserting a screen shot was the best I came up with, but it would be nice to have something akin to the method for highlighting sourcecode.