[英]R shiny: renderTable reset input$tableid_rows_selected value
背景:我正在使用以下代碼創建一個簡單的表:
output$mytable = DT::renderDataTable({
datatable(data_frame_object, selection='single')
})
請注意, selection='single'
使得每次只顯示一行被選中。 我使用以下代碼跟蹤用戶選擇的行:
index=input$mytable_rows_selected
問題:我遇到的問題是,每次選擇新行時,我都想重置index=input$mytable_rows_selected
的值。 當前,如果用戶選擇第1行,則索引將具有值[1]。 如果用戶隨后選擇第2行,則索引的值為[1、2]。 但是,我只希望索引的值為[2]。
嘗試的解決方案:我的解決方法是使用index [length(index)]獲取上次選擇的行,但這不適合我的目的。
使用虹膜數據的工作示例:
library(shiny)
library(DT)
server <- function(input, output, session) {
output$mytable = DT::renderDataTable({
datatable(iris[,c(1,4,5)],selection='single')
},
options = list(lengthMenu = c(5, 30, 50), pageLength = 10, orderClasses=TRUE)
)
output$info = DT::renderDataTable({
index=input$mytable_rows_selected
if (length(index)){
index2=index[length(index)]
}
else{
index2=index
}
iris[index2,c(1,4)]
},
options = list(pageLength = 5, orderClasses=TRUE, searching=FALSE)
)
}
ui <- fluidPage(
fluidRow(
column(4,dataTableOutput('mytable')),
column(6,offset=1,
tabsetPanel(type="tabs",
tabPanel("hi",dataTableOutput('info')))
)
)
)
試試這個代碼
library(shiny)
library(DT)
server <- function(input, output, session) {
output$mytable = DT::renderDataTable({
datatable(iris[,c(1,4,5)],selection='single')
},
options = list(lengthMenu = c(5, 30, 50), pageLength = 10, orderClasses=TRUE)
)
selected.rows <- reactiveValues(index = NULL)
observe({
selected.rows$index <- input$mytable_rows_selected
})
output$info = DT::renderDataTable({
index=selected.rows$index
if (length(index)){
index2=index[length(index)]
}
else{
index2=index
}
iris[index2,c(1,4)]
},
options = list(pageLength = 5, orderClasses=TRUE, searching=FALSE)
)
}
ui <- fluidPage(
fluidRow(
column(4,dataTableOutput('mytable')),
column(6,offset=1,
tabsetPanel(type="tabs",
tabPanel("hi",dataTableOutput('info')))
)
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.