2016-03-18 21 views
3

Ich möchte eine R-glänzende App erstellen, die einen dynamischen Eingang hat, der den Benutzer nach einer numerischen Eingabe fragt und dann basierend auf diesem Eingang 4 weitere Eingabefelder generiert. Hier ist, was ich im Sinn habe.R Shiny Dynamischer Eingang

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(

) 
))) 

Ich weiß, dass der obige Code nicht funktioniert, aber es ist meine Begründung. Ich weiß, dass Shiny bedingte Anweisungen hat, aber ich kann keinen finden, der es mir erlaubt, eine vorgegebene Anzahl zusätzlicher Felder zu generieren. Ist das überhaupt möglich?

+0

Sie müssen 'renderUI' im Server-Code verwenden. Sehen Sie diese Seite http://shiny.rstudio.com/articles/dynamic-ui.html für einige Beispiele –

+0

@warmoverflow Ich bin mir bereits dessen bewusst, aber ich verstehe nicht, wie man es verwendet, um ein vorspezifiziertes dynamisch zu erstellen Anzahl der Eingänge. – RustyStatistician

Antwort

3

Sehen Sie ein funktionierendes Beispiel unter

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

Ich öffnete eine neue Frage basierend auf Ihrer Antwort, vielleicht können Sie einen Blick darauf werfen: http://stackoverflow.com/questions/36141451/ammending-dynamic-input-code-in-r-shiny – RustyStatistician