Skip to content

Instantly share code, notes, and snippets.

@ngoet
Last active January 20, 2019 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngoet/c61435adf8997815b60394f637c4e740 to your computer and use it in GitHub Desktop.
Save ngoet/c61435adf8997815b60394f637c4e740 to your computer and use it in GitHub Desktop.
Combining R and Python
# set up log file
logfile_name <- gsub('-| |:','_',paste('logfile_',Sys.time(),'.log',sep=''))
log_file <- file(logfile_name)
# function
execute_script <- function(print_message,script_name,type="R",input_params=NULL,numCores=NULL){
message(print_message)
log_file <- file(logfile_name, open = 'a')
cat(paste('\n\nExecuting ',script_name,' script (start time: ',Sys.time(),')',sep=''), file = log_file,append = TRUE)
close(log_file)
if(type=="R"){
execution_time <- round(system.time({source(script_name)})[3]/60,digits=2)
}else{
execution_time <- round(system.time({system(paste('python3',script_name,input_params,sep=' '))})[3]/60,digits=2)
}
log_file <- file(logfile_name, open = 'a')
cat(paste('\n',script_name, ' script successfully run. Execution time: ',execution_time," minutes",sep=''), file = log_file,append = TRUE)
close(log_file)
message(paste('\n',script_name, ' script successfully run. Execution time: ',execution_time," minutes",sep=''))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment