[英]R shiny: renderTable reset input$tableid_rows_selected value
Background : I'm creating a simple table using the following code: 背景:我正在使用以下代码创建一个简单的表:
output$mytable = DT::renderDataTable({
datatable(data_frame_object, selection='single')
})
Note that selection='single'
makes it so that only one row is shown as selected at a time. 请注意, selection='single'
使得每次只显示一行被选中。 I track the row that is selected by the user by using the following code: 我使用以下代码跟踪用户选择的行:
index=input$mytable_rows_selected
Problem : The problem I am having is that I want to reset the value of index=input$mytable_rows_selected
every time a new row is selected. 问题:我遇到的问题是,每次选择新行时,我都想重置index=input$mytable_rows_selected
的值。 Currently, if user selects row 1, the index will have value [1]. 当前,如果用户选择第1行,则索引将具有值[1]。 If user then selects row 2, then index will have value of [1, 2]. 如果用户随后选择第2行,则索引的值为[1、2]。 However, I just want index to have value of [2]. 但是,我只希望索引的值为[2]。
Attempted solution: My workaround has been to use index[length(index)] to get the row that was last selected,but this is not suitable for my purposes. 尝试的解决方案:我的解决方法是使用index [length(index)]获取上次选择的行,但这不适合我的目的。
Working example using iris data: 使用虹膜数据的工作示例:
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')))
)
)
)
Try this code 试试这个代码
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.