你基本上可以做閃亮的幾乎所有的東西,因爲你可以創建自己的input和output綁定–所以回答你的問題是肯定的,你要問是可能的。假設你有一個你發送到網頁的數據框供用戶查看。舉個例子,你想允許用戶只需單擊單元格,如果它是應該被刪除的異常值(與NA
代替)。
假設數據幀是這樣的:
x <- data.frame(Age = c(10, 20, 1000), Weight = c(120, 131, 111))
x
# Age Weight
# 10 120
# 20 131
# 1000 111
從閃亮的你將構建的網頁上顯示時可能看起來像這樣一個普通的HTML表:
<table class="outlier-finder" id="outliers">
<tr>
<td>Age</td>
<td>Weight</td>
</tr>
<tr>
<td>10</td>
<td>120</td>
</tr>
<tr>
<td>20</td>
<td>131</td>
</tr>
<tr>
<td>1000</td>
<td>111</td>
</tr>
</table>
現在打破出了jQuery和綁定點擊事件,以便在單擊單元格時,你可以記錄的行和列數(see here)然後更換電池與閃亮NA
。你輸入綁定可能看起來像(see here for details of what's going on here):
$(document).on("click", ".outlier-finder td", function(evt) {
// Identify the clicked cell.
var el = $(evt.target);
// Raise an event to signal that the something has been selected.
el.trigger("change");
});
var cell_binding = new Shiny.InputBinding();
$.extend(cell_binding, {
find: function(scope) {
return $(scope).find(".outlier-finder td");
},
getValue: function(el) {
// Get the row and cell number of the selected td.
var col = el.parent().children().index(el);
var row = el.parent().parent().children().index(el.parent());
var result = [row, col];
return result;
},
setValue: function(el, value) {
$(el).text(value);
},
subscribe: function(el, callback) {
$(el).on("change.cell_binding", function(e) {
callback();
});
},
unsubscribe: function(el) {
$(el).off(".cell_binding");
}
});
Shiny.inputBindings.register(cell_binding);
有很多這裏發生,但通常這些輸入綁定是相當彼此相似。最重要的是setValue()
功能。什麼應該發生的事情(這是未經測試)是被點擊的單元格的行數和列數被記錄並回發到服務器。
然後從閃亮的你只會做這樣的事情:
updateData <- reactive({
# Get selection
remove_outlier <- as.integer(RJSONIO::fromJSON(input$outliers))
if (!is.null(remove_outlier)) {
# Remove that outlier.
x[remove_outlier[1], remove_outlier[2]] <- NA
}
return(x)
})
output$outliers <- renderText({
# Update x.
current_x <- updateData()
# Write code to output current_x to page.
# ...
# ...
})
你可能會需要做的輸出以及輸出$離羣結合。這是簡化的代碼在這裏很明顯,你將需要申請錯誤檢查等
這僅僅是一個例子。實際上,當用戶每次更改數據時,您都可能不會Shiny更新數據框。你會想要某種提交按鈕,以便一旦用戶完成了他/她的所有更改,就可以應用它們。
我還沒有遠程測試任何這,這樣幾乎肯定一些錯誤。但既然你只是在問一個理論問題,我沒有太多的檢查。無論如何,一般的策略都應該起作用。使用輸入綁定,您可以從網頁獲取任何內容返回到服務器,反之亦然,輸出綁定。也許說什麼「什麼」是一個延伸—但你可以做很多事情。
我想難度是有一個可編輯的數據表,我不知道現有的一個。一旦我們有這樣一個顯示和可編輯的數據表,信息可以傳回服務器。 – Zhenglei