Open In App

p5.js loadTable() Function

The loadTable() function is used to read the contents of a file or URL and create a p5.Table object from it. The options parameter can be used to define the type of format that the data is expected to be read. All the files loaded and saved are in UTF-8 encoding.

This function is asynchronous, therefore it is recommended to be called in the preload() function to ensure that the function is executed before the other functions.



Syntax:

loadTable(filename, options, [callback], [errorCallback])

or



loadTable(filename, [callback], [errorCallback])

Parameters: This function accepts four parameter as mentioned above and described below.

Below examples illustrate the loadTable() function in p5.js:

Example 1:




// Contents of books.csv
  
// Book One, Author One, Price One
// Book Two, Author Two, Price Two
// Book Three, Author Three, Price Three
  
let loadedTable = null;
  
function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  text("Click on the button below to"+
       " load Table from file", 20, 20);
  
  // Create a button for loading the table
  loadBtn = createButton("Load Table from file");
  loadBtn.position(30, 50)
  loadBtn.mousePressed(loadFile);
}
  
function loadFile() {
  
  // Load the table from file
  loadedTable = loadTable('books.csv', 'csv', onFileload);
}
  
function onFileload() {
  text("Table loaded successfully...", 20, 100);
  
  // Display through the table
  for (let r = 0; r < loadedTable.getRowCount(); r++) {
    for (let c = 0; c < loadedTable.getColumnCount(); c++) {
      text(loadedTable.getString(r, c), 
            20 + c * 200, 150 + r * 20);
    }
  }
}

Output:

Example 2:




// Contents of books_header.csv
  
// title, author, price
// Book One, Author One, Price One
// Book Two, Author Two, Price Two
// Book Three, Author Three, Price Three
  
let loadedTable = null;
  
function setup() {
  createCanvas(500, 300);
  textSize(22);
  
  text("Click on the button below to "
       + "load Table from file", 20, 20);
  
  // Create a button for loading the table
  loadBtn = createButton("Load Table from file");
  loadBtn.position(30, 50)
  loadBtn.mousePressed(loadFile);
}
  
function loadFile() {
  // Load the table from file with headers
  loadedTable = loadTable('books_header.csv',
                'csv', 'header', onFileload);
}
  
function onFileload() {
  text("Table loaded successfully...", 20, 100);
  
  // Display the headers
  for (let h = 0; h < loadedTable.getColumnCount(); h++) {
    text(loadedTable.columns[h], 20 + h * 200, 150);
  }
  
  textSize(16);
  // Display the data in the table
  for (let r = 0; r < loadedTable.getRowCount(); r++) {
    for (let c = 0; c < loadedTable.getColumnCount(); c++) {
      text(loadedTable.getString(r, c), 
            20 + c * 200, 170 + r * 20);
    }
  }
}

Output:

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/amp/

Reference: https://p5js.org/reference/#/p5/loadTable


Article Tags :