R语言 如何计算选择输入框被点击的次数?

k97glaaz  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(151)

下面的MWE代码使用observeEvent()函数和reactiveVal()来跟踪和显示selectInput()框被点击的次数和actionButton()被点击的次数。
actionButton()跟踪工作正常。注意在output$...代码中selectInput()actionButton()的工作方式不同,看起来很奇怪。
无论如何,我无法让selectInput()跟踪与actionButton()相同的内容。当第一次调用应用程序时,用户单击第一个选项时会呈现selectInput()框中的(“Cyl”)不计为单击,而我希望它计为单击。如果在selectInput()中单击了相同的选项(例如“Trans”)不止一次,当我每次点击都要计数时,点击〉1不算点击。基本上,每当用户点击selectInput()框时,它都需要被包括在“点击”中。有什么方法可以做到这一点吗?
在完整的代码中,这很重要,因为该框中的selectInput()会触发removeUI(),并且选项列表是动态的,每次单击都会按顺序重新编号。
代码:

library(shiny)

ui = fluidPage(hr(),
  selectInput("selInput",label=NULL,c("Cyl"="cyl","Trans"="am","Gears"="gear"),selected=NULL),
  actionButton("addBtn","Add"), hr(),
  textOutput("clickSelInput"),
  textOutput("clickAddBtn"),
  tableOutput("data")
)
  
server = function(input, output) {
  x = reactiveVal(0)
  y = reactiveVal(0)
    
  output$data <- renderTable({mtcars[1:10, c("mpg", input$selInput), drop = FALSE]})
    
  observeEvent(input$selInput,{x(x()+1)})
  observeEvent(input$addBtn,{y(y()+1)})
    
  output$clickSelInput <- renderText({paste('Select Input clicks =',x()-1)})
  output$clickAddBtn <- renderText({paste('Add Button clicks =',y())})
    
}

shinyApp(ui, server)
kknvjkwl

kknvjkwl1#

在这种情况下,将“multiple = TRUE”添加到selectInput()解决了问题。还允许从OP中的out$selInput..x()-1)})中删除怪异的-1\f25 -1\f6。这也适用于较大的应用程序。参见修订的OP代码,其中包含OP注解的更改(下面是“较大的应用程序”,其中此功能很重要):

library(shiny)

ui = fluidPage(hr(),
  selectInput("selInput",
              label=NULL,
              c("Cyl"="cyl","Trans"="am","Gears"="gear"),
              selected=NULL,
              multiple=TRUE # added this
              ),
  actionButton("addBtn","Add"), hr(),
  textOutput("clickSelInput"),
  textOutput("clickAddBtn"),
  tableOutput("data")
)
  
server = function(input, output) {
  x = reactiveVal(0)
  y = reactiveVal(0)
    
  output$data <- renderTable({mtcars[1:10, c("mpg", input$selInput), drop = FALSE]})
    
  observeEvent(input$selInput,{x(x()+1)})
  observeEvent(input$addBtn,{y(y()+1)})
    
  output$clickSelInput <- renderText({paste('Select Input clicks =',x())}) # removed the -1 from x()
  output$clickAddBtn <- renderText({paste('Add Button clicks =',y())})
    
}

shinyApp(ui, server)

下面是这个功能非常重要的“大型应用程序”:

library(dplyr)
library(rhandsontable)
library(shiny)

rowNames1 <- c("A", "B", "C", "Sum")
DF1 <- data.frame(row.names = rowNames1, "Col 1" = c(1, 1, 0, 2), check.names = FALSE)

ui <- fluidPage(br(),
  rHandsontableOutput('hottable1'),br(),
  actionButton("addCol1", "Add column 1"),br(),
  h5(strong("Select column to delete:")),
  uiOutput("delCol1"), hr(),
  textOutput("clickSelInput"),
  textOutput("clickAddBtn"),
)

server <- function(input, output) {
  x = reactiveVal(0)
  y = reactiveVal(0)
  uiTbl1 <- reactiveVal(DF1)
  
  observeEvent(input$hottable1, {uiTbl1(hot_to_r(input$hottable1))})
  
  output$hottable1 <- renderRHandsontable({
    rhandsontable(uiTbl1(),rowHeaderWidth = 100, useTypes = TRUE)%>%
      hot_context_menu(allowRowEdit = FALSE, allowColEdit = FALSE)
  })
  
  observeEvent(input$addCol1, {
    newCol <- data.frame(c(1,1,0,2))
    names(newCol) <- paste("Col", ncol(hot_to_r(input$hottable1)) + 1)
    uiTbl1(cbind(uiTbl1(), newCol))
  })
  
  observeEvent(input$delCol1, {
    tmp <- uiTbl1()                  
    delCol <- input$delCol1                              
    tmp <- tmp[ , !(names(tmp) %in% delCol), drop = FALSE]  
    newNames <- sprintf("Col %d",seq(1:ncol(tmp)))       
    names(tmp) <- newNames                                  
    uiTbl1(tmp)                                         
  })

  output$delCol1 <- 
    renderUI(
      selectInput(
        "delCol1", 
        label = NULL,
        choices = colnames(hot_to_r(input$hottable1)), 
        selected = "",
        multiple = TRUE)
      )

  observeEvent(input$delCol1,{x(x()+1)})
  observeEvent(input$addCol1,{y(y()+1)})
  
  output$clickSelInput <- renderText({paste('Select Input clicks =',x())})
  output$clickAddBtn <- renderText({paste('Add Button clicks =',y())})
  
}

shinyApp(ui,server)

相关问题