DT错误:数据更改时保留先前选择的行

时间:2018-10-24 19:46:50

标签: r shiny dt

在闪亮时,DT会在数据源更改时保留先前选择的行。在下面的代码中,当您从表中选择行然后更改下拉值时,它仍将返回先前选择的行的索引(来自先前的下拉值)。这似乎是DT库中的错误。我一无所知。我想存储所有选定的行,然后根据选定的行在valueboxoutput中显示总和。还可以选择保留蓝色的行吗?

library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)

# FETCH DATA
mydata = mtcars
mydata$id = 1:nrow(mydata)

#Dashboard header carrying the title of the dashboard
header <- dashboardHeader(title = "My Dashboard")

######################
# Dashboard Sidebar
######################

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    selectInput(
      "hyp",
      "Select:", 
      list(
        'All','drat','wt'
      ) , 
      selected =  "All", selectize = TRUE)
  )
)

# Dashboard Body

frow1 <- fluidRow(
  valueBoxOutput("value1")
)





frow2 <- fluidRow(
  box(DT::dataTableOutput("mytable"), width = 12)
)

# combine the two fluid rows to make the body
body <- dashboardBody(frow1, frow2)

####################
# Dashboard Page
###################
ui <- dashboardPage(title = 'Model', header, sidebar, body, skin='purple')

####################
# SERVER
###################

d = data.frame(stringsAsFactors = F)
server <- function(input, output, session) {
  dd = reactiveValues(select = NULL, select2 = NULL)

  # Render Table
  output$mytable = DT::renderDataTable({
    DT::datatable(test(), rownames= FALSE, extensions = c('FixedHeader'),
                  filter = 'top', 
                  selection=list(mode = 'multiple'), 
                  options = list( autoWidth = TRUE,
                                  scrollX = TRUE, 
                                  orderClasses = TRUE,
                                  pageLength = 50, 
                                  fixedHeader = TRUE,
                                  dom = 'Bfrtip'
                  ),escape=F)
  }
  )


  proxy = DT::dataTableProxy('mytable')

  test <- reactive({
    if(input$hyp == 'All') {
      result = mydata
    } else {
      result = mydata %>% dplyr::filter(UQ(as.name(input$hyp)) <= 3)
    }
    return(result)
  })


  mt = reactiveValues(ndt = NULL)

  observe({
    if (length(input$mytable_rows_selected) >0) {  
    mt$ndt<- test()[input$mytable_rows_selected,]
    }
  })

  observeEvent(input$hyp, {freezeReactiveValue(input, "mytable_rows_selected")})

  proxy = DT::dataTableProxy('mytable')
  observe({print(input$mytable_rows_selected)})
  observe({print(mt$ndt)})

  #creating the valueBoxOutput content
  output$value1 <- renderValueBox({
    c_a = sum(mydata[mt$ndt[["id"]],"mpg"], na.rm = T)
    valueBox(
      formatC(c_a, format="d", big.mark=',')
      ,'Total MPG'
      ,icon = icon("th",lib='glyphicon')
      ,color = "purple")
  })
}

runApp(list(ui = ui, server = server), launch.browser = TRUE)

更新

我设法部分修复了它。我现在面临的问题-当我取消选择行时,它不会更改总和计算。我也希望所有选中的行保持突出显示。

  mt = reactiveValues(ndt = NULL)
  ft = reactiveValues(pa = NULL)

  observeEvent(input$mytable_rows_selected, {
    mu = data.frame(n = input$mytable_rows_selected, stringsAsFactors = F)
    mt$ndt<- test()[as.numeric(mu$n),]
    ft$pa = rbind(ft$pa, mt$ndt)
    ft$pa <- distinct(ft$pa, .keep_all = TRUE)
  }
  )

  #creating the valueBoxOutput content
  output$value1 <- renderValueBox({
    c_a = sum(ft$pa[,"mpg"], na.rm = T)
    valueBox(
      formatC(c_a, format="d", big.mark=',')
      ,'Total MPG'
      ,icon = icon("th",lib='glyphicon')
      ,color = "purple")
  })
}

1 个答案:

答案 0 :(得分:0)

总体而言,如果现在未选择任何行,则需要清除ft$pa,为此,您的观察者需要对input$mytable_rows_selected中的NULL值做出反应(此参数ignoreNULL = FALSE会有所帮助)。我对您的observeEvent

进行了简单的更改
observeEvent(input$mytable_rows_selected, ignoreNULL = FALSE, {
    mu = data.frame(n = input$mytable_rows_selected, stringsAsFactors = F)
    mt$ndt<- test()[as.numeric(mu$n),]
    ft$pa = rbind(ft$pa, mt$ndt)
    ft$pa <- distinct(ft$pa, .keep_all = TRUE)

    #clear reactive dataframe
    if (is.null(input$mytable_rows_selected))
      ft$pa <- ft$pa[-1,]
  })