2017-11-11 150 views
0

嘿,我有一个闪亮的应用程序输入数据并与他们一起工作。但是现在我想要一个为子集功能选择因子的动作按钮。问题是,当我启动应用程序并输入数据时,因素不存在,只需在第一次点击操作按钮后,因素就会获得数据的列名称。R闪亮:得到输入数据后的所有因素

我该如何编程,以便在读取数据后直接显示这些因子。感谢您的帮助!

这里的UI和服务器的一部分

   radioButtons(
        "fileType_Input", 
        label = h5("Choose file type and click on browse"), 
        choices = list(".csv" = 1, ".xlsx" = 2), 
        selected = 1, 
        inline = TRUE 
       ), 

       fileInput('file1', '' ), 

       selectInput("letters", label=NULL, factors, multiple = TRUE), 

       actionButton("choose", "Auswahl"), 

       verbatimTextOutput("list") 

服务器:

shinyServer(function(input, output, session) { 

    # Get the upload file 
    myData <- reactive({ 
    inFile <- input$file1 

    if (is.null(inFile)) { 
     return(NULL) } 

    if (input$fileType_Input == "1") { 
     read.csv2(inFile$datapath, 
       header = TRUE, 
       stringsAsFactors = FALSE) 
    } else { 
     read_excel(inFile$datapath) 
    } 
    }) 

    # When the Choose button is clicked, add the current letters to v$choices 
    # and update the selector 
    observeEvent(input$choose, { 
    data <- myData() 
    factors <- colnames(data) # get the names of the Factors in a Vector to select them 
    v$choices <- input$letters # append(v$choices,input$letters) 
    updateSelectInput(session, "letters", 
         choices = factors #[!factors %in% v$choices)] 
    ) 
    }) 

    output$list <- renderPrint({ 
    v$choices 
    }) 

回答

0

而不是把你的代码observeEventinput$choose的,你可以把它放在观察。像这样的东西:

observe({ 

    if(!is.null(myData())) 
    { 
     data <- myData() 
     factors <- colnames(data) # get the names of the Factors in a Vector to select them 
     v$choices <- input$letters # append(v$choices,input$letters) 
     updateSelectInput(session, "letters", 
         choices = factors #[!factors %in% v$choices)] 
    ) 
    } 
    }) 

希望它有帮助!

+0

感谢您的帮助,不幸的是,它不像我想象的那样工作。我想保留“observevent”并且只安装一个函数,我在数据加载之后,一旦“factor”矢量设置完成,所以他可以直接显示给我。如果按照您的建议,我的按钮将不再工作。 – Zorro

+0

然后,您可以在反应表达式中使用'updateSelectInput',而不是将它放在观察中。 – SBista

+0

Sry,但我不明白你的想法...我尝试了一些被动的表情,但我仍然有办法。你能再解释一遍吗? – Zorro