2016-03-21 22 views
0
'da değişen dinamik giriş kodu

Birkaç gün önce aşağıdaki soruları yanıtladım, R Shiny Dynamic Input, ve soruya verilen cevap doğru olmasına rağmen, şimdi verilen kodu düzeltemediğim için biraz daha detaylandırmak istiyorum. yeni soruma cevap ver. Bu yüzden aslen kullanıcıdan, bir kullanıcı için bir sayı belirtmesini istemek istedim, k demek ki, dinamik olarak kullanıcı için doldurması gereken k alanlarını üretecek. Şimdi, verilen kod, çıktının bir sayısal olduğunu varsayar, ancak kullanıcının, 1, ..., k alanlarının her birinde 5 değerden oluşan bir vektör belirleyebilmesini istiyorum. K belirtildikten sonra, girdiler sayısal değerlerin 5 uzunluğundaki k vektörleri olacak, bu değerleri bir k matrisinde saklayabilmek istiyorum. Bu şekilde daha sonra veri manipülasyonlarını yürütmek için bu değerleri kullanabilirim. Eğer yardımı olacaksa, burada orijinal cevabını kodudur: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) 

Düzenleme İşte

hala yok kod güncellenir tamamen çalışır:

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) 

cevap

1

Yalnızca ihtiyaç Birkaç değişiklik yapmak için:

  1. 'i değiştir
  2. Değişim numericInput(inputName, inputName, 1)

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

için Değişim output$inputValues <- renderText({......

  • textInput(inputName, inputName, "1 2 3 4 5") için mainPanel(tableOutput("inputValues")) için(bunları görebilmeniz için bu gerekli değildir, sadece bir tablo/matris biçiminde değerlerini gösterir) matrix istediğin şey: ak 5 matrix.

    Giriş doğrulaması yapmadığımı unutmayın. Kullanıcının boşluklarla ayrılmış her girişe 5 sayı gireceği varsayılmaktadır. Yoksa, çıktı yanlış olabilir veya bir hata görürsünüz. Burada 5 sayı olduğunu ve başka bir şey olmadığını doğrulamak için bazı giriş kontrollerini uygulamanız gerekebilir. Güncelleme için

    Komple kod

    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

    teşekkürler. Ancak, kodu çalıştırdığımda, herhangi bir alanı dinamik olarak doldurmaz. – RustyStatistician

    +0

    Soruyu bu kodu kullanarak güncelledim. – RustyStatistician

    +0

    Tüm çalışma kodu ile güncellendi. –