通过闪亮的 selectInput() 将更改应用于数据表的特定单元格

Apply changes to a specific cell of a datatable through a shiny selectInput()

我有一个简单闪亮的应用程序

#ui.r
navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(
               uiOutput("tex2"),
               uiOutput("book1"),
               uiOutput("book3")

             ),
             mainPanel(
               DT::dataTableOutput("hot3")

             )
           )))
#server.r
library(shiny)
library(DT)
server <- function(input, output,session) {
  output$tex2<-renderUI({
    numericInput("text2","#tests",
                 value = 1,
                 min=1
    )
  })
  output$book1<-renderUI({
    numericInput("bk1", 
                 "Items in test", 
                 value = 1,
                 min = 1)
  })
  output$book3<-renderUI({

    selectInput("bk3", 
                "Label", 
                choices=(paste("Test",1:input$text2)))
  })
  rt4<-reactive({
        DF=data.frame(
          Label=paste("Test",1:input$text2),
          Avail.=as.integer(input$bk1),
          stringsAsFactors = FALSE)
  })
  output$hot3 <-DT::renderDataTable(
    rt4(),
    selection=list(mode="single") 

  )

}

上部numericInput()“#tests”用于设置table中的行数。然后我使用 selectInput() "Label" 到 select 某个测试(行)并通过 numericInput() "Items in test" 设置 "Avail" 的数字这一行,而其余的具有默认值 1。现在,当我更改 "Items in test".

时,我的所有行都会一起更改

我用 if() 来解决这个问题所以我 post 在这里帮助别人

#ui.r
    navbarPage(
      "Application",
      tabPanel("General",
               sidebarLayout(

                 sidebarPanel(
                   uiOutput("tex2"),
                   uiOutput("book1"),
                   uiOutput("book3")

                 ),
                 mainPanel(
                   DT::dataTableOutput("hot3")

                 )
               )))
    #server.r
    library(shiny)
    library(DT)
    server <- function(input, output,session) {
      output$tex2<-renderUI({
        numericInput("text2","#tests",
                     value = 1,
                     min=1
        )
      })
      output$book1<-renderUI({
        numericInput("bk1", 
                     "Items in test", 
                     value = 1,
                     min = 1)
      })
      output$book3<-renderUI({

        selectInput("bk3", 
                    "Label", 
                    choices=(paste("Test",1:input$text2)))
      })
      rt4<-reactive({
            DF=data.frame(
              Label=paste("Test",1:input$text2),
              Avail.=as.integer(input$bk1),
              stringsAsFactors = FALSE)
       for(i in 1 : input$text2){
         if(DF[i,1]==input$bk3){
           DF[i,2]<-input$bk1
         }
         else{
           DF[i,2]<-1
         }
       }
       DF
     })
      output$hot3 <-DT::renderDataTable(
        rt4(),
        selection=list(mode="single") 

      )

    }