Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"publisher": "atsyplenkov",
"license": "MIT",
"pricing": "Free",
"sponsor": {
"url": "https://github.com/sponsors/atsyplenkov"
},
"icon": "assets/logo.png",
"repository": {
"type": "git",
Expand Down Expand Up @@ -106,6 +103,11 @@
"default": true,
"markdownDescription": "Show the `Pastum: paste as default dataframe` command in the editor context menu *(i.e., right-click menu)*. It will only appear in R, Python, and Julia editors. The dataframe will be pasted according to the specified `pastum.defaultDataframeR` and `pastum.defaultDataframePy`."
},
"pastum.airFormat": {
"type": "boolean",
"default": true,
"markdownDescription": "Add comment to skip air formatting in R. (i.e., `# fmt:skip`) before the pasted dataframe."
},
"pastum.defaultDataframeR": {
"type": "string",
"enum": [
Expand Down
36 changes: 18 additions & 18 deletions src/paste-r.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ async function clipboardToRDataFrame(framework = null) {
*/
function createRDataFrame(tableData, framework) {
const { headers, data, columnTypes } = tableData;
let code = "";
const config = vscode.workspace.getConfiguration("pastum");
const airFormat = config.get("airFormat");

let code = airFormat ? "# fmt:skip\n" : "";
// let code = "";

/**
* Formats a value according to its column type for R syntax
Expand Down Expand Up @@ -118,26 +122,24 @@ function createRDataFrame(tableData, framework) {

// Generate code based on selected framework
if (framework === "base") {
code = `data.frame(\n`;
code += `data.frame(\n`;
headers.forEach((header, i) => {
const values = data.map((row) => formatValue(row[i], i)).join(", ");
code += ` ${header} = c(${values})${
i < headers.length - 1 ? ",\n" : "\n"
}`;
code += ` ${header} = c(${values})${i < headers.length - 1 ? ",\n" : "\n"
}`;
});
code += `)`;
} else if (framework === "tibble") {
code = `tibble::tibble(\n`;
code += `tibble::tibble(\n`;
headers.forEach((header, i) => {
const values = data.map((row) => formatValue(row[i], i)).join(", ");
code += ` ${header} = c(${values})${
i < headers.length - 1 ? ",\n" : "\n"
}`;
code += ` ${header} = c(${values})${i < headers.length - 1 ? ",\n" : "\n"
}`;
});
code += `)`;
} else if (framework === "tribble") {
const colWidths = calculateColumnWidths();
code = `tibble::tribble(\n`;
code += `tibble::tribble(\n`;

// Column headers with padding
code +=
Expand All @@ -162,21 +164,19 @@ function createRDataFrame(tableData, framework) {
// Remove trailing comma and close parentheses
code = code.trimEnd().slice(0, -1) + `\n)`;
} else if (framework === "data.table") {
code = `data.table::data.table(\n`;
code += `data.table::data.table(\n`;
headers.forEach((header, i) => {
const values = data.map((row) => formatValue(row[i], i)).join(", ");
code += ` ${header} = c(${values})${
i < headers.length - 1 ? ",\n" : "\n"
}`;
code += ` ${header} = c(${values})${i < headers.length - 1 ? ",\n" : "\n"
}`;
});
code += `)`;
} else if (framework === "polars") {
code = `polars::pl$DataFrame(\n`;
code += `polars::pl$DataFrame(\n`;
headers.forEach((header, i) => {
const values = data.map((row) => formatValue(row[i], i)).join(", ");
code += ` ${header} = c(${values})${
i < headers.length - 1 ? ",\n" : "\n"
}`;
code += ` ${header} = c(${values})${i < headers.length - 1 ? ",\n" : "\n"
}`;
});
code += `)`;
}
Expand Down