2016-03-18 5 views
3

ユーザーに数値入力を求め、その入力に基づいてさらに4つの入力フィールドを生成するダイナミック入力を持つRシャイニーアプリを構築したいと考えています。ここに私が心に留めていることがあります。Rシャイニーダイナミック入力

library(shiny) 

# Define UI for random distribution application 
shinyUI(fluidPage(

    # Application title 
    titlePanel("Calcs"), 

    # Sidebar with controls to select the random distribution type 
    # and number of observations to generate. Note the use of the 
    # br() element to introduce extra vertical spacing 
    sidebarLayout(
    sidebarPanel(
    numericInput("dorr", "How many inputs do you want",4), 
    i = i+1 
    while(input$dorr<i){ 
     numericInput("S", "Number of simulations to run:", 10) 
     i=i+1 
    } 
    numericInput("S", "Number of simulations to run:", 10), 
    actionButton(inputId = "submit_loc",label = "Run the Simulation") 
    ), 


    mainPanel(

) 
))) 

私は上記のコードは動作しませんが、それは私の理論的根拠です。私はShinyに条件文があることを知っていますが、あらかじめ指定された数の追加フィールドを生成することができないようです。これも可能ですか?

+0

以下の実施例を参照してください。いくつかの例についてはこのページを参照してください。 –

+0

@warmoverflow私はすでにそれを認識していますが、それを使って動的に事前に指定されたものを作成する方法を理解していません入力数。 – RustyStatistician

答えて

3

あなたは、サーバーコードで `renderUI`を使用する必要が

library(shiny) 

ui <- shinyUI(fluidPage(
    titlePanel("Old Faithful Geyser Data"), 

    sidebarLayout(
    sidebarPanel(
     numericInput("numInputs", "How many inputs do you want", 4), 
     # place to hold dynamic inputs 
     uiOutput("inputGroup") 
    ), 
    # this is just a demo to show the input values 
    mainPanel(textOutput("inputValues")) 
) 
)) 

# Define server logic required to draw a histogram 
server <- shinyServer(function(input, output) { 
    # observe changes in "numInputs", and create corresponding number of inputs 
    observeEvent(input$numInputs, { 
    output$inputGroup = renderUI({ 
     input_list <- lapply(1:input$numInputs, function(i) { 
     # for each dynamically generated input, give a different name 
     inputName <- paste("input", i, sep = "") 
     numericInput(inputName, inputName, 1) 
     }) 
     do.call(tagList, input_list) 
    }) 
    }) 

    # this is just a demo to display all the input values 
    output$inputValues <- renderText({ 
    paste(lapply(1:input$numInputs, function(i) { 
     inputName <- paste("input", i, sep = "") 
     input[[inputName]] 
    })) 
    }) 

}) 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

私はあなたの答えに基づいて新しい質問を開いた、多分あなたが見てみることができる:http://stackoverflow.com/questions/36141451/ammering-dynamic-input-code-in-r-shiny – RustyStatistician

関連する問題