Monthly Archives: March 2018

Module 11 – Debugging and defensive programming in R

Our assignment this week was to debug a block of code.

The original code was:

tukey_multiple <- function(x) {
   outliers <- array(TRUE,dim=dim(x))
   for (j in 1:ncol(x))
    {
    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])
    }
outlier.vec <- vector(length=nrow(x))
    for (i in 1:nrow(x))
    { outlier.vec[i] <- all(outliers[i,]) } return(outlier.vec) }

To start debugging, I pasted the code into RStudio to see what kind of error it would throw. I got:

Error: unexpected symbol in:
"    for (i in 1:nrow(x))
    { outlier.vec[i] <- all(outliers[i,]) } return"

The “unexpected symbol” error made me think that some brackets or parentheses weren’t closed or were misplaced. That wasn’t it, but it turned out to be something similar – looking at the last line, I noticed that the “return” looked out of place. It should have been on a separate line, by itself. That would certainly make it an unexpected symbol. I moved return to the next line, making the code look like:

tukey_multiple <- function(x) {
  outliers <- array(TRUE,dim=dim(x))
  for (j in 1:ncol(x))
  {
    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])
  }
  outlier.vec <- vector(length=nrow(x))
  for (i in 1:nrow(x))
  { outlier.vec[i] <- all(outliers[i,]) }
  return(outlier.vec) }

At that point, the code ran successfully and the function was created.

Using the function was a different story – I passed it a built-in list as an argument, and got a new error:

Error in tukey.outlier(x[, j]) : could not find function "tukey.outlier"

I’m guessing that tukey.outlier() is a function that returns TRUE or FALSE depending on whether the value is an outlier. The code runs fine without that function (albeit it makes tukey.multiple() always return a bunch of TRUEs), so I assume this code will work provided tukey.outlier() is defined.

Posted in R Programming | Comments Off

Module 10 – Building an R package

This week’s assignment was to create a DESCRIPTION file for our final project package.

The DESCRIPTION file I created is on GitHub here:

https://github.com/jered0/lis4930-rpackage/blob/master/queryNIBRS/DESCRIPTION

Getting the DESCRIPTION format right was interesting, and I ended up looking at a few packages on CRAN to see what package names typically look like and how they describe themselves. For example, I thought at first that the Title field was just an expanded name for the package, but I see that it does, as you said in the module, act more as a descriptive subtitle.

Because my package will involve retrieving data from the FBI’s Crime Data API, I added an Imports section for the dependency the package will need, RCurl. I’ll look into whether that’s strictly required – I might be able to use R’s built-in web queries instead of relying on RCurl’s fancy capabilities.

Posted in R Programming | Comments Off

Module 8 – PHP Strings and Web Design documentation

This week we covered PHP strings and documenting web sites.

Web Programming Section

The assignment was to create a string with PHP code. My web page is located here:

http://jered.0fees.us/module8/index.php

I created some strings using both single quotes and double quotes, then output them as one sentence using concatenation. In the process of concatenating the strings I used str_replace() to replace a word, for the sake of including a string function.

The relevant code is:

$one_string = "<p>This is a waffle that's been";
$two_string = 'concatenated with PHP.</p>';
$output_string = str_replace("waffle", "sentence", $one_string)." $two_string";
echo $output_string;

Strings are values that can be manipulated with the instructions in functions. There are a number of functions associated with strings – that makes sense, given that strings comprise most of the information that will be communicated to a user from a web page or program.

Web Design Section

For this assignment we are to document the web site we’ll be creating for our final project.

Step 1. Define key stakeholders’ goals

The stakeholder is the farmer the site is created for. He wants to display his wares (vegetables), provide information about them to site visitors, and give those visitors the opportunity to purchase his vegetables.

Step 2. Identify your users’ goals and expectations

The users are site visitors who are interested in purchasing vegetables online. They’ll expect a site that lets them readily find information about what vegetables are for sale and purchase them easily.

Step 3. Define your site’s content areas

Pending a discussion with my eventual partner, I expect the site would be divided into a landing page, a main page to list the vegetables, pages for each vegetable on offer (turnips, radishes, and spinach), a page to check order status, and a page with information about the farmer.

Step 4. Organize the content areas

The landing page would be the first site in the hierarchy, and would link directly to the vegetable page and to the farmer information page. The vegetable page would in turn link to individual pages for each vegetable.

Posted in Web Design Technologies | Comments Off

This week the assignment was to plot graphs for a data set using a built-in R function, a function from the lattice package, and a function from the ggplot2 package.

The source code I created for the task can be found on GitHub here:

https://github.com/jered0/lis4930-rpackage/blob/master/module9/lattice-ggplotting.r

I used the SeaSlugs data set. A description of the set is on this page, and the data set itself can be downloaded via this link.

Given that there were two categories of data (time and metamorphose percentage), and there were multiple percentages for each time entry, I decided to represent the data with a boxplot.

Built-in function

I used the built-in boxplot() function to create the first graph.

Graph created with the built-in boxplot() function in R
Built-in boxplot() function

I’ve used this function before, but I did have to fiddle with it to work out how to get the y-axis labels horizontal (I thought it looked better that way), and to apply colors from the rainbow() function. It’s a simple graph, nothing fancy.

Lattice function

I used the bwplot() function from the lattice package to make the next box plot graph.

Graph created with the bwplot() function from the lattice package
The lattice package's bwplot() function

The syntax is pretty close to the built-in function, which made it easier to work with than I expected. I had to tweak settings to get it to use vertical boxes, and had to find the parameter that would set the colors. Unfortunately I didn’t find an option to draw a line instead of use a plot to mark the median, to match the appearance of the other graphs.

ggplot2 function

For the final graph, I used the ggplot2 package’s ggplot(), geom_boxplot(), labs(), and theme_classic() functions in concert.

Graph created with the ggplot2 package
The ggplot2 package's geom_boxplot() function

It took some time to get the syntax and everything sorted out, since it uses a different syntax from the other two functions. I also had to convert the Time column to factors – without that change, all the percentage data and time data were collected into one giant boxplot. Definitely not what I wanted.

The ggplot2 graph came out looking the best, I think, and next time there would be less fuss given that I’m more familiar with the package.

Posted in R Programming | Comments Off

Module 7 – PHP Functions and Rollover Images

This week we covered PHP functions and rollover images. My web page for the assignment is here:

http://jered.0fees.us/module7/index.php

Web programming section

I used a simple function and if/else block for the web code. The syntax was fairly similar to other languages.

One thing that struck me right off the bat was that an error in PHP code could cause the page to fail to load – omitting a semicolon yielded a 500 server error. JavaScript is more forgiving, just skipping that code instead of blocking the whole page.

Another difference is the way variable scope is handled. As I recall, JavaScript will use a variable from outside a function simply by referencing it, while in PHP you have to explicitly import a global variable into a function.

The if statement seems to work similarly in both languages. Function definitions are also similar between the two, with both languages being loosely typed and not defining return types.

Web design section

For the web design portion, we needed to add an image to a page that would change when someone hovered the cursor over it. I went looking for a way to do it with CSS, using :hover, and that started looking a bit complicated. Then I came across some simple JavaScript to do the same thing (using onmouseover and onmouseout to change the image displayed via this.src). Then I kicked myself for not thinking to check JavaScript first, and went with that approach. The HTML is much easier to read with the JavaScript approach.

Posted in Web Design Technologies | Comments Off

Module 8 – Input/Output, string manipulation and plyr package

This module’s assignment was to read data from a file, alter or filter that data, then write the new data to new files.

The code I used is on my GitHub repository here:

https://github.com/jered0/lis4930-rpackage/blob/master/module8/studentsorting.r

I was generally familiar with the concepts involved in reading from files thanks to other coding languages, and the format of read.table is pretty intuitive, so I had no problems there. The file.chooser() function was easy to use, and being able to read the data in all at once without looping through lines one at a time was nice.

It did take me a bit to wrap my head around the ddply function, but running it a few times and tweaking the parameters cleared it up. Being able to fiddle with a data set and pick it apart like that definitely seems like it would come in handy.

I’ve used the grep command on Unix often, so using grepl() was straightforward – though I did have to look over the documentation to see how grep() and grepl() differ, and why grepl needed to be used with subset().

Posted in R Programming | Comments Off

Module 6 – Introduction to PHP and CSS

Web Programming Section

This week the first assignment was to create a PHP page and upload it to my web space. The URL of the page is:

http://jered.0fees.us/module6/index.php

Next we were presented with a list of questions and directed to choose the correct answers. My questions and answers are:

Question # 1
A. PHP server scripts are surrounded by delimiters, which one?
A3. <?php…?>

Question # 2
B. How do you write “Hello World” in PHP
B1. echo “Hello World”;

Question # 3
C. All variables in PHP start with which symbol?
C2. $

Question # 4
D. What is the correct way to end a PHP statement?
D1. ;

Web Design Section

This week we were asked to create a CSS style sheet. I created mine at the following URL:

http://jered.0fees.us/module6/module6style.css

Cascading style sheets are useful in a number of ways. For one, it separates style from content – the main section of a web page can consist primarily of content to be presented to the user, making it easier to read in HTML form. Using CSS also provides stylistic consistency, as several pages can import style information from a single CSS file. On top of that, website design changes become easier to make because you can make a style change in a single file, then every HTML page on the site that uses that stylesheet will automatically use the new style. Pages styled with CSS will usually load faster than pages formatted with frames and tables. And because CSS can be easily overridden by a web browser, using CSS aid accessibility by allowing users to use larger font sizes or more dyslexia-friendly fonts.

The following are my questions and answers for the web design part of the week’s assignment:

Question # 1
A. What is the correct CSS syntax for making all the elements bold?
A2. span {font-weight:bold}

Question # 2
B. What property is used to change the text color of an element?
B3. color:

Question # 3
C. The # symbol specifies that the selector is?
C4. id

Posted in Web Design Technologies | Comments Off