在RStudio Shiny中,希望自定义表格 [英] in RStudio shiny, wishing to customize a table

查看:208
本文介绍了在RStudio Shiny中,希望自定义表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有由renderTable()生成的表的RStudio闪亮应用程序中,我想添加单选按钮的前导列(当然是活动的)并更改所选行的样式.最好的策略是什么?我认为如果绝对必要,我可以使用jQuery,但是不是更简单的方法吗?我尝试将html插入renderTable()表达式args内的表单元格中...

In an RStudio shiny application with a table produced by renderTable(), I'd like to add a leading column of radio-buttons (reactive, of course) and change the style of the selected row. What's the best strategy? I think I can use jQuery if absolutely necessary, but isn't there a simpler way? I've tried inserting html into table cells within the renderTable() expression args... doesn't work.

推荐答案

追求@MadScone的出色建议,我想到了以下代码, 这是
的最终解决方案 使它对我有用的一些其他功能是: *单选按钮位于第1列(而不是第1行) *它们属于同一广播组 *表标题行的格式正确 *单选按钮选择的行将接受特殊格式,而无需jQuery.

Pursuing @MadScone 's excellent advice, I came up with the following code, which is the definitive solution to
Some additional features that make it work for me are: * the radio buttons are in column 1 (not row 1) * they belong to the same radio group * the table header row is properly formatted * the row selected by radio button receives special formatting, without needing jQuery.

values = reactiveValues(PopRow=1)   ### To receive and hold the selected row number.

f.objects_table_for_OneCT = function(){
    f.changeSelectedRow()        #### See definition below.
    df = createObjectsTable()    #### Any data frame goes here; code not provided here.
    selectedRow = values$PopRow
    header_html <- function(table_cell) paste0('<th>', table_cell, '</th>')
    cell_html <- function(table_cell) paste0('<td>', table_cell, '</td>')
    radio_html <- function(radio_name, radio_value, is_checked, radio_text) {
      paste0('<input type="radio" name="', 
             radio_name, '" value=', radio_value, 
                   ifelse(is_checked, " checked ", ""),
                   '>', radio_text)
    }    
    row_html <- function(table_row_num) {
      table_row = df[table_row_num, ]
      cells <- sapply(table_row, cell_html)
      cells <- c(cell_html(radio_html(
                  "whichRow", table_row_num, table_row_num == selectedRow, "")),
               cells)
      collapse_cells <- paste0(cells, collapse='')
      selectedRowStyle = "style='color:red; font-weight:bold'"
      collapse_cells <- paste0('<tr ', 
                     ifelse(table_row_num == selectedRow, selectedRowStyle, ""),
                    '>', collapse_cells, '</tr>')
      collapse_cells 
    }
    df_rows <- sapply(1:nrow(df), row_html) 
    df_header_row <- header_html(c("CHOICE", names(df)))
    collapse_cells <- paste0(c(df_header_row, df_rows), collapse='')    
    full_table <- paste0('<table class=\"data table table-bordered table-condensed\">', 
                         collapse_cells, '</table>')
    return(full_table)
  }

  output$objects_table_for_OneCT = renderText({f.objects_table_for_OneCT()})

(关于最后一行,我习惯性地将expr arg包装在一个函数中,所以我可以debug.到目前为止,它工作正常.)

(Concerning the last line, I habitually wrap my expr arg in a function, so I can debug. So far it's worked fine.)

响应单选按钮的功能如下:

The function that responds to the radio buttons is as follows:

  f.changeSelectedRow = reactive({
    if(is.null(values$PopRow)) values$PopRow = 1
    if(!is.null(input$whichRow))   ### from the radio button set.
             if(input$whichRow != values$PopRow) values$PopRow = input$whichRow
  })

这篇关于在RStudio Shiny中,希望自定义表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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