2014-10-17 17 views
1

我將R閃光燈與ggplot結合使用以可視化某個數據集。我希望用戶能夠爲這個數據集添加值。我可以獲得我的應用程序,以便向我顯示原始數據+一個數據點,但只要用戶輸入一個新點,舊點就消失了:用戶輸入數據實際上並未存儲在我的數據框中。向R數據框中添加(多個)值使用R閃光燈

一些我正在使用的代碼的(改變的變量名爲簡單起見):

shinyServer(
    function(input, output) {  
    output$newPlot <- renderPlot({ 
     input$addButton 
     isolate({ 
     x <- as.numeric(input$x) 
     y <- as.numeric(input$y) 
     if (!is.na(y)) { 
      data <- rbind(data, data.frame(x = x, y = y)) 

      # more code here 

     } 

     # more code here 

     plot <- myPlot(data) 
     print(plot) 

     }) 
    }) 
    } 
) 

用戶可以給出x和y值與textInput,然後用一個按鈕(actionButton)提交這些值。每次用戶點擊「添加」時,最近輸入的x和y值將顯示在原始數據的頂部,但用戶輸入的任何其他值(在同一會話中)都會丟失。我如何閃亮記住我的用戶輸入並將其全部繪製出來?

+0

您是否希望新的輸入永久存儲(即,如果您重新啓動閃亮的應用程序,仍然存在)或者僅僅是爲了會話? – cdeterman 2014-10-17 14:47:12

+0

只爲會議。 – Marleen 2014-10-17 14:49:18

回答

3

根據您提供的代碼,我無法重現您的具體情況,但可能有一個示例可以幫助您。您需要的部分是reactiveValues,這允許您存儲數據集,並在您認爲合適的整個會話期間進行更新。也許這可以幫助你解決你的問題?

require(shiny) 
data(iris) 

runApp(
    list(
    ui = fluidPage(
     headerPanel('Adding Data'), 
     sidebarPanel(
     textInput("species", label="Add a new species", value="Enter species"), 
     numericInput("sepal.length", label="Add a new sepal length", value=""), 
     numericInput("sepal.width", label="Add a new speal width", value=""), 
     numericInput("petal.length", label="Add a new petal length", value=""), 
     numericInput("petal.width", label="Add a new petal width", value=""), 
     actionButton("addButton", "UPLOAD!") 
    ), 
     mainPanel(
     tableOutput("table")) 
    ), 

    server = function(input, output) {  
     # just a small part of iris for display 
     iris_sample <- iris[sample(nrow(iris), 10),] 
     row.names(iris_sample) <- NULL 

     # The important part of reactiveValues() 
     values <- reactiveValues() 
     values$df <- iris_sample 
     observe({ 

     # your action button condition 
     if(input$addButton > 0) { 
      # create the new line to be added from your inputs 
      newLine <- isolate(c(input$sepal.length, input$sepal.width, input$petal.length, input$petal.width, input$species)) 
      # update your data 
      # note the unlist of newLine, this prevents a bothersome warning message that the rbind will return regarding rownames because of using isolate. 
      isolate(values$df <- rbind(as.matrix(values$df), unlist(newLine))) 
     } 
     }) 
     output$table <- renderTable({values$df}, include.rownames=F) 
    } 
) 
) 
+0

感謝堆,這確實幫助我獲得了我想要的效果。 – Marleen 2014-10-20 10:12:57

+0

這對我也很有幫助。但是,我無法管理一些問題,我指出在這個問題:http://stackoverflow.com/questions/34930846/another-follow-up-to-add-values-to-a-reactive-table-in-shiny -when-we-already-h 如果你願意幫助我,我會很高興。 – 2016-01-22 19:11:13

+0

'addData < - 觀察({...'with'observe'不能爲變量賦值 – Stophface 2016-05-19 16:04:24