来自 renderDataTable 的反应式输入 [英] Reactive input from renderDataTable

查看:70
本文介绍了来自 renderDataTable 的反应式输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新(2015 年 12 月 18 日)此问题的当前最佳方法是:

感谢您的帮助.@Vincent,你的应用程序很酷.希望最终能到达那里.我会接受你的回答.

解决方案

不是答案,而是冗长的评论:我认为您可以这样做,但会有点混乱.一旦有人选择了一个单选按钮并显示了数据的子集,您如何返回显示所有数据?这只适用于第一列吗?当数据行数较少时,您是否要重新创建所有单选按钮?renderDataTable 将(在某些时候)可以选择在每列下方的框中包含诸如> 20"之类的内容.与此同时,您最好尝试使用 textInput 对数据进行子集化.例如,请参阅此 应用 中的数据 > 查看.如果这不是您想要的,请让我(我们)知道您想要实现的目标.

UPDATE (18-Dec-2015) The currest best apporach for this problem is here: Get Selected Row From DataTable in Shiny App

======================

I am trying to reactively re-display a dataset using a row selection provided by the user. As a toy example,

ui.R

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel('Examples of DataTables'),
  sidebarPanel(
    checkboxGroupInput('show_vars', 'Columns to show:', names(mtcars),
                       selected = names(mtcars))
  ),
  mainPanel(
               dataTableOutput("mytable")
    )
  )
)

server.R

library(shiny)

shinyServer(function(input, output) {

  # sorted columns are colored now because CSS are attached to them
  output$mytable = renderDataTable({
    addRadioButtons <- paste0('<input type="radio" name="row', 1:nrow(mtcars), '" value="', 1:nrow(mtcars), '">',"")
    #Display table with radio buttons
    cbind(Pick=addRadioButtons, mtcars[, input$show_vars, drop=FALSE])
  }, options = list(bSortClasses = TRUE, aLengthMenu = c(5, 25, 50), iDisplayLength = 25))

})

I would like to use the radio button to collect the row number (say, row 4 in image above) from the user using some reactive expression, say rowSelect(), and then dynamically re-display the table following some operation on the dataset, for example,

 mtcars[mtcars[,1] > mtcars[rowSelect(),1], input$show_vars, drop=FALSE]

which would drop certain rows dynamically every time the user selects a row.

UPDATE (7-feb-14)

Incorporating the changes suggested by @Julien and implementing the method suggested by @Vincent, server.R becomes:

library(shiny)

shinyServer(function(input, output) {

  #reactive row-selection 
  rowSelect <- reactive({
  if(is.null(input[["row"]])) 1 #initialize
  else as.numeric(input[["row"]])})

  # User-selected sorting of dataset
  output$mytable = renderDataTable({
    addRadioButtons <- paste0('<input type="radio" name="row" value="', 1:nrow(mtcars), '">')
    cbind(Pick=addRadioButtons, mtcars[order(mtcars[,1] - rnorm(nrow(mtcars), mtcars[rowSelect(),1])), input$show_vars, drop=FALSE])
  }, options = list(bSortClasses = TRUE, aLengthMenu = c(5, 10, 20), iDisplayLength = 10))
})

As guessed by @agstudy, it was important to keep the number of rows in the datatable the same as before (not subsetting), hence the weird sorting operation above order(mtcars[,1] - rnorm(nrow(mtcars), mtcars[rowSelect(),1])).

UPDATE 2 (7-feb-14)

Anyway, this exercise does reveal a flaw in my example. My original intent was to use a separate covariance/similarity matrix to get the sorting order for the display datatable based on the user-selected row. Since the matrix and table would be very large, it didn't make sense to include them as drop-down input list or any other radiobutton input method in the side panel. It had to come from user-selection after displaying the whole datatable.

To achieve this, just replace the value in the radiobutton with the unique row ID. In server.R, using the rowSelect() expression (data is the similarity matrix and meta is the datatable displayed, ID is the unique row identifier shared by data and meta):

addRadioButtons <- paste0('<input type="radio" name="row" value=', meta[order(data[rowSelect(),]),"ID"], '>')

cbind(Pick=addRadioButtons, meta[order(data[rowSelect(),]),input$show_vars])

This would keep resorting the datatable based on the user-selected row while choosing the order based on the similarity matrix via a unique row ID. Hope this helps someone.

But, if I use the rowSelect reactive expression, then the row-selection based on a sorted datatable would not give me the correct row-selection for the similarity matrix (the order would have changed for the dataset but not for the matrix - recursion issue). This means that I would need to collect something other than a radio input of row number -- something more like an identifying row ID (which would match for both the table -- one of the columns -- and the matrix) using a form or a click submission -- along the lines of this: Selecting all text in HTML text input when clicked

Thanks for the help. @Vincent, your App is very cool. Hope to get there eventually. I will accept your answer.

解决方案

Not an answer but a longish comment: I think you could do this but it would get a bit messy. Once some selects a radio button and a subset of the data is shown how do you go back to showing all the data? Would this only apply to the first column? Are you going to recreate all the radio buttons when the data has fewer rows? renderDataTable will (at some point) have the option to include something like '> 20' in the boxes below each column. In the meantime you might be better off trying to subset the data with a textInput. As an example, see Data > View in this app. If that is not what you are looking for let me (us) know what you are trying to achieve.

这篇关于来自 renderDataTable 的反应式输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆