fork download
  1. # your code goes here# Install necessary packages if not already installed
  2. # install.packages(c("reticulate", "data.table", "dplyr"))
  3.  
  4. library(reticulate)
  5. library(data.table)
  6. library(dplyr)
  7.  
  8. # 1. Ensure 'autogluon' is installed in your Python environment
  9. # You can check which python executable path is used by:
  10. # py_config()
  11. # Then, you can access the pip binary with `reticulate::py_install` like in:
  12. # reticulate::pip_install("autogluon", envname=".virtualenvs/r-reticulate")
  13. # Here we use the default env and install it.
  14.  
  15.  
  16. reticulate::py_install("autogluon")
  17.  
  18.  
  19.  
  20. # 2. Create or Load Your Data
  21. # Example: Using the Iris dataset
  22. # Use data.table to load a dataframe
  23. data("iris")
  24. dt_iris <- as.data.table(iris)
  25. setnames(dt_iris, "Species", "label")
  26.  
  27.  
  28. # Create a temporary directory to save the data as CSV
  29. temp_dir <- tempdir()
  30.  
  31. # Save the data as CSV
  32. train_file <- file.path(temp_dir, "train.csv")
  33. fwrite(dt_iris, train_file)
  34.  
  35.  
  36. # 3. Define the Python script that uses AutoGluon
  37. python_code <- '
  38. from autogluon.tabular import TabularPredictor
  39. import pandas as pd
  40. import os
  41. import sys
  42.  
  43. # Get arguments passed from R
  44. train_file = sys.argv[1]
  45.  
  46.  
  47. # Prepare data
  48. train_data = pd.read_csv(train_file)
  49.  
  50. label = "label"
  51.  
  52. predictor = TabularPredictor(label=label)
  53. predictor.fit(train_data, presets="best_quality")
  54.  
  55. # Ensure predict and leaderboards are returned
  56. predictions = predictor.predict(train_data)
  57. leaderboard = predictor.leaderboard(train_data)
  58.  
  59. print("Predictions:")
  60. print(predictions)
  61.  
  62. print("Leaderboard:")
  63. print(leaderboard)
  64.  
  65. #Save Predictor for later use
  66. predictor.save("predictor.pkl")
  67.  
  68. '
  69.  
  70. # Save the Python code to a temporary file
  71. python_file <- file.path(temp_dir, "autogluon_script.py")
  72. writeLines(python_code, con = python_file)
  73.  
  74.  
  75. # construct python argument list
  76.  
  77. python_args <- c(train_file)
  78.  
  79.  
  80.  
  81. # 4. Call the Python code from R using subprocess and reticulate
  82.  
  83. # using with so python can find correct python path.
  84. with(reticulate_python_env <- reticulate_python_exists(), {
  85.  
  86. # create bash subprocess
  87. res <- system2(command = reticulate::py_exe(),
  88. args = c(python_file,python_args),
  89. stdout = TRUE,
  90. stderr = TRUE)
  91. })
  92.  
  93. # Print output
  94. print(res)
  95.  
  96.  
  97. # 5. Load and examine results
  98. # using system2 output and manual parsing
  99. res_lines <- grep("Predictions|Leaderboard", res, value = T)
  100. # extract the predicted dataframe
  101. pred_line <- grep("Predictions", res_lines, value = T)
  102. board_line <- grep("Leaderboard", res_lines, value = T)
  103.  
  104. # parsing using string separation, can be enhanced here.
  105. # extract the values
  106. predicted_values <- gsub("Predictions:\\[|\\]","", pred_line)
  107. board_values <- gsub("Leaderboard:","", board_line)
  108. # parse results, they are saved as strings from python output
  109. # create prediction dataframe
  110. predicted_df<- as.data.frame(strsplit(predicted_values, " ")[[1]])
  111. colnames(predicted_df)<-"predicted"
  112. print("------- Predictions ------")
  113. print(predicted_df)
  114. # create model leaderboard data frame
  115. board_table <- tryCatch(
  116. {
  117. board_table <- fread(board_values) #read in parsed data
  118. },
  119. error = function(cond)
  120. {
  121. message(paste("Issue parsing leaderboard, try upgrading Autogluon: ", cond))
  122. return(NULL)
  123. }
  124. )
  125.  
  126.  
  127. if(!is.null(board_table)){
  128. print("------- Leaderboard ------")
  129. print(board_table)
  130. } else {
  131. print("Not able to parse leaderboard, check autogluon!")
  132. }
  133.  
  134. # optional: Load the predictor directly using reticulate (uncomment)
  135. #
  136. # py_run_string(paste0('import pickle; predictor = pickle.load(open("predictor.pkl", "rb"))'))
  137. # predictions<- py$predictor$predict(dt_iris[,1:4])
  138. # predictions
  139. # leaderboard<- py$predictor$leaderboard(dt_iris[,1:4])
  140. # leaderboard
  141.  
  142.  
  143. # Clean up temporary resources if needed
  144. unlink(temp_dir, recursive = TRUE)
Success #stdin #stdout #stderr 0.32s 40884KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(reticulate) : there is no package called ‘reticulate’
Execution halted