r - 当我编辑反应式 DT 时,DT 输出将我带到第一页
问题描述
我在下面有我的代码的玩具版本。我闪亮的 DT 输出中的一列(第 7 列)是可编辑的。当我编辑列的单元格时,它会将我带回到列的第一行。我检查了环境中的数据表对象,编辑的单元格确实得到了更新。所以,这很好。但我想在编辑单元格后留在同一页面上。这是因为用户可能已经应用了一些过滤器来到达某个页面,然后当他编辑一个单元格时,他想从那里继续而不是返回开始。
我是 R 新手,所以任何帮助都将不胜感激。
我正在使用 DT 0.7
我的数据框有 7 列:大陆、州、国家、日期、速率(污染)、车辆、备注(可编辑列)
用户可以通过选择输入、范围和滑块输入来过滤表格输出。我想让该输出可编辑。
提前致谢!
library(shiny)
library(DT)
ui <- navbarPage("Hello",
tabPanel("Tab1",
sidebarLayout(
sidebarPanel( width = 4,
selectInput("continent", "Select:",
choices = ""),
selectInput("country" , "Select:",
choices = ""),
selectInput("state" , "Select:",
choices = ""),
dateRangeInput("date", "Select:",
startview = "month",
minview = "months",
maxview = "decades",
start = as.Date('1999-01-01'),
end = as.Date(today()),
separator = "-"),
sliderInput("rate", "Select:",
min = 1, max = 5, value = c(1,5),
dragRange = TRUE)),
mainPanel(
tabsetPanel(
tabPanel("Analysis",
dataTableOutput("Table1")
)))))
#server
server <- function(input, output, session)
{
observe({
updateSelectInput(session, "continent",
choices = c("All", unique(Df$Continent)))
})
observe({
updateSelectInput(session, "country",
choices = c("All", Df %>%
filter(`Continent` == input$continent) %>%
select(Country)))
})
observe({
updateSelectInput(session, "state",
choices = c("All", Df %>%
filter(`Continent` == input$continent &
`Country` == input$country) %>%
select(State)))
})
#create reactive table
RecTable <- reactive({
Df
if(input$continent != "All") {
Df <- Df[Df$Continent == input$continent,]
}
if(input$country != "All") {
Df <- Df[Df$Country == input$country,]
}
if(input$state != "All") {
Df <- Df[Df$State == input$state,]
}
Df <- Df %>%
filter(Date >= input$date[1] & Date <= input$date[2]) %>%
filter(Rate >= input$rate[1] & Rate <= input$rate[2])
Df})
output$Table1 <- DT::renderDT({
DT::datatable(RecTable(),
rownames = FALSE ,
editable = list(target = 'cell', disable = list(columns = c(0:6))))
})
proxy1 <- dataTableProxy('Table1')
observeEvent(input$Table1_cell_edit, {
Df <<- editData(Df, input$Table1_cell_edit, 'Table1', rownames = FALSE, resetPaging = FALSE)
})}
#run
shinyApp(ui = ui, server = server)
解决方案
推荐阅读
- php - Laravel livewire 存储数组无法存储数据
- sql - BigQuery:多个 JOIN 条件下的 WITH 子句行为
- react-native - 如何访问在特定端口上运行的 gitpod react-native locallink?
- reactjs - React axios Post错误,数据格式错误
- javascript - 使用 Discord JS 创建角色
- c - 将 While 循环转换为 For 循环
- azure-devops - 如何在外部 API 中验证 System.Accesstoken 在 Auth 标头中使用时?
- asp.net - 如何在 Asp.Net 中的数据绑定表达式中防止 XSS
- html - 我可以让我的卡片中的孩子都拥有其中最大的身高吗?
- python - 如何将我从以前的算法中获得的多个数值添加到数组中,以便在 MU python 编辑器中为 microbit 进行一些计算?