在R Shiny中重置DataTable

时间:2016-01-08 13:07:34

标签: r shiny

我想在特定事件发生时重置属于R Shiny中对象的值(即DataTable中的row_last_clicked

(简化)工作流程如下(我的原始应用程序包括对数据库的查询,所以我在这里简化了它):

  1. 用户在搜索字段中输入搜索字词(例如' versicolor')(' which_species')。
  2. 结果显示在DataTable(query_result_table)。
  3. 用户在显示的DataTable(query_result_table)中选择一行。
  4. 所选行只显示在新的DataTable(selected_row_table)中。
  5. 用户输入新的查询字词(例如' setosa')。我们希望重置第二个DataTable(selected_row_table)并且不显示任何内容(直到第3步结束)。
  6. 问题是如果用户键入新的查询字词,那么第一个DataTable(row_last_clicked)的query_result_table值会保持其当前值,因此第二个DataTable selected_row_table将显示一行基于先前选择的当前查询结果。

    我尝试从代码中修改input$query_result_table_row_last_clicked的值,但它是只读的。

    ui.R

    shinyUI(fluidPage(
      fluidRow(
        column(4,
               selectInput("which_species", label = h5("Specify species"), 
                           choices = c("versicolor","setosa","virginica")))
      ),
      fluidRow(
        column(12,
               dataTableOutput(outputId = "query_result_table"))
      ),
    
      fluidRow(
        column(12,
               dataTableOutput(outputId = "selected_row_table"))
      )
      ))
    

    server.R

      library(DT)
    shinyServer(function(input, output) {
    
      # This might be a time consuming database query so I use reactive expression 
      query_result <-  reactive({
        data <- iris[iris$Species==input$which_species,]
        # Below code is needed to ensure |row_last_clicked| returns the actually clicked row and not rowname of the data.frame
        q <- dim(data)
        if (q[1]==0) {return(data)}
        rownames(data) <- 1:q[1]
        data
        })
    
      output$query_result_table <- renderDataTable({
        query_result()
      },selection = 'single')
    
    
      output$selected_row_table <- renderDataTable({
    
        selected_row_in_query_result  <- input$query_result_table_row_last_clicked
        if (is.null(selected_row_in_query_result)) {return()}
        selected_row_in_query_result  <- as.integer(selected_row_in_query_result)
        message(selected_row_in_query_result)
        data_to_display <- query_result()
        data_to_display <- data_to_display[selected_row_in_query_result,]
      })
    

1 个答案:

答案 0 :(得分:1)

以下代码可满足您的需求。基本思想是单行表(data_to_display)中显示的数据由两个eventReactive()函数控制。一个在input$which_species更改时激活,只需将data_to_display设置为NULL,这样您就不会看到表格。第二个在input$query_result_table_row_last_clicked更改时激活(即您单击一行),并且会根据所选行显示该表。

library(shiny)
library(DT)

ui<-fluidPage(
  fluidRow(
    column(4,
           selectInput("which_species", label = h5("Specify species"), 
                       choices = c("versicolor","setosa","virginica")))
  ),
  fluidRow(
    column(12,
           DT::dataTableOutput(outputId = "query_result_table"))
  ),

  fluidRow(
    column(12,
           DT::dataTableOutput(outputId = "selected_row_table"))
  )
)

server<-shinyServer(function(input, output) {

  # This might be a time consuming database query so I use reactive expression 
  query_result <-  reactive({
    data <- iris[iris$Species==input$which_species,]
    # Below code is needed to ensure |row_last_clicked|
    # returns the actually clicked row and not rowname of the data.frame
    q <- dim(data)
    if (q[1]==0) {return(data)}
    rownames(data) <- 1:q[1]
    data
  })

  output$query_result_table <- DT::renderDataTable({
    query_result()
  },selection = 'single')

  data_to_display<-eventReactive(input$query_result_table_rows_selected,ignoreNULL=TRUE,
                 query_result()[as.integer(input$query_result_table_row_last_clicked),]
   )

  output$selected_row_table<-DT::renderDataTable(data_to_display())

})

shinyApp(ui,server)

根据内容编辑了一行