2017-03-02 4 views
0

Gibt es eine Möglichkeit, den DataTable-Ausgang mit seiner Auswahl zu verwenden und diejenigen zu verwenden, die als reaktiver Eingang für die Darstellung markiert sind?In Shiny, ist es möglich, Zeilen aus DT durch Klicken als reaktive Eingabe auszuwählen?

ui <- basicPage(
    plotOutput("plot1", click = "plot_click"), 
    dataTableOutput("table1"), 
    verbatimTextOutput("info") 
) 

server <- function(input, output) { 
    output$table1 <- renderDataTable(({mtcars})) 
    #figure out a way to reactively select points to point into output$plot1 
    output$plot1 <- renderPlot({ 
    plot(mtcars$wt, mtcars$mpg) 
    }) 

    output$info <- renderPrint({ 
    # With base graphics, need to tell it what the x and y variables are. 
    nearPoints(mtcars, input$plot_click, xvar = "wt", yvar = "mpg") 
    # nearPoints() also works with hover and dblclick events 
    }) 
} 

shinyApp(ui, server) 

https://shiny.rstudio.com/articles/selecting-rows-of-data.html https://shiny.rstudio.com/gallery/datatables-options.html

+0

Sehen Sie sich https://yihui.shinyapps.io/DT-rows/ – HubertL

Antwort

1

hier ist die Lösung für Ihre Frage:

library(shiny) 
library(DT) 
library(ggplot2) 

ui <- basicPage(
    plotOutput("plot1", click = "plot_click"), 
    dataTableOutput("table1"), 
    verbatimTextOutput("info") 
) 

server <- function(input, output) { 
    output$table1 <- DT::renderDataTable(mtcars) 
    #figure out a way to reactively select points to point into output$plot1 
    output$plot1 <- renderPlot({ 
    s = input$table1_rows_selected 
    mtcars <- mtcars[ s,] 
    ggplot(mtcars, aes(mtcars$wt, mtcars$mpg)) + geom_point() 
    }) 

    output$info <- renderPrint({ 
    # With base graphics, need to tell it what the x and y variables are. 
    nearPoints(mtcars, input$plot_click, xvar = "wt", yvar = "mpg") 
    # nearPoints() also works with hover and dblclick events 
    }) 
} 

shinyApp(ui, server) 

ich die besondere Funktion von DT Paket namens verwendet haben: input$table1_rows_selected, die die higlighted Zeilen auswählt, i dann weiter unterteilen sie aus dem Datensatz mtcars

+0

Großartig an, das sagt mir, ich muss DT-Dokumentation ansehen :) – Jonathan

Verwandte Themen