2016-11-15 121 views
2

我試圖讓conditionalPanellabel包含從使用另一個輸入創建的輸出派生的名稱。我一直在研究,還沒有能夠弄清楚如何做到這一點呢。下面是我試過的,無濟於事。R基於另一個輸入的Shiny條件輸入標籤

ui.R

shinyUI(
    fluidPage( 
    titlePanel("example"), 
    sidebarLayout(  
     sidebarPanel(

     numericInput("criteria_count", 
        label = "How many criteria are being considered? (max = 5)", 
        2, min = 2, max = 5), 

     textInput("critera_text", 
        label = "Type the different criteria titles, seperated by commas.", 
        value = "Example A, Example B"), 
     #verbatimTextOutput("value"), 

     conditionalPanel(condition = "input.criteria_count == 2", 
         selectInput("main_cri1", 
            label = paste0("How much more important is ", output.criteria1, " than ", output.criteria2, "?"), 
            c("9 (Extremely more important)", 
             "7 (Much more important)", 
             "5 (Moderately more important)", 
             "3 (Slightly more important)", 
             "1 (Equally as important)", 
             "1/3 (Slightly less important)", 
             "1/5 (Moderately less important)", 
             "1/7 (Much less important)", 
             "1/9 (Extremely less important)"))) 

    ) 
    ) 
) 
) 

server.R

library(shiny) 

shinyServer(function(input, output){ 

    output$criteria1 <- renderText({ 
    strsplit(input$critera_text, ", ")[[1]][1] 
    }) 

    output$criteria2 <- renderText({ 
    strsplit(input$critera_text, ", ")[[1]][2] 
    }) 

}) 

鑑於上述ui.R,該labelconditionalPanel應如下:

「實例A比實例B更重要?「

回答

1

試試這個(app.R

ui <- 
    fluidPage(titlePanel("example"), 
      sidebarLayout(

       sidebarPanel(
       numericInput(
        "criteria_count", 
        label = "How many criteria are being considered? (max = 5)", 
        2, 
        min = 2, 
        max = 5 
       ), 

       textInput("critera_text", 
          label = "Type the different criteria titles, seperated by commas.", 
          value = "Example A, Example B"), 

       conditionalPanel(condition = "input.criteria_count == 2", 
           uiOutput("mySelectInput")) 
      ), 

       mainPanel() 

      )) 

server <- 
    function(input, output) { 
    output$mySelectInput <- renderUI({ 
     criteria1 <- strsplit(input$critera_text, ", ")[[1]][1] 
     criteria2 <- strsplit(input$critera_text, ", ")[[1]][2] 

     selectInput(
     "main_cri1", 
     label = paste0(
      "How much more important is ", 
      criteria1, 
      " than ", 
      criteria2, 
      "?" 
     ), 
     choices = c(
      "9 (Extremely more important)", 
      "7 (Much more important)", 
      "5 (Moderately more important)", 
      "3 (Slightly more important)", 
      "1 (Equally as important)", 
      "1/3 (Slightly less important)", 
      "1/5 (Moderately less important)", 
      "1/7 (Much less important)", 
      "1/9 (Extremely less important)" 
     ) 
    ) 
    }) 
    } 

shinyApp(ui = ui, server = server) 
+0

的偉大工程。謝謝! – bshelt141