Here is how you pull SQL data into R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require( sqlutils ) | |
require( RMySQL ) | |
library( Cairo ) | |
library( yaml ) | |
my.cnf = yaml.load_file( '~/.my.yaml' ) | |
default = my.cnf$clients$default | |
p_cols <- c( "blue" , "red" , "orange" , "green" ) | |
con <- dbConnect( | |
MySQL(), | |
user=default$user , | |
password=default$password, | |
dbname=default$database, | |
host=default$host | |
) | |
rs <- dbSendQuery( | |
con , | |
' | |
SELECT TIME_FORMAT( time , "%l %p" ) time | |
, AVG( temp_f ) temperature | |
FROM weather | |
WHERE HOUR( TIMEDIFF( SYSDATE() , TIME ) ) < 24 | |
GROUP BY HOUR( time ) | |
ORDER BY HOUR( TIMEDIFF( SYSDATE() , TIME ) ) | |
DESC; | |
' | |
) | |
fields = dbColumnInfo(rs) | |
data_frame = fetch(rs, n = 24 ) | |
time = data_frame[[ "time" ]] | |
temp = data_frame[[ "temperature" ]] | |
CairoPNG( | |
filename="/path/to/where/i/write/test.png" , | |
width = 500 , | |
height = 500 , | |
pointsize = 9 | |
) | |
plot ( temp | |
, main="Temperatures for the last 24 Hours" | |
, sub="West Lafayette, IN - Temperatures in Fahrenheit" | |
, col=p_cols[2] | |
, type="l" | |
, xlab="Time" | |
, ylab="Temperature(F)" | |
, xaxt='n' | |
) | |
axis( 1 , las=2 , at=1:24 , lab=time ) | |
box() |
Cool, isn't it?
Except there's a password in plain text. Which means I have to modify it when I want to show it off.
I'll have to work on that.
Except there's a password in plain text. Which means I have to modify it when I want to show it off.
I'll have to work on that.