### This file describes a script for testing the connection. ### $Revision: 1.3 $ ### $Date: 2005-06-30 17:01:58 $ ###Parameters: ### Set things up #library("RMySQL") ### Note: On windows, might need to ## source("C:/Program Files/R/rw1091/library/RMySQL/R/RMySQL.R") ###Connect to database ### Sample Script ##Do not Run #asp.dbConnect(user="ralmond",dbname="MCMC") #asp.repairAllTables() ###Check status of chains #getCycles() ### You will want to run this next part manually. ### Adjust the chain a couple of cycles back to be sure all is ### cleanly written. For example, if # > getCycles() # chain_first_cycle chain_last_cycle # 1 1 3000 # 2 1 1270 # 3 1 -1 ## Then you might use: ##asp.trimTables(1268,chain=2) ## Again for sanity check. #getCycles() ### This function runs repair tables on all tables in a database. ### It should do no harm. "asp.repairAllTables" <- function () { tables <- dbListTables(asp.con) for (table in tables) { rquery <- paste("REPAIR TABLE ",table,";") print(dbGetQuery(asp.con,rquery)) } } ### This function deletes all records for cycles greater than cycle in ### in chain. Recommended best practice is to delete the last two ### cycles if an MCMC run crashes in the middle. getCycles() ### can help you determine where the calibrator was during the crash. ### WARNING: This function will destructively modify the database. "asp.trimTables" <- function (cycle, chain) { models <- listModels() fixTable <- function (table,cycle,chain) { fquery <- paste("DELETE FROM ",table, " WHERE CYCLE >= ",cycle, " AND CHAIN = ",chain,";") print(fquery) dbGetQuery(asp.con,fquery) } for (model in models) { fixTable(parameterTable(model),cycle,chain) fixTable(variableTable(model),cycle,chain) } fixTable("Statistics",cycle,chain) ## Now fix chain_data_table; cquery <- paste("UPDATE chain_data_table", " SET chain_last_cycle =",cycle-1, " WHERE CHAIN =",chain,";") print(cquery) dbGetQuery(asp.con,cquery) }