【问题标题】:Create a grid with checkboxes in R shiny在 R Shiny 中创建一个带有复选框的网格
【发布时间】:2019-01-15 16:20:17
【问题描述】:

我正在尝试创建一个网格,它的行名和列名作为特定数据集中的变量,以便我可以在后端的 r 中映射各个行以进行回归(其中行名将是因变量,列名将是自变量)。任何人都可以帮助解决这个问题。

I am trying to get something like this on my app. when a user selects lets say app and alerts and when he presses the submit button it does the regression on the backend i.e website ~ app + alerts

我现在没有任何代码,因为我对 r shiny 很陌生,不知道如何使用复选框创建此网格。如果有人能指导我如何处理这将非常有帮助。

谢谢

【问题讨论】:

  • 它太宽泛了。您还应该提供一个包含您迄今为止尝试过的代码的数据集。

标签: r shiny shinyjs shiny-reactivity


【解决方案1】:

您可以使用DT 包创建带有复选框(或其他活动元素)的表格。请看下面的代码(基于"Showing as Checkbox" anwser:

library(shiny)
library(DT)

# create data.frame of row/column names of futre datable
my_df <- data.frame(nam = c("Favorite1", "Favorite2"))

runApp(
  list(
    ui = fluidRow(
      sidebarLayout(
      h2("Checkboxes Datatable"),
      DT::dataTableOutput("mytable", width = "1%")),
      mainPanel(h2("Selected"),
      tableOutput("checked"))
    ),
    server = function(input, output) {

    # helper function for making checkbox
    shinyInput <- function(FUN, len, id, ...) { 
      inputs <- character(len) 
      for (i in seq_len(len)) { 
        inputs[i] <- as.character(FUN(paste0(id, i), label = NULL, ...)) 
      } 
      inputs 
    } 
    # datatable with checkbox
    output$mytable <- DT::renderDataTable( 
      expr = {
        df <- data.frame(
        #  my_df,
          my_df,
          Favorite1 = shinyInput(checkboxInput, nrow(my_df), "cbox1"), 
          Favorite2 = shinyInput(checkboxInput, nrow(my_df), "cbox2")
        )
        names(df)[1] <- " "
        df
      }, 
      rownames = FALSE,
      server = FALSE, 
      escape = FALSE, 
      options = list(
        ordering = FALSE,
        searching = FALSE,
        paging = FALSE,
        info = FALSE,
        preDrawCallback = JS("function() { 
          Shiny.unbindAll(this.api().table().node()); }"
        ), 
        drawCallback = JS("function() { 
          Shiny.bindAll(this.api().table().node()); } "
        ) 
      )
    )

    # helper function for reading checkbox
    shinyValue <- function(id, len) { 
      unlist(
        x = lapply(
          X = seq_len(len), 
          FUN = function(i) { 
            value = input[[paste0(id, i)]] 
            if (is.null(value)) {
              NA
            } else {
              value
            }  
          }
        )
      ) 
    } 
    # output read checkboxes
    output$checked <- renderTable({
      data.frame(
        Favorite1 = shinyValue("cbox1", nrow(my_df)),
        Favorite2 = shinyValue("cbox2", nrow(my_df))
      )
    }
  )
 }
  )
)

输出(在上方表格中单击的复选框在下方显示其状态):

【讨论】:

  • 非常感谢您的帮助。我只是在寻找这样的东西。
猜你喜欢
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
相关资源
最近更新 更多