A blog by Iwan Luijks, an unofficial Microsoft evangelist talking about PHP, .NET, other programming languages and information theory.

Installing the RMySQL package on Windows 7

Posted on by in statistics

introduction

So you want to get statistical? Nowadays one of the ways to go is to use R, mostly in combination with ggplot2 for generating the plots. These plots and graphs however need some data, for that we use data sources. There are a lot of data sources available for us to use and every company and consumer has its own opinion about which one to use, which can even differ per type of usage, application or website. Therefore a lot of database adapters exist for R; one of these adapters is RMySQL, packaged as the RMySQL package.

This post describes the installation of the RMySQL package from beginning to end, assuming you have a 32-bit Windows 7 machine ready.

MySQL

First off we need a working MySQL installation. Head over to http://www.mysql.com/ and download the server of your wishes. Or use, like me, mysql-5.5.17-win32.msi. Follow it's installation instructions and make sure the checkbox next to "Client tools" is checked. I installed it to "C:\Program Files\MySQL\MySQL Server 5.5\", and when you don't, remember to change each of MySQL's paths following. After the installation press the "Windows" key and enter "System", this will open your system's settings screen. Click on "Advanced settings" and choose Environment Variables. When the screen pops up, add a new environment variable for all users and name it "MYSQL_HOME" with a value of "C:\Program Files\MySQL\MySQL Server 5.5\.

This is not all yet! Create the directory "opt" within "C:\Program Files\MySQL\MySQL Server 5.5\lib" and copy-paste "C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib" to "C:\Program Files\MySQL\MySQL Server 5.5\lib\opt\" and "C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.dll" to "C:\Program Files\R\R-2.13.2\bin\i386\".

R

After the installation of MySQL it's time to install R from http://cran.r-project.org/bin/windows/base/, the version I use is 2.13.2. Again follow the installation instructions, choosing for a "Full installation" and a customized startup at the "Startup options" screen to setup your preferred way of display, for me that was: "MDI", "HTML help" and "Internet2" while having "C:\Program Files\R\R-2.13.2" chosen as installation directory.

Now, that was easy, we already installed MySQL and R, what's next?

RTools

For the more difficult part of this post we go to http://www.murdoch-sutherland.com/Rtools/ and install the Rtools for R 2.13.x, i.e. Rtools213.exe, this allows us to build packages for R under Windows. Note that there are other packages and tools available for doing this but in my experience this was the most easy one, so we stick with this one for now.

Install Rtools to "c:\Rtools" and go for a "Custom installation" by checking "Extras to build 32 bit R: TCL/TK, bitmap code, internationalization" and at the "Select R Source Home Directory", paste "C:\Program Files\R\R-2.13.2\src" in the textfield. Click on "Next >" and choose to "Edit the system PATH" by ticking the checkbox to the left of it. The rest speaks for itself.

Rcmd.bat

Download this from http://code.google.com/p/batchfiles/source/browse/trunk/Rcmd.bat into "C:\Program Files\R\R-2.13.2\bin".

RStudio

One application very useful to have, though not necessary is RStudio, downloaded from http://rstudio.org/. With this application we have direct access to the R command line tools, a plots window and more. I recommend downloading this application not only for the purpose of this tutorial but also for upcoming daily(?) usage.

Make sure you download "RStudio 0.94.110 - Windows XP/Vista/7" and choose "C:\Program Files\RStudio" as its installation directory.

RMySQL

Now hit the Windows key and enter "RStudio", and when the program "RStudio" comes up, click it. At the console window type

install.packages('RMySQL',type='source')

and hit Enter. Choose the country most close to you as CRAN mirror for the fastest download, mine is 47.

Note: When the message:

"Warning in install.packages :
  'lib = "C:/Program Files/R/R-2.13.2/library"' is not writable"

shows up, head over to the directory and grant modification rights to the current user (or the RStudio user when installed under its own user profile) and execute the

install.packages('RMySQL',type='source')

command again.

  • R
  • RMySQL
  • Windows

Getting certified, some pro's and misconceptions

Posted on by in resume

Every now and then as developer you're looking at your resume and questioning the number of certificates on it. Whether there is only one, two, or even when there are tens of certificates listed... You still urge yourself to learn, learn and eventually maybe even get certified (again) as reward.

This is exactly the reason why it doesn't matter how many certificates are listed, ever, since you still want to keep on learning and not giving anyone the impression you're standing still doing nothing.

In the community of PHP developers there are more non-certified professionals than certified professionals. PHP developers mostly don't really see the pro's of getting certified. These developers mostly honor the fact that PHP is a quick and easy language to learn and to get started with, and professionalism is found even by building a simple dynamic web page, leaving the discussion of whether this is a good or bad thing for what it is.

Certification can enable employers having more confidence in you as developer, they notice you're doing the best you can to let them know you keep on learning, improving... A glimpse look on your certification-rich resume by HR-departments can even get you on top of the candidates list faster when job-hunting. But there are more benefits to certification, next to the fact your resume looks hotter.

As a developer you can greatly improve your skills when preparing for certification. For example, learning for the PHP 5.3 certification exam practically requires you to have knowledge on multiple topics you might not ever touched before, such as streams, phar and gc. Not because questions on those subjects will be even asked for sure on the exam, but simply the fact that there might be questions on those subjects. Most developers get triggered to learn or rethink something through the fact that they might not know something or are not sure of something.

My own opinion is that certification is nice, and may actually, in this society, really help you getting a job faster, get more respect, and even enhance the speed your learning at. But, yes, there's a but... Take a look at the number of front-end developers out there, doing amazing stuff with HTML5, JavaScript, CSS, just name it... and are they certified? Hell no. Try to name 3 great certification centers for front-end developers, quite hard ain't it?

Front-end developers are probably the most underestimated (web)developers out there, they wipe your ass when you make some back-end component screw up the layout of the site. They provide the user with some occasional spiffy search pages, a window popping out of nowhere improving the user experience, highlighting input boxes when focused. Even providing the users with a gun enabling them to shoot some ducks on the site is no exception.

I chose certification, because it's there and when something's there and can help you improve faster, why not. But don't choose certification just because you will look better, it might to some, but there's a brick wall you'll run into some day.

  • certification
  • resume
  • PHP

Using Enchant with PHP on Windows - Part 1

Posted on by in programming

introduction

PHP 5.3 introduced us to some new bundled extensions, including Enchant. The Enchant extension is a binding for the Enchant library.

Note: Before PHP 5.3, Enchant was available as optional extension through PECL. This article however only covers the setup and usage of the bundled extension in PHP 5.3

This short tutorial gives an introduction to setting up and using this new extension for your PHP projects.

What is Enchant?

Enchant is a library that provides a generic interface to third-party spell-checking APIs (Application Programming Interfaces) and ABIs (Application Binary Interfaces). And this is mainly where the power of Enchant lies in.

Normally if you want to spell-check English and French texts, you'd use the same spell-checking API (e.g. Aspell), because implementing multiple spell-checking libraries would require that you, the developer, knows how to use those specific APIs, each API bringing different methods of checking and correction. But that also means if Aspell, while very good in spell-checking English texts, isn't very strong in spell-checking French texts, while for example Ispell is strong in spell-checking French texts, you cannot bring the same spell-checking quality to the user as he should be able to.

When you use Enchant, the same programming interface can be used for different spell-checking libraries. We are now able to use Aspell for English texts and Ispell for French texts without adding specific Ispell API-calls for checking French texts.

Follow along to see how easy this can be, even on Windows!

Installing Enchant

Installing Enchant is as easy as enabling the extension in your active php.ini. Go to the line that says:

;extension=php_enchant.dll

And change it to:

extension=php_enchant.dll

Now restart your webserver to see these changes live. But that's not all, some more stuff comes to it. Read the next paragraph to find out more!

Using Enchant

As with every extension, PHP provides documentation. For Enchant the documentation can be found on http://www.php.net/enchant

Note: Not every function of the Enchant extension is currently documented. A look inside the Enchant PECL source shows some functions and constants like enchant_broker_get_dict_path which are still on the php.net documentation TODO-list all as far as I noticed.

Nonetheless, a lot of documentation about Enchant can be found at other resources. Since the first day I started programming, I gathered information from as much resources as possible, think of "How to do this in Java" while actually needing to do something in Ruby, or "How to install this in Perl" when needing documentation on installing Ada libraries. This is something that helped me in this situation too; try searching Google on Enchant, PHP and Windows or Ispell and MySpell for that matter and see how many awesome results you get /sarcasm. But lets talk about that in a minute in the next paragraph.

First I want to show you the basic usage of Enchant and how it doesn't work by default in Windows (don't know about Linux/*BSD?) Below is the example taken from php.net:

<?php
$tag = 'en_US';
$r = enchant_broker_init();
$bprovides = enchant_broker_describe($r);
echo "Current broker provides the following backend(s):\n";
print_r($bprovides);

$dicts = enchant_broker_list_dicts($r);
print_r($dicts);
if (enchant_broker_dict_exists($r,$tag)) {
	$d = enchant_broker_request_dict($r, $tag);
	$dprovides = enchant_dict_describe($d);
	echo "dictionary $tag provides:\n";
	$wordcorrect = enchant_dict_check($d, "soong");
	print_r($dprovides);
	if (!$wordcorrect) {
		$suggs = enchant_dict_suggest($d, "soong");
		echo "Suggestions for 'soong':";
		print_r($suggs);
	}
	enchant_broker_free_dict($d);
} else {
}
enchant_broker_free($r);
?>

By default PHP will not go beyond the line saying

if (enchant_broker_dict_exists($r,$tag)) {

We will have to install some dictionaries first.

Installing dictionaries

As I already mentioned above, before you can actually use Enchant's spelling suggestions and other material you have to install some dictionaries. Funny enough, no Windows documentation exists for this yet and even the Linux/*BSD part isn't documented at php.net.

Fortunatly, there are some other resources we can gain information from. First on, I'll show you what I tried before.

In the last paragraph I said not all Enchant's functions were documented at php.net yet, so I looked at the source of the extension, and found a function I thought it'll help me, named enchant_broker_get_dict_path. The function signature for that function is:

string enchant_broker_get_dict_path(resource broker, int dict_type)

This function requires 2 parameters, first the broker resource (created using enchant_broker_init()) and the dict_type, a non-documented constant value. There are 2 possible values for dict_type as indicated by the source, the first one is ENCHANT_ISPELL and the second ENCHANT_MYSPELL, the 2 default included spell-checking providers.

Calling this function however triggered a 500 error on my Apache and crashed my CLI instance of PHP (bug?).

This function already triggerd my brain, as how does it actually know where my dictionaries are installed in the first place? A lot of searches pointed me to this post on Google Groups talking about pyenchant (remember my note on this before?)

So there is also a function allowing me to set this path! Browsing the source again, I stumbled upon enchant_broker_set_dict_path:

bool enchant_broker_set_dict_path(resource broker, int dict_type, string value)

Calling this function above enchant_broker_get_dict_path, fixed the crashing/500 error I got before and actually allowed me to set my dictionary paths:

enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, 'C:\PHP\enchant\MySpell');
enchant_broker_set_dict_path($broker, ENCHANT_ISPELL, 'C:\PHP\enchant\Ispell');
Note: these directories do not exist by default, so you have to create them, but you can create them anywhere since it doesn't matter where you actually store these dictionaries as long as PHP can access them.

Now we can actually install the dictionaries. Dictionaries can be found at many places, I used the Slovak dictionary found on the Mozilla Wiki's L10n:Dictionaries page.. Extract the downloaded zip-file to your dictionary path (so you get something like C:\PHP\enchant\MySpell\sk_SK.aff and C:\PHP\enchant\MySpell\sk_SK.dic) and refresh your page. As you can see, the dictionary you extracted is now ready to use.

A working example!

Since I already mentioned in the previous paragraph I installed the Slovak (sk_SK) dictionary for the MySpell provider in C:\PHP\enchant\MySpell\, I will just show you the code I use to test all of this; the code I used to get a spelling suggestion for the word "soong" (the word the PHP manual also uses in its example):

<?php
$broker = enchant_broker_init();
$tag = 'sk_SK';

enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, 'C:\PHP\enchant\MySpell');

if (enchant_broker_dict_exists($broker, $tag)) {
	$dict = enchant_broker_request_dict($broker, $tag);
	$word = 'soong';
	$isCorrectlySpelled = enchant_dict_check($dict, $word);

	if ($isCorrectlySpelled !== true) {
		$suggestions = enchant_dict_suggest($dict, $word);

		echo nl2br(print_r($suggestions, true));
	} else {
		echo 'The word is correctly spelled!';
	}
}

enchant_broker_free($broker);
?>

If you followed all my instructions above the following should be on your screen right now:

Array
(
	[0] => song
)

conclusion

It took me a lot of time figuring out how this all worked and I hope I helped anyone reading this article looking for some information on how to set all this up. In the next part I'll go up and do some more funky stuff using Enchant. Stay tuned!

  • PHP
  • Enchant
  • Windows