Jquery 在RStudio中,希望定制一张桌子
在一个RStudio应用程序中,有一个由Jquery 在RStudio中,希望定制一张桌子,jquery,r,shiny,Jquery,R,Shiny,在一个RStudio应用程序中,有一个由renderable()生成的表,我想添加一个单选按钮的前导列(当然是被动的),并更改所选行的样式。最好的策略是什么?如果绝对必要的话,我想我可以使用jQuery,但是没有更简单的方法吗?我已尝试将html插入到renderTable()表达式参数中的表单元格中。。。不起作用。不确定您是否仍在寻找此问题的答案。也许不是,但看到没有人回答,我很难过。我只需要自己创建html表并使用renderText() 例如,假设您希望页面上的此数据框顶部有单选按钮: d
renderable()
生成的表,我想添加一个单选按钮的前导列(当然是被动的),并更改所选行的样式。最好的策略是什么?如果绝对必要的话,我想我可以使用jQuery,但是没有更简单的方法吗?我已尝试将html插入到renderTable()
表达式参数中的表单元格中。。。不起作用。不确定您是否仍在寻找此问题的答案。也许不是,但看到没有人回答,我很难过。我只需要自己创建html表并使用renderText()
例如,假设您希望页面上的此数据框顶部有单选按钮:
df <- data.frame(A=1:5, B=1:5)
并使用这些功能:
df_rows <- apply(df, 1, row_html)
将生成以下表单的HTML:
<input type="radio" name="row1" value="1">1
现在我们只需要组合df
,单选按钮并将整个内容包装在HTML表格标记中
table_cells <- c(radio_row, df_rows)
collapse_cells <- paste0(table_cells, collapse='')
full_table <- paste0('<table>', collapse_cells, '</table>')
将我的表格呈现到输出$x
。要设置所选行的样式,我建议使用jQuery。一个简单的事件(高度未经测试)[编辑:请参见下面评论中的建议修订]:
$('table input:radio').change(function() {
var index = $('#table input:radio').index(this);
// Add one to skip radio button row.
$('table tr').eq(index + 1).css('background-color', 'blue');
// Also handle reset on other rows
// ...
// ...
});
您也可以尝试将表和一个“selected”类构建到适当的表行服务器端,并使用一些CSS对其进行样式设置
在缺少样本数据的情况下,所有这些都未经测试,因此可能会出现一些错误
此外,如果您乐于使用ui.R
而不是您自己的自定义HTML,那么这种方法应该仍然有效。我只是建议您使用自定义HTML,因为您似乎正在沿着这条路线徘徊
我在回答你的问题。。。i、 e.制作一个单选按钮的前导行。不过我自己可能不会这么做。为什么不使用
renderable()
正常生成表格,并单独添加单选按钮,即完全不属于表格的一部分?请参阅以寻求帮助。如果您必须将单选按钮与表格列对齐,则可以通过一些CSSing来实现 根据@MadScone的绝妙建议,我想出了以下代码,
哪一个是使其适用于我的其他一些功能包括: *单选按钮位于第1列(不是第1行) *他们属于同一个广播集团 *表格标题行的格式正确 *单选按钮选择的行接收特殊格式,而不需要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()})
次要但重要的一点是,我将更改
$('table input:radio')。将(function(){
更改为$(document)。在('change','table input:radio',function(){
上,后者将允许它在服务器重新创建表后继续工作(即每次执行renderText
代码时)。基本上,它将在所有未来的表输入:radio
实例上侦听更改事件,而不仅仅是在执行该行时存在的实例。很好,Joe,这是最近给我带来麻烦的事情。谢谢!这就是我要找的。非常感谢。请澄清一下,数据源用于df也需要反应?
radio_row <- row_html(radios)
table_cells <- c(radio_row, df_rows)
collapse_cells <- paste0(table_cells, collapse='')
full_table <- paste0('<table>', collapse_cells, '</table>')
<div name="x" id="x" class="shiny-html-output"></div>
$('table input:radio').change(function() {
var index = $('#table input:radio').index(this);
// Add one to skip radio button row.
$('table tr').eq(index + 1).css('background-color', 'blue');
// Also handle reset on other rows
// ...
// ...
});
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()})
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
})