2016-03-22 7 views
1

私は、光沢のあるアプリケーションでtextInput()を並べて追加できる必要があります。新しいテキストボックスとコマンドボタンを使用するtextInput()が必要です。コマンドボタンがクリックされるたびに、最初のtxtInputからラベルを取得する新しいテキストボックスをリストに追加する必要があります。例えばどのように光沢のあるアプリケーションでinputTextを動的に追加しますか?

:私はコマンドボタンをクリックしたときに

1stTextBox:[ Application ] 
{commandButton} 

が、私はのcommandButton以下のようにtextInputを持っている必要があり、

Application:[  ] 

は私が1stTextBoxに何かを入れて、コマンドボタンをクリックした場合textInputリストに追加する必要があります。

どのように光沢を持ってこれを動的に行うことができますか?

これは誤りである:

Listening on http://127.0.0.1:3091 
Warning: Error in handlers$add: Key/already in use 
Stack trace (innermost first): 
    43: handlers$add 
    42: handlerManager$addHandler 
    41: startApp 
    40: runApp 
    1: shiny::runApp 
Error in handlers$add(handler, key, tail) : Key/already in use 
+0

サーバーコードに 'observeEvent'と' renderUI'の組み合わせが必要です。 'observeEvent'はボタンのクリックを監視し、' renderUI'を使用して、指定されたラベルを持つ2番目のテキスト入力を描画します。 –

+0

@warmoverflow、私は光り輝くために非常に新しいです。どんな例がありますか? – user1471980

+0

別の質問のための例です。これは、numericInputの変更を観察し、より多くのnumericInputをレンダリングします。 http://stackoverflow.com/questions/36094718/r-shiny-dynamic-input/36096128#36096128 –

答えて

1

Iはコード例を与えます。これを試すには、スクリプトをコピーして全体を実行します。

私はreactiveValuesオブジェクトを使用して、バックエンドに情報を保持しています。 ここで、info_keeper$input_infoはリストであり、各要素は[id、label、value]の3文字の文字ベクトルとします。

ボタンをクリックすると、(1)すでに定義されているtextInputsの内容が保存されます。 (2)新しい要素が追加されます。

不要な動作を避けるために、おそらくは必要以上にisolateを使用しています。

library(shiny) 

ui <- list(
    textInput("name", "Type new text input name", value = ""), 
    actionButton("btn", "click me to create text input"), 
    uiOutput("newInputs") 
) 

server <- function(input, output) 
{ 
    info_keeper <- reactiveValues(
    input_info = list() 
) 

    observeEvent(input$btn, { 
    # copy the current contents to info_keeper 
    isolate(
    { 
     for (i in seq_along(info_keeper$input_info)) 
     { 
     id <- info_keeper$input_info[[i]][1] 
     info_keeper$input_info[[i]][3] <- input[[id]] 
     } 
    }) 

    # add new text input to the info_keeper 
    isolate(
    { 
     newid <- paste(
     "text", isolate(length(info_keeper$input_info)) + 1, sep = "") 
     info_keeper$input_info <- c(
     info_keeper$input_info, list(c(newid, input$name, ""))) 
    }) 

    # invoke the update of the text inputs 
    info_keeper 
    }) 

    output$newInputs <- renderUI({ 
    lapply(info_keeper$input_info, function(a) 
     textInput(a[1], a[2], value = a[3])) 
    }) 
} 

runApp(list(ui = ui, server = server)) 
+0

このエラーは「ERROR:Key/already in use」のままです。 rstudioウィンドウにコード全体を配置しました。何か案は? – user1471980

+0

私は元の投稿にすべてのエラーを配置しました。 – user1471980

+0

私は分かりません。チェックするだけで、rstudioを再起動し、エディタでコード全体を貼り付けてから、rstudioのソースボタンをクリックすることができますか?私の環境では、これはエラーなしでアプリを起動します。これでも同じエラーが発生する場合は、お知らせください。 –

関連する問題