2014-10-17 2 views
1

저는 반짝이는 ggplot과 함께 특정 데이터 세트를 시각화하고 있습니다. 사용자가이 데이터 세트에 값을 추가 할 수 있기를 바랍니다. 지금까지 원래 데이터 + 하나의 데이터 포인트를 보여주기 위해 애플리케이션을 사용할 수 있지만, 사용자가 새로운 포인트를 입력하자 마자 이전 데이터는 사라졌습니다. 사용자 입력 데이터는 실제로 내 데이터 프레임에 저장되지 않습니다.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) 

     }) 
    }) 
    } 
) 

사용자는 textInput으로 x와 y 값을 제공 한 후 버튼 (actionButton)로 그 값을 제출할 수 있습니다. 사용자가 '추가'를 누를 때마다 x 및 y에 대해 가장 최근에 입력 된 값이 원본 데이터 위에 표시되지만 사용자가 입력 한 다른 값은 손실됩니다. 내 사용자 입력을 기억하고 모든 것을 입력하도록 반짝이는 방법은 무엇입니까?

+0

새로운 입력을 영구적으로 저장 하시겠습니까 (즉, 반짝이는 앱을 다시 시작 하시겠습니까?) 또는 세션에만 사용 하시겠습니까? – cdeterman

+0

세션에만 사용하십시오. – Marleen

답변

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

+0

이것은 나에게도 매우 도움이된다. 그러나, 나는이 문제에서 나타내는 몇 가지 문제를 관리 할 수 ​​없습니다 : http://stackoverflow.com/questions/34930846/another-follow-up-to-add-values-to-a-reactive-table-in-shiny -when-we-already-h 당신이 나를 도울 수 있다면, 나는 그렇게 기쁘다. –

+0

'addData <- observe ({...'observe를 사용하면 변수에 값을 할당 할 수 없습니다.) – Stophface

관련 문제