簡體   English   中英

R Shiny:從數據表中獲取訂單並適用於plot

[英]R Shiny: Get ordering from data table and apply to plot

使用以下 Shiny 應用程序,我想根據數據表中的排序更新 plot,即如果我在數據表中按x對數據進行排序,它應該自動將該排序應用於 Z32FA6E1B78A9D40284953E6。 我怎么會 go 呢? 數據表是否有一個輸入變量,如rows_selected用於當前排序?

測試數據

testdata <- data.frame(x = round(runif(10)*100), y = round(runif(10)*100))
rownames(testdata) <- LETTERS[1:10]

ui.R

require(DT)

ui <- fluidPage(
  
  fluidRow(
    plotOutput("plot")
  ),
  fluidRow(
    DT::dataTableOutput("table")
  )
  
)

server.R

server <- function(input, output) {
  

  output$plot <- renderPlot({
    
    barplot(testdata[,1])
    
  })
  
  output$table <- DT::renderDataTable({
    
    DT::datatable(
      testdata
    )
    
  })
  
}

shinyApp(ui, server)

在下面的示例中,plot 根據下表中選擇的列重新排序。

library(shiny)
library(DT)

ui <- fluidPage(
  plotOutput("plot"),
  DTOutput("table") #, DTOutput("table2") 
)

server <- function(input, output) {
  rv <- reactiveValues(data=NULL,
                       data2=NULL)
  
  output$table <- renderDT({
    
    rv$data <- data.frame(
      Name = c("Person A", "Person B", "Person C", "Person D"), 
      Metric_1 = c(8, 7, 4, 10), 
      Metric_2 = c(3, 5, 2, 8)
    )
    rv$data2 <- rv$data
    
    datatable(rv$data, extensions = 'Select', 
                       options = list(select = list(style = 'single', items = 'column'),
                                      ordering = TRUE, searching = FALSE, pageLength = 10), 
              selection = 'none')
    
  }, server = FALSE)
  
  observeEvent(input$table_columns_selected,{
    df <- rv$data[order(rv$data[,input$table_columns_selected]), ]
    output$table2 <- renderDT({
      subset_table <- df[, input$table_columns_selected, drop = F]
      datatable(subset_table)
    })
  
    rv$data2 <- df
    
  })
  
  output$plot <- renderPlot({
    barplot(rv$data2[,2])
  })
}

shinyApp(ui = ui, server = server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM