【问题标题】:Checkbox on table or dataframe表格或数据框上的复选框
【发布时间】:2013-10-03 05:53:07
【问题描述】:

如何使用复选框从数据框或表格中选择数据行? 我已经完成了以下代码,但似乎复选框项目是列,并没有真正显示结果。

感谢您的帮助。

服务器.R

   shinyServer(function(input, output) {
       dataset<-reactive({
         data(cars)
         cars
       })

       output$choose_data <- renderUI({
         checkboxGroupInput("dtab", "Data Table", dataset()) 
    })

       dataset2<-reactive({
         input$dtab         
    })      

       output$data_table <- renderTable({
         data2()                    
       })
    })

ui.R

   shinyUI(pageWithSidebar(
         headerPanel(""),

         sidebarPanel(
         uiOutput("choose_data"),
         br()
         ),

        mainPanel(
        wellPanel("Data", tableOutput("data_table")
    ))))

【问题讨论】:

  • 我也有几乎完全相同的问题——你明白了吗?我正在使用 renderTable 渲染一个 R data.frame,并且我希望有一列复选框(in 表),单击该复选框会更改 R 数据列中的相应条目。 frame 为 TRUE 或 FALSE,具体取决于当前是否选中了复选框。找不到任何关于如何实现这一点的示例。
  • 并非如此。还没搞清楚呢

标签: r checkbox checkboxlist shiny


【解决方案1】:

你好,你可以试试包ReporteRs,有一个函数FlexTable用于创建html表(或word表),一个例子:

library("shiny")
library("ReporteRs")
mymtcars <- head(mtcars)

# ui
ui <- fluidPage(
  tags$h1("Table with checkboxes"),
  tableOutput(outputId = "table"),
  br(),
  verbatimTextOutput(outputId = "out")
)
# server
server <- function(input, output) {
  output$table <- renderFlexTable({
    # Create checkboxes
    mymtcars$Name <- paste0('<label><input type="checkbox" id="car', seq_along(rownames(mymtcars)), '"> <span>', rownames(mymtcars), '</span></label>')
    mymtcars <- mymtcars[c("Name", names(mtcars))] # Put col 'Name' in the first place
    ft <- vanilla.table(mymtcars) # convert to FlexTable objet
    ft[, "Name", to = "header"] <- parLeft() # left align checkboxes
    ft[, "Name"] <- parLeft() # left align header
    return(ft)
  })
  # the inputs created are in input$car1, input$car2, ...
  output$out <- renderPrint({
    # results
    res <- unlist(lapply(1:nrow(mymtcars), function(i) input[[paste0("car", i)]]))
    print(res)
    if (any(res)) {
      print(rownames(mymtcars)[res])
    }
  })
}
# launch app
shinyApp(ui = ui, server = server)

结果如下:

有关 FlexTable 对象的更多信息,您可以查看here

【讨论】:

    【解决方案2】:

    一个例子。注意我是如何设置标签的,并使用为checkboxGroupInputchoose_row 指定的id 作为output$data_table 的输入参数。这可以替换您的 server.R 文件。

    shinyServer(function(input, output) {
    
      data(cars)
      cars <- cars[1:10,] ## limit ourselves to first 10 rows for example
    
      ## a UI element that we can use for selecting rows
      output$choose_data <- renderUI({
        labels <- setNames( as.list(1:nrow(cars)), paste("Row", 1:nrow(cars)) )
        checkboxGroupInput("choose_row", "Select Rows", labels)
      })
    
      ## render the data.frame at the selected rows
      output$data_table <- renderTable({
        cars[input$choose_row, ]
      })
    })
    

    【讨论】:

    • 你没有抓住重点。我想将所有数据显示为侧面板上的表格,每行都有一个复选框(不是名称 Row)。选定的行在 mainPanel 上显示为表格。
    【解决方案3】:

    添加一些 HTML 标签并使用 `DT::datatable(escape = F) 来显示。 下面的代码很容易解释。

    1. &lt;input type="checkbox" id="checkbox1" class="styled"&gt; 是一个复选框标签。
    2. DT::datatable(escape = F) 构建一个 html 页面。

    {r} library(tidyverse) tibble( x = '<input type="checkbox" id="checkbox1" class="styled">This is a checkbox' ) %>% DT::datatable(escape = F)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2017-02-15
    • 2016-06-18
    • 2021-08-14
    • 2016-05-30
    • 2010-11-24
    • 2020-03-22
    相关资源
    最近更新 更多