R 表或数据框上的复选框

R 表或数据框上的复选框,r,checkbox,checkboxlist,shiny,R,Checkbox,Checkboxlist,Shiny,如何使用复选框从数据框或表中选择数据行? 我已经完成了以下代码,但复选框项似乎是列,并没有真正显示结果 谢谢你的帮助 服务器.R 举个例子。请注意,我是如何设置标签的,并使用为checkboxGroupInput、choose_row指定的id作为output$data_表的输入参数的。这可以替换服务器.R文件 您好,您可以尝试package ReporteRs,这里有一个用于创建html表或word表的函数FlexTable,例如: library("shiny") library("Repo

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

谢谢你的帮助

服务器.R
举个例子。请注意,我是如何设置标签的,并使用为checkboxGroupInput、choose_row指定的id作为output$data_表的输入参数的。这可以替换服务器.R文件

您好,您可以尝试package ReporteRs,这里有一个用于创建html表或word表的函数FlexTable,例如:

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对象的更多信息,您可以查看。

添加一些HTML标记并使用`DT::datatableescape=F显示。 下面的代码很容易解释

是一个复选框标记。 DT::datatableescape=F构建一个html页面。 {r} 图书馆管理员 蒂布尔 x='这是一个复选框' %>% DT::datatableescape=F


你没抓住重点。我想在侧面板上以表格的形式显示所有数据,每行都有一个复选框,而不是名称行。选定的行在主面板上显示为一个表。我有几乎完全相同的问题-你明白了吗?我正在使用renderable呈现R data.frame,我希望表中有一列复选框,单击该复选框时,将R data.frame列中的相应条目更改为TRUE或FALSE,具体取决于当前是否选中该复选框。找不到任何关于如何实现这一点的例子。不是真的。还没有真正弄明白是不是可以默认检查?@fifthace绝对可以,只需将语法更改为checked,这是参考,
   shinyUI(pageWithSidebar(
         headerPanel(""),

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

        mainPanel(
        wellPanel("Data", tableOutput("data_table")
    ))))
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, ]
  })
})
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)