动态添加 UI 元素并在闪亮的数据框中收集它们的输入 [英] Dynamically add UI elements and gather their input in a dataframe in shiny

查看:21
本文介绍了动态添加 UI 元素并在闪亮的数据框中收集它们的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ui.R函数如下图

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("Add Features"),
  sidebarPanel(width=4,
  fluidRow( 
  column(6,  selectInput("features", label = h3("Features"), 
  choices = list("Feature1","Feature2","Feature3"), selected = "Feature1")),    

  br(),
  br(),
  column(6,  numericInput("n", label="",min = 0, max = 100, value = 50)),
  br(),
  column(2, actionButton("goButton", "Add!"))
  #column(3, submitButton(text="Analyze"))
)),


mainPanel(
verbatimTextOutput("nText"),
textOutput("text2")
)
))

我的server.R函数如下:

library(shiny)

shinyServer(function(input, output) {
 selFeatures <- data.frame()
  valFeatures <- data.frame()
  # builds a reactive expression that only invalidates 
  # when the value of input$goButton becomes out of date 
  # (i.e., when the button is pressed)
  ntext <- eventReactive(input$goButton, {

    selFeatures <- rbind(selFeatures,input$features)
    valFeatures <- rbind(valFeatures,input$n)
    paste("The variables are",input$features,input$n)
    paste("The variables are",selFeatures,valFeatures)
    })

    output$nText <- renderText({
    ntext()
  })
   output$text2 <- renderText({ 
     paste("You have selected", input$features)
   })
})

我想做的是要求用户输入一些变量.这里是 Feature1Feature2Feature3.用户必须输入Feature1,但Feature2Feature3 是可选的.所以,在这里用户选择一个特征,在 numericInput 中输入它的值,然后按下按钮 Add.选择 feature1 后按 add ,用户可以选择使用添加按钮提交表单或添加功能2和3.最后,我想用这三个变量来学习一个预测模型.如何收集数据框中的所有估算信息以对其进行处理.此外,如果可能的话,在添加后从 selectBox 中删除 Feature1.在按下添加按钮之前,我希望我的用户界面如下所示

What I want to do is ask user to input some variables. Here Feature1, Feature2, and Feature3. User has to input Feature1 but Feature2 and Feature3 are optional. So, here user selects a feature, inputs its value in numericInput and presses button Add. When Add is pressed after selecting Feature1, user can select to submit the form or add features 2 and 3 using the add button. I finally, want to use these three variables to learn a prediction model. How can I collect all the imputed information in the dataframe to process it. Also, if possible to remove Feature1 from the selectBox after it has been added. I want my UI to look like the following before Pressing the add button

按下添加按钮后,它应该如下所示.

and it should look like the following after pressing the add button.

这里的 feature1 不需要在选择框里,只是一个显示它已经添加的方式就可以了.

The feature1 here need not be in the select box, just a way to display that it has been added is fine.

推荐答案

我不太确定你为什么要使用 selectInputs 来设置变量值,所以这里有一个关于如何访问的一般示例来自动态生成内容的输入:

I wasn't quite sure why you wanted to use selectInputs for setting the variable values so here's a general example on how to access inputs from dynamically generated content:

library(shiny)

ui <- shinyUI(pageWithSidebar(
  headerPanel("Add Features"),
  sidebarPanel(width=4,
               fluidRow(column(12,
                               h3('Features'),
                               uiOutput('uiOutpt')
               )), # END fluidRow
               fluidRow(
                 column(4,div()),
                 column(4,actionButton("add", "Add!")),
                 column(4,actionButton('goButton',"Analyze"))
               ) # END fluidRow
  ), # END sidebarPanel
  mainPanel(
    verbatimTextOutput("nText"),
    textOutput("text2"),
    tableOutput('tbl')
  )
))

server <- shinyServer(function(input, output) {
  features <- reactiveValues(renderd=c(1))

  ntext <- eventReactive(input$goButton, {
    out <- lapply(features$renderd,function(i){
      fv <- paste0('numInp_',i)
      vn <- paste0('Feature',i)
      # Get input values by namw
      sprintf( 'Variable: %s, Value: %5.3f',input[[vn]],input[[fv]] )
    })
    do.call(paste,c(out,sep="\n"))
  })

  df <- eventReactive(input$goButton, {
    out <- lapply(features$renderd,function(i){
      fv <- paste0('numInp_',i)
      vn <- paste0('Feature',i)
      data.frame(Variable=input[[vn]], Value=input[[fv]] )
    })
    do.call(rbind,out)
  })

  output$nText <- renderText({
    ntext()
  })
  output$text2 <- renderText({ 
    sprintf("You have selected feature: %s", paste(features$renderd,collapse=", "))
  })

  output$tbl <- renderTable({
    df()
  })

  # Increment reactive values used to store how may rows we have rendered
  observeEvent(input$add,{
    if (max(features$renderd) > 2) return(NULL)
    features$renderd <- c(features$renderd, max(features$renderd)+1)
  })

  # If reactive vector updated we render the UI again
  observe({
    output$uiOutpt <- renderUI({
      # Create rows
      rows <- lapply(features$renderd,function(i){
        fluidRow(
          column(6,  selectInput(paste0('Feature',i), 
                                 label = "", 
                                 choices = list("Feature1","Feature2","Feature3"), 
                                 selected = paste0('Feature',i))),   
          column(6,  numericInput(paste0('numInp_',i), label="",min = 0, max = 100, value = runif(1,max=100)))
        )
      })
      do.call(shiny::tagList,rows)

    })
  })
})

shinyApp(ui=ui,server=server)  

我只是将动态生成的内容的 ID 存储在一个向量中,以帮助我跟踪我生成的内容.要访问这些值,我只需根据存储在向量中的数字重建元素 ID.

I'm simply storing the ID's of the dynamically generated content in a vector that helps me keep track of what I've generated. To access the values i simply reconstruct the elements ID from the numbers stored in the vector.

这篇关于动态添加 UI 元素并在闪亮的数据框中收集它们的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆