About
In this notebook, the functions used in the method used for building a model of sugar beet clamp temperature are described. The following custom functions included:
sql_import
:
next
Method
Data import
sql_import
This function connects to a SQL database to import the required data. Currently, the function does not have any input arguments as the joins and selections are static within this project. The database is local, uses PostgreSQL, and is built from a PostgreSQL script (built in ATOM) and csv files in the directories /650.1-Clamp_statistical_model and /650.1-Clamp_statistical_model/Data/sql_csv/yrs respectively.
sql_import <- function(){
con <- dbConnect(drv=PostgreSQL(),
user="postgres",
password="password",
host="localhost",
port=5432,
dbname="nbr_trials")
#query the database and store the data in dataframe
tb_data <- dbGetQuery(con, "SELECT * FROM data
INNER JOIN metrics ON data.metric_id = metrics.id
INNER JOIN plots ON data.plot_id = plots.yr_series_trial_plot
INNER JOIN treatments ON plots.treatment_1_id = treatments.id
WHERE (metric_id = 1 OR metric_id = 2 OR metric_id = 3 OR metric_id = 4 OR metric_id = 8
OR metric_id = 9 OR metric_id = 10 OR metric_id = 11 OR metric_id = 12)
AND (treatment_1_id = 1 OR treatment_1_id = 4 OR treatment_1_id = 7);")
dbDisconnect(con) #disconnect from database
}
LS0tDQp0aXRsZTogIkEgZGF0YSBwaXBlbGluZSBmb3Igc3VnYXIgYmVldCBjbGFtcCB0ZW1wZXJhdHVyZSBtb2RlbGxpbmcgLSBGdW5jdGlvbnMiDQpvdXRwdXQ6DQogIGh0bWxfbm90ZWJvb2s6DQogICAgbnVtYmVyX3NlY3Rpb25zOiB5ZXMNCiAgaHRtbF9kb2N1bWVudDoNCiAgICBkZl9wcmludDogcGFnZWQNCi0tLQ0KDQojIEFib3V0DQoNCkluIHRoaXMgbm90ZWJvb2ssIHRoZSBmdW5jdGlvbnMgdXNlZCBpbiB0aGUgbWV0aG9kIHVzZWQgZm9yIGJ1aWxkaW5nIGEgbW9kZWwgb2Ygc3VnYXIgYmVldCBjbGFtcCB0ZW1wZXJhdHVyZSBhcmUgZGVzY3JpYmVkLiBUaGUgZm9sbG93aW5nIGN1c3RvbSBmdW5jdGlvbnMgaW5jbHVkZWQ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