代码之家  ›  专栏  ›  技术社区  ›  geodex

表或数据帧上的复选框

  •  9
  • geodex  · 技术社区  · 11 年前

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

    谢谢你的帮助。

    服务器.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()                    
           })
        })
    

    单位:R

       shinyUI(pageWithSidebar(
             headerPanel(""),
    
             sidebarPanel(
             uiOutput("choose_data"),
             br()
             ),
    
            mainPanel(
            wellPanel("Data", tableOutput("data_table")
        ))))
    
    3 回复  |  直到 11 年前
        1
  •  4
  •   Victorp    9 年前

    嗨,你可以试试套餐 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)
    

    结果如下所示:

    ft_example

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

        2
  •  0
  •   Kevin Ushey    11 年前

    一个例子。注意我是如何设置标签的,并使用为 checkboxGroupInput , choose_row ,作为的输入参数 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, ]
      })
    })
    
        3
  •  0
  •   Jiaxiang    6 年前

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

    1. <input type="checkbox" id="checkbox1" class="styled"> 是一个复选框标记。
    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)

    enter image description here