简体   繁体   English

如何在 Global R 环境中加载动态数据框?

[英]How load a dynamic data frame in Global R environment?

I am working in the development of an app for decision trees using shiny, and java script.我正在使用闪亮的 Java 脚本开发决策树应用程序。

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.我有一个动态树的结构,用户可以在其中创建新的分支和节点,当用户完成他的树的构建时,他可以按下一个按钮并将树的结构导出到 txt 文件中。

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.我想知道是否有办法避免“导出到数据集”按钮,而不是这样,而是在 R 全局环境中加载树,就像动态数据框一样。

Java Script Function to export the tree用于导出树的 Java Script 函数

/*
 * 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 HTML代码

<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:在你的桌面上, assign可以从 Shiny App 中创建变量到.GlobalEnv ,见下面的例子:

library(shiny)

ui <- fluidPage(
  actionButton("do", "Click Me")
)

server <- function(input, output, session) {
  counter <- reactiveVal(0)
  observeEvent(input$do, {
    counter(counter()+1)
    cat('Saving object ', counter(),'\n')
    assign(paste0("test",counter()), counter(),.GlobalEnv)
    
  })
}

shinyApp(ui, server)

After each click, a new variable is created in .GlobalEnv :每次点击后,都会在.GlobalEnv创建一个新变量:

Saving object  1 
Saving object  2 
Saving object  3 
Saving object  4 


> ls()
 [1] "server" "test1"  "test2"  "test3"  "test4"  "ui" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM