如何在R Shiny中选择反应数据集中的某些行

时间:2016-09-07 10:26:31

标签: r shiny dt

我有被动数据react$data,我有两个输入input$chosencolumninput$chosenrows

使用反应数据集,我如何能够指定我想要的数据行所在的行data[data$chosencolumn == chosenrows,]

可重复的例子:

server.R

### Start of Shiny server
shinyServer(function(input, output, session) {

  reactdata <- reactiveValues()
  observe({
    if(is.null(input$fileinput)){return(NULL)}
    else{reactdata$inputdata <-  read.xlsx(input$fileinput$datapath, header=T, sheetIndex = 1)}
  })



  output$selectsamples <- renderUI({
    if(is.null(input$fileinput)){return(NULL)}
    selectInput("selectsamples",
                label = h5("Samples"), choices = colnames(reactdata$inputdata),
                selected="Sample")
  })

  output$sampleselected <- renderUI({
    if(is.null(input$fileinput)){return(NULL)}
    selectInput("sampleselected",
                label = h5("sampleselected"), choices = unique(as.character(reactdata$inputdata[,input$selectsamples])),
                selected="B")
  })

  output$selectdilutions <- renderUI({
    if(is.null(input$fileinput)){return(NULL)}
    selectInput("selectdilutions",
                label=h5("Select Dilutions"),
                choices = colnames(reactdata$inputdata),
                selected="Dilution")
  })


  reactdata1 <- reactiveValues()
  observe({
    reactdata1$datatable1  <- datatable(reactdata$inputdata,
              rownames = TRUE,
              options = list(pageLength = 100, dom = 'tip'))

  })


  output$datatable1 <- renderDataTable({
   reactdata1$datatable1
  })

})

ui.R

require(shiny)
require(devtools)
require(grDevices)
require(xlsx)
require(DT)


shinyUI(fluidPage(
  navbarPage("",inverse = FALSE,
             tabPanel("Analyse")),
  titlePanel(""),
  fluidRow(
    column(3,
           wellPanel(
             fileInput("fileinput", label = h5("Input file")),
             uiOutput("selectsamples"),
             uiOutput("sampleselected"),
             uiOutput("selectdilutions")
           )),

    column(9,
           fluidRow(
             wellPanel(
                  uiOutput("sample1"),
                  dataTableOutput("datatable1"))

              )))
    )
)

我想更改reactdata1$datatable1,以便它只包含所选样本选择的数据行(即选择输入$ sampleselected的值)。

所以,像reactdata1$datatable1[input$selectsamples == input$sampleselected,]

示例数据集在此处: Dropbox link to excel file

1 个答案:

答案 0 :(得分:0)

以下是根据动态输入的用户输入对活动数据框进行子集的一般示例:

require(shiny)

ui <- shinyUI(fluidPage(    
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "Choose a dataset:", 
                  choices = c("rock", "pressure", "cars","DNase","iris")
      ),
      selectizeInput(
        'colName', 'Select Column: ', list(), multiple = TRUE
      ),
      selectizeInput(
        'rowName', 'Select Rows', list(), multiple = TRUE
      )
    ),
    mainPanel(
      tableOutput('tbl')
    )
  ) #end sidebar layout
))

server <- shinyServer(function(input, output, session) {

  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars,
           "DNase"=DNase,
           "iris"=iris)
  })

  # Update UI
  observe({
    updateSelectizeInput(session, "colName", choices = colnames( datasetInput() ))  
    updateSelectizeInput(session, "rowName", choices = rownames( datasetInput() ))  
  })

  # Create reactive data by subseting the reactive dataset
  r1 <- reactive({
    v <- input$colName %in% colnames(datasetInput())
    if( sum(v == FALSE) > 0) return() # Check for missmatching datasetInput names and column names
    if(is.null(input$colName) || is.null(input$rowName)) return() # None selected, return empty

    # Subset data
    datasetInput()[as.numeric(input$rowName), input$colName, drop=FALSE]
  })

  output$tbl <- renderTable({ 
    r1()
  })
})

shinyApp(ui, server)