简体   繁体   中英

Select rows from one table to another then plot the data from the second table R Shiny

I want to store rows selection from the first table to a second table. Then, create plot from selected rows that are now in the second table. Below is what I have tried to do, any suggestion? The data I have can be seen in the picture

library(shiny)
library(DT)
readfile <- read.csv("data.csv")
server <- shinyServer(function(input, output, session) {
    
    output$x1 = DT::renderDataTable(readfile, server = FALSE)
    
    output$x2 = DT::renderDataTable({
        sel <- input$x1_rows_selected
        if(length(readfile)){
            readfile[sel, ]
        }
        
    }, server = FALSE)  
    output$x3 <- renderPlot({
        s = input$x3_rows_selected
        ggplot(readfile[input$x1_rows_all, ], aes(x=Month)) +
            geom_bar()
    })
})


ui <- fluidPage(
    
    fluidRow(
        column(6, DT::dataTableOutput('x1')),
        column(6, DT::dataTableOutput('x2')),
        column(6, plotOutput('x3', height = 500))
    )
    
)

shinyApp(ui, server)

在此处输入图像描述

I think you just need to replace this:

ggplot(readfile[input$x1_rows_all, ], aes(x=Month)) 

with this:

ggplot(readfile[input$x1_rows_selected, ], aes(x=Month)) 

Update: Here's the whole app using the mtcars data.

library(DT)
readfile <- mtcars
server <- shinyServer(function(input, output, session) {
  
  output$x1 = DT::renderDataTable(readfile, server = FALSE)
  
  output$x2 = DT::renderDataTable({
    sel <- input$x1_rows_selected
    if(length(readfile)){
      readfile[sel, ]
    }
    
  }, server = FALSE)  
  output$x3 <- renderPlot({
    s = input$x3_rows_selected
    ggplot(readfile[input$x1_rows_selected, ], aes(x=factor(cyl, levels=c(4,6,8)))) +
      geom_bar()
  })
})


ui <- fluidPage(
  
  fluidRow(
    column(6, DT::dataTableOutput('x1')),
    column(6, DT::dataTableOutput('x2')),
    column(6, plotOutput('x3', height = 500))
  )
  
)

shinyApp(ui, server)

Here's what the output looks like - it seems to be doing what is intended.

闪亮的应用程序图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM