【问题标题】:How to select certain rows in a reactive dataset in R Shiny如何在 R Shiny 的反应性数据集中选择某些行
【发布时间】:2017-01-14 23:53:10
【问题描述】:

我有反应数据react$data,我有两个输入input$chosencolumn,input$chosenrows

使用反应式数据集,我如何能够指定我想要的行,比如你在其中执行 data[data$chosencolumn == chosenrows,] 的 data.frame

可重现的例子:

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,使其仅包含所选样本选择的数据行(即 input$sampleselected 被选择为的值)。

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

这里有一个示例数据集: Dropbox link to excel file

【问题讨论】:

  • 可重现的例子会有所帮助
  • 我加了一个。
  • 看起来与data.table无关

标签: r shiny dt


【解决方案1】:

这是一个通用示例,您可以根据动态输入的用户输入对反应式 data.frame 进行子集化:

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)

【讨论】:

  • 谢谢你,看到这项工作很有趣。虽然我仍然无法让它为我工作,但我想我会继续尝试它。我不太明白 as.numeric 的用途。
  • 尝试运行?as.numeric,它将字符向量从input$rowNames转换为数字向量。
  • 对不起,我的意思是,你为什么要把它转换成数字?我设法让你的榜样以类似的方式为我工作; datainput()[datainput()[,input$column, drop=FALSE] == input$row,]
  • 因为selectizeInput 将选定的值作为字符串返回。看起来不错,但 datainput()[input$row, input$column, drop=FALSE] 不适合吗?
  • 我不知道为什么,但没有,它返回并清空数据集。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 2019-11-11
  • 2016-07-12
  • 2016-05-31
相关资源
最近更新 更多