Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/src/main/r/0-getting-started.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have the Apache License at the top of every file. You can see https://github.com/apache/spark/blob/master/examples/src/main/r/dataframe.R#L1 for an example

Also per our style guide we don't put in Author names / dates in the file itself as this is tracked in the commit log

# Author: Daniel Emaasit (@emaasit)
# Purpose: This script shows how to install SparkR onto your workstation/PC
# and initialize a spark context and a SparkSQL context
# Date: 06/05/2015
#


# Install SparkR from CRAN
install.packages("SparkR")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of these example files we will be assuming the user has installed Spark through some method (say from http://spark.apache.org/downloads.html). So we can just include the package as library(SparkR) and avoid the installation steps.


## OR Install the dev version from Github
install.packages(devtools)
devtools::install_github("amplab-extras/SparkR-pkg", subdir="pkg")

# Load SparkR onto your PC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should probably be 'Load SparkR library into your R session'

library(SparkR)

## Initialize SparkContext on your local PC
sc <- sparkR.init(master = "local", appName = "MyApp")

## Initialize SQLContext
sqlCtx <- SparkRSQL.init(sc)
30 changes: 30 additions & 0 deletions examples/src/main/r/1-data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Author: Daniel Emaasit (@emaasit)
# Purpose: This script shows how to create Spark DataFrames
# Date: 06/05/2015
#

# For this example, we shall use the "flights" dataset
# The data can be downloaded from: https://s3-us-west-2.amazonaws.com/sparkr-data/flights.csv
# The dataset consists of every flight departing Houston in 2011.
# The data set is made up of 227,496 rows x 14 columns.

source("0-getting-started.R")

# Create an R data frame and then convert it to a SparkR DataFrame -------

## Create R dataframe
install.packages("data.table") #We want to use the fread() function to read the dataset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the spark-csv reader for this ? That way users will learn the way to read in large CSV files. I've done something similar in this gist here https://gist.github.com/shivaram/d0cd4aa5c4381edd6f85#file-dataframe_example-r-L6

library(data.table)

flights_df <- fread("flights.csv")
flights_df$date <- as.Date(flights_df$date)

## Convert the local data frame into a SparkR DataFrame
flightsDF <- createDataFrame(sqlCtx, flights_df)

## Print the schema of this Spark DataFrame
printSchema(flightsDF)

## Cache the DataFrame
cache(flightsDF)
51 changes: 51 additions & 0 deletions examples/src/main/r/2-data-manipulation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Author: Daniel Emaasit (@emaasit)
# Purpose: This script shows how to explore and manipulate Spark DataFrames
# Date: 06/05/2015
#

source("1-data.R")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the source will work correctly given the way we encourage users to run example programs. If you see the Running examples section at http://people.apache.org/~pwendell/spark-releases/latest/index.html you can see that we ask users to run example programs with something like ./bin/spark-submit examples/src/main/r/data-manipulation.R.

So I'd just recommend merging all the code into one file data-manipulation.R and then take in the CSV file name as a command line argument.



# Install the magrittr pipeline operator
install.packages("magrittr")
library(magrittr)

# Print the first 6 rows of the DataFrame
showDF(flightsDF, numRows = 6) ## Or
head(flightsDF)

# Show the column names in the DataFrame
columns(flightsDF)

# Show the number of rows in the DataFrame
count(flightsDF)

# Show summary statistics for numeric colums
Describe(flightsDF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be describe and not Describe ?


# Select specific columns
destDF <- select(flightsDF, "dest", "cancelled")

# Using SQL to select columns of data
# First, register the flights DataFrame as a table
registerTempTable(flightsDF, "flightsTable")
destDF <- sql(sqlCtx, "SELECT dest, cancelled FROM flightsTable")

# Use collect to create a local R data frame
dest_df <- collect(destDF)

# Print the newly created local data frame
print(dest_df)

# Filter flights whose destination is JFK
jfkDF <- filter(flightsDF, "dest == JFK") ##OR
jfkDF <- filter(flightsDF, flightsDF$dest == JFK)

# Group the flights by date and then find the average daily delay
# Write the result into a DataFrame
groupBy(flightsDF, "date") %>%
avg(dep_delay = "avg", arr_delay = "avg") -> dailyDelayDF

# Stop the SparkContext now
sparkR.stop()