2016-03-21 18 views
0

So fragte ich das folgende, R Shiny Dynamic Input, vor ein paar Tagen und obwohl die Antwort richtig ist angesichts der Frage, ich möchte nun einige mehr, da ich nicht in der Lage bin, den Code zu bearbeiten beantworte meine neue Frage. Ursprünglich wollte ich den Benutzer bitten, eine Zahl, sagen wir k, anzugeben, die dann dynamisch k Felder für den Benutzer ausfüllt. Nun nimmt der angegebene Code an, dass die Ausgabe ein numerischer Wert ist. Ich möchte jedoch, dass der Benutzer einen Vektor mit 5 Werten in jedem der Felder 1, ..., k angeben kann. Da nach der Angabe von k die Eingaben k Vektoren mit der Länge 5 von numerischen Werten sein werden, möchte ich in der Lage sein, diese Werte in einer k mal 5 Matrix zu speichern. Auf diese Weise kann ich diese Werte verwenden, um später Datenmanipulationen durchzuführen. Wenn es hilft, hier ist der Code von der ursprünglichen Antwort:Amending dynamischen Eingabe-Code in R Shiny

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) 

bearbeiten

Hier Code aktualisiert wird, die noch nicht vollständig funktioniert:

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(tableOutput("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$inputValues <- renderTable({ 
    all <- paste(lapply(1:input$numInputs, function(i) { 
    inputName <- paste("input", i, sep = "") 
    input[[inputName]] 
    })) 
    matrix <- as.matrix(all, ncol=5) 
    as.data.frame(matrix) 
}) 

    }) 

    # 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) 

Antwort

1

Sie brauchen nur Um ein paar Änderungen vorzunehmen:

  1. Ändern mainPanel(textOutput("inputValues"))-mainPanel(tableOutput("inputValues")) (dies nicht wesentlich ist, es zeigt nur die Werte in einer Tabelle/Matrix-Format, damit Sie sie sehen können)
  2. ändern numericInput(inputName, inputName, 1) zu textInput(inputName, inputName, "1 2 3 4 5")
  3. ändern output$inputValues <- renderText({...... zu

    output$inputValues <- renderTable({ 
        all <- paste(lapply(1:input$numInputs, function(i) { 
        inputName <- paste("input", i, sep = "") 
        input[[inputName]] 
        })) 
        matrix = as.matrix(read.table(text=all)) 
        data.frame(matrix) 
    }) 
    

matrix ist was du willst: ak von 5 Matrix.

Beachten Sie, dass ich keine Eingabeverifizierung durchgeführt habe. Es wird angenommen, dass der Benutzer 5 Zahlen in jede Eingabe eingibt, die durch Leerzeichen getrennt sind. Ist dies nicht der Fall, ist die Ausgabe möglicherweise falsch oder Sie sehen einen Fehler. Möglicherweise müssen Sie hier eine Eingabeüberprüfung implementieren, um sicherzustellen, dass es sich um 5 Zahlen und nichts anderes handelt.

komplette Code

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(tableOutput("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 = "") 
     textInput(inputName, inputName, "1 2 3 4 5") 
     }) 
     do.call(tagList, input_list) 
    }) 
    }) 

    # this is just a demo to display all the input values 
    output$inputValues <- renderTable({ 
    all <- paste(lapply(1:input$numInputs, function(i) { 
     inputName <- paste("input", i, sep = "") 
     input[[inputName]] 
    })) 
    matrix = as.matrix(read.table(text=all)) 
    data.frame(matrix) 
    }) 

}) 

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

Dank für das Update. Wenn ich den Code ausführe, werden jedoch keine Felder dynamisch gefüllt. – RustyStatistician

+0

Ich habe die Frage mit dem Code aktualisiert. – RustyStatistician

+0

Aktualisiert mit vollständigem Arbeitscode. –