r - 在 Shiny 中仅从复选框组获取一次值

标签 r shiny

我编写了以下程序:

ui.r

library(shiny)
shinyUI(fluidPage(
    mainPanel(
    textOutput("text1"),
    checkboxGroupInput("checkGroup", 
                       label = h3("Alternatives"), 
                       choices = list("A" = 1, 
                                      "B" = 2, 
                                      "C" = 3,
                                      "D" = 4),
                       selected = NULL),
    actionButton("action", label = "Next")
    )
))

服务器.r

library(shiny)
shinyServer(function(input, output) {
  observe({
    if(input$action>0 & input$action<=2){
      valores<-renderText({
        input$checkGroup
      })
      data<-unlist(strsplit(valores(), split=" "))
      print(data)
    }
  })
})

我已经设法捕获所选的值并将它们放入向量中,因此我解决了之前遇到的问题。

我现在遇到的问题是,它始终输出我从复选框组中选择或取消选择的值,但我只想捕获按下“下一步”按钮后选择的最后一个值。

有什么帮助吗?我查过文档,但是很少。

最佳答案

这是剥猫皮的一种方法。

我们将 isolatereactiveactionButton 一起使用,以确保在按下按钮之前不会评估任何内容。通过将输入本身与任何进一步的格式隔离,我们可以出于任何目的多次调用它。请注意,如果没有隔离,它将在输入更改时进行评估。

服务器.R

library(shiny)
shinyServer(function(input, output) {

  # store as a vector to be called wherever desired
  # evaluated whenever inputs change
  datasetInput <- reactive({
    perm.vector <- as.vector(input$checkGroup)
    perm.vector
  }) 

  # call as a text output
  output$textOut <- renderText({
    input$action # makes sure nothing moves till the button is hit
    # isolate prevents datasetInput from reactively evaluating
    isolate(datasetInput()) 
  })

  # call in a different place as a data table output
  output$dataTableOut <- renderDataTable({
    input$action # makes sure nothing moves till the button is hit
    isolate(df <- data.frame(inputs = datasetInput()))
    df   
  }, options = list(
    lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
    pageLength = 15)
  )

})

ui.R

library(shiny)
shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("checkGroup", 
                         label = h3("Alternatives"), 
                         choices = list("A" = 1, 
                                        "B" = 2, 
                                        "C" = 3,
                                        "D" = 4),
                         selected = NULL),
      actionButton("action", label = "Next")
    ),

    mainPanel(
      h4("Print as text"), textOutput("textOut"),
      h4("Print as data table"), dataTableOutput("dataTableOut")
    )
  )
))

enter image description here

关于r - 在 Shiny 中仅从复选框组获取一次值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282524/

相关文章:

r - 一种快速计算所有点之间距离的方法

r - 将 R ggplot 中密度图中的 y 轴归一化为按组比例

r - 卡住数据表中的页眉和页脚 - Shiny

r - 选择 dataTable 中的行以显示 Shiny 中另一个表中的数据

r - Shiny 的 : run code only once after button clicked

r - 如何使用自定义列名称将数据框转置为 Shiny

regex - R从字符串中提取第一个数字

r - 如何使用库(插入符号)更改指标?

重置 Shiny 的 react 计时器

r - ddply 总结字符串操作