2017-05-28 3 views
0

に基づいにtextInput行を削除し、私は私のスクリプトを変更:シャイニー:動的に追加/ <a href="https://gallery.shinyapps.io/111-insert-ui/" rel="nofollow noreferrer">this article</a>に基づいてインデックス

library(shiny) 

ui = fluidPage( 
    actionButton("add", "Add new Row", icon=icon("plus", class=NULL, lib="font-awesome")), 
    actionButton("remove", "Remove last Row", icon=icon("times", class = NULL, lib = "font-awesome")), 
    tags$div(id = 'placeholder') 
) 

server = function(input, output) { 

    ## keep track of elements inserted and not yet removed 
    inserted <- c() 

    observeEvent(input$add, { 
    id <- '' 
    insertUI(
     selector = "#placeholder", 
     where = "beforeBegin", 
     ui =tags$div(
     id = id, 
     fluidRow(
      column(2, 
       textInput("alias", label = h5("first")) 
     ), 
      column(2, 
       textInput("pop", label = h5("second")) 
     ), 
      column(2, 
       textInput("parent", label = h5("third")) 
     ), 
      column(2, 
       textInput("dims", label = h5("fifth")) 
     ), 
      column(2, 
       textInput("method", label = h5("fourth")) 
     ), 
      column(2, 
       textInput("args", label = h5("sixth")) 
     ) 
     ) 
    ) 
    ) 

    inserted <<- c(inserted, id) 

    }) 

    observeEvent(input$remove, { 
    removeUI(
     ## pass in appropriate div id 
     selector = paste0('#', inserted[length(inserted)]) 
    ) 

    inserted <<- inserted[-length(inserted)] 
    }) 

} 

shinyApp(ui = ui, server = server) 

私はthis questionに見えたが、addボタンが正常に動作しますが、行が行うbefore.Theよりも混乱していますremoveボタンを押すと消えません。私は間違って何をしていますか?

ありがとうございます!

答えて

1

idの割り当てを除いて、あなたはかなり近づいていました。 IDを設定するのを忘れましたか?私が見る唯一の割り当ては:id <- ''です。 また、グローバル変数ではなく、光沢のあるreactiveValues()で作業する必要があります。私は今data.table(後にさらに操作でそれを使用する)でユーザーの入力を保存したい場合、私は何を開始すると、

library(shiny) 

ui = fluidPage( 
    actionButton("add", "Add new Row", icon=icon("plus", class=NULL, lib="font-awesome")), 
    actionButton("remove", "Remove last Row", icon=icon("times", class = NULL, lib = "font-awesome")), 
    tags$div(id = 'placeholder') 
) 

server = function(input, output) { 

    ## keep track of elements inserted and not yet removed 
    inserted <- reactiveValues(val = 0) 

    observeEvent(input$add, { 
    id <- length(inserted$val) + 1 
    insertUI(
     selector = "#placeholder", 
     where = "beforeBegin", 
     ui =tags$div(
     id = id, 
     fluidRow(
      column(2, 
       textInput("alias", label = h5("first")) 
     ), 
      column(2, 
       textInput("pop", label = h5("second")) 
     ), 
      column(2, 
       textInput("parent", label = h5("third")) 
     ), 
      column(2, 
       textInput("dims", label = h5("fifth")) 
     ), 
      column(2, 
       textInput("method", label = h5("fourth")) 
     ), 
      column(2, 
       textInput("args", label = h5("sixth")) 
     ) 
     ) 
    ) 
    ) 
    inserted$val <- c(inserted$val, id) 
    print(inserted$val) 
    }) 

    observeEvent(input$remove,{ 
    print(inserted$val) 
    removeUI(
     ## pass in appropriate div id 
     selector = paste0('#', inserted$val[length(inserted$val)]) 
    ) 

    inserted$val <- inserted$val[-length(inserted$val)] 
    }) 

} 

shinyApp(ui = ui, server = server) 
+0

は以下の適応コードを参照してください? – Rivka

+1

新しい質問、...;)真剣に、私は同様にreactValueにdata.tableを格納し、そこに更新します。あなたがそこに助けを必要とするならば、私は新しい質問でそれをしなければならないと思うし、あなたはここに私に通知することができます... – BigDataScientist

+0

解決済み@BigDataScientist – Rivka

関連する問題