I am working in the development of an app for decision trees using shiny, and java script.
I have a structure of a dynamic tree, where the user can create new branchess and nodes and when the user finishes the construction of his tree, he can press a button and export the structure of the tree in a txt file.
I wonder if there is a way to avoid the button "Export to DataSet" and instead of this, load the tree in the R global environment, like a dynamic data frame.
Java Script Function to export the tree
/*
* Print out CSV of tree: node names
*/
function printCSV() {
var csv = "";
if (root.children) {
root.children.forEach(function (d) {
csv = csv + getCSVstring(d, "-", "", 0);
})
}
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/text,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'TreeDataSet.txt';
hiddenElement.click();
}
HTML code
<button id="exportToMatrix" onclick="printCSV();">Export To DataSet</button>
app.R
library(shiny)
server = function(input, output, session){
x <- output$contents <- renderText({
data2 <<- read.table("exportToMatrix")
assign(data2,envir=.GlobalEnv)
print(summary(data))
})
}
# Run the application
shinyApp(ui = htmlTemplate("www/Tree.html"), server)
Thanks!
On your desktop,
assign
can create variables from the Shiny App to.GlobalEnv
, see following example:After each click, a new variable is created in
.GlobalEnv
: