闪亮动态用户界面并汇总DF中的输入

时间:2018-09-10 14:55:14

标签: r shiny dynamic-ui

目标是构建具有动态UI的R-Shiny应用程序。我想做的是请用户选择一些变量(selectInput)。在我的案例中,Outflow1,Outflow2,Outflow 3,Inflow1,Inflow2 ...这些输入是可选的。因此,用户选择一个输入,以便显示新的数字输入(insertUI)。然后,用户在添加的numericInput中选择一个值。我想使用这些值,并将它们乘以一个值(例如0.85),具体取决于所选的输入(Outflow1 = 0.85,Inflow2 = 0.7等)

我怎样才能从numericInputs中收集所有信息,并根据selectinput与我的期望值相乘?我也想用加权值的摘要呈现一个表。

我的ui.R函数如下所示。

library(shiny) 

ui <- fluidPage( 
 sidebarLayout(

  wellPanel(checkboxInput("OUTFLOWBT", "OUTFLOW",
                          value = FALSE),

            uiOutput("OUTFLOW"),
            uiOutput("insertOUTFLOWBtn")),

  wellPanel(checkboxInput("INFLOWBT", "INFLOW",
                          value = FALSE),

            uiOutput("INFLOW"),
            uiOutput("insertINFLOWBtn")),

  uiOutput("insertBtnrmv"),

  wellPanel(tags$div(id = 'placeholder1'),
            tags$div(id = 'placeholder2'),
            tags$div(id = 'placeholder3'),
            tags$div(id = 'placeholder4'))

),

# Show a plot of the generated distribution
mainPanel(
  tableOutput('table')  
) )

我的server.R函数如下:

server <- function(input, output, session) {


  output$OUTFLOW <- renderUI({
    if (isTRUE(input$OUTFLOWBT))
      selectInput(inputId = "OUTFLOW",
                  label = "OUTFLOW",
                  choices = c("","OUTFLOW1", "OUTFLOW2", "Others"))

  })
  output$insertOUTFLOWBtn <- renderUI({
    if (isTRUE(input$OUTFLOWBT))
      actionButton('insertOUTFLOWBtn', '+')

  })
  observeEvent((input$insertOUTFLOWBtn),{

    switch(input$OUTFLOW, 

           "OUTFLOW1" =  insertUI(
             selector = "#placeholder2",
             where = "afterEnd", numericInput("OUTFLOW1", "OUTFLOW1",
                                              value = 0)),

           "OUTFLOW2" =   insertUI(
             selector = "#placeholder2",
             where = "afterEnd", numericInput("OUTFLOW2", "OUTFLOW2",
                                              value = 0)),
           "Others" =  insertUI(
             selector = "#placeholder2",
             where = "afterEnd", tags$div(
               tagList(numericInput("OutflowsOth", "Other Outflow",
                                    value = 0),
                       sliderInput("OutflowOthP", "Other Outflow Percentage",
                                   min = 0, max = 100, value = 100)))))

  })
  output$INFLOW <- renderUI({
    if (isTRUE(input$INFLOWBT))
      selectInput(inputId = "INFLOW",
                  label = "INFLOW",
                  choices = c("","INFLOW1", "INFLOW2", "Others"))

  })
  output$insertINFLOWBtn <- renderUI({
    if (isTRUE(input$INFLOWBT))
      actionButton('insertINFLOWBtn', '+')



  })

  observeEvent((input$insertINFLOWBtn),{
    btn <- input$insertHQLABtn
    id <- paste0('txt', btn)
    switch(input$INFLOW, 

           "INFLOW1" =  insertUI(
             selector = "#placeholder3",
             where = "afterEnd", numericInput("INFLOW1", "INFLOW1",
                                              value = 0)),

           "INFLOW2" =   insertUI(
             selector = "#placeholder3",
             where = "afterEnd", numericInput("INFLOW2", "INFLOW2",
                                              value = 0)),
           "Others" =  insertUI(
             selector = "#placeholder3",
             where = "afterEnd", tags$div(
               tagList(numericInput("INFLOWOth", "Other Inflow",
                                    value = 0),
                       sliderInput("INFLOWOthP", "Other Inflow Percentage",
                                   min = 0, max = 100, value = 100)))))

  })

}

shinyApp(ui = ui, server = server)

要让您对UI有所了解,请先了解一下: enter image description here

0 个答案:

没有答案