使用R在数据表中选择下拉和键控文本列后获取数据表记录

时间:2017-12-28 06:27:24

标签: r shiny

如何在点击“更新表格”操作按钮,选择下拉列和用户输入的文本后,获取数据表记录的数据?下拉选择中的任何更改都应立即刷新数据表记录,从而使其成为被动的。

library(shiny)
library(shinyjs)
library(DT)
library(data.table)

shinyInput <- function(FUN, len, id, ...) {
  inputs <- character(len)
  for (i in seq_len(len)) {
    inputs[i] <- as.character(FUN(paste0(id, i), ...))
  }
  inputs
}

mydata = data.frame(id=letters[1:5], val=sample(10,5,T))
lengthofData <- nrow(mydata)
mydata[["Constraint Type"]] <-  c(shinyInput(inputId = "constraintType",
                                             selectInput,
                                             lengthofData,
                                             "d",
                                             label = "",
                                             choices = c(">", "<")
))

mydata[["Constraint Value"]] <- c(shinyInput(textInput, lengthofData, "t", label = ""))

ui = fluidPage(dataTableOutput("table"),
               actionButton("goButton", "Update Table"),
               dataTableOutput("newtable"))

server = function(input,output){
  output$table <- renderDataTable( df(),server = FALSE,
                                   escape = FALSE,
                                   selection = 'none',
                                   options = list(
                                     sDom  = '<"top">lrt<"bottom">ip',
                                     rowCallback = JS("function(r,d) {$(r).attr('height', '10px')}"),
                                     #columnDefs = list(list(width = '200px', targets = "_all")),
                                     scrollY = '50vh',
                                     paging = TRUE,
                                     autoWidth = TRUE
                                   ))
  newTableData <- reactive({
    return(mydata)
  })

  df <- eventReactive(input$goButton, {
    mydata
  }, ignoreNULL = FALSE)

  output$newtable <- renderDataTable({
    DT::datatable(newTableData(),rownames = FALSE,
                  extensions = c('Responsive', 'Buttons'))
  })
}

shinyApp(ui,server)

UI的屏幕截图 UI screenshot

0 个答案:

没有答案