I am new to shiny
but was wondering if there is any way to store a filtered datatable (using the column filters) in a R
object so that this filtered data can be passed to histogram and plot functions.
EDIT May 7, 15: Including the author's expanded explanation from comments
I want the table to get filtered using the built-in column filters and then want the plot to automatically adjust. I've already tried the DT package but I don't like very much of the column filters that come with this package as it is not possible (I think) to remove the filters from a subset of the columns in the table
How to use DataTables in a Shiny App Basic Usage. The DataTables application demonstrates HTML tables using the jQuery library DataTables. The basic usage is... Customizing DataTables. There are a large number of options in DataTables that are customizable (see its website for... Upgrading from ...
It might be better to use [ or for example dplyr's filter_ function. You can make this much simpler if you begin to explore the DT package for datatables with shiny. With this, you can just type in whatever filter criteria you like above the respective columns.
DT is an interface to the JavaScript library DataTables. It allows you to display R dataframes (or matrices) as interactive tables in HTML pages, such as in a Shiny app. The most basic way to use it is the function datatable (df): library(DT) datatable (villagers 1:8])
You can edit a table via the editable argument of datatable (). After you finish editing, you can obtain the row/column indices and the new values of the cells that were edited via input$tableId_cell_info. See this Shiny app for more concrete examples.
Just building up on @JasonAizkalns's example, you can hide some of the built-in column filters using jQuery. for example here the first two are hidden:
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(dataTableOutput('tbl'),
plotOutput('plot1')),
server = function(input, output) {
output$tbl = renderDataTable({
datatable(iris, filter="top",options = list(lengthChange = FALSE),callback=JS("
//hide column filters for the first two columns
$.each([0, 1], function(i, v) {
$('input.form-control').eq(v).hide()
});"))
})
output$plot1 = renderPlot({
filtered_data <- input$tbl_rows_all
hist(iris[filtered_data, "Sepal.Length"])
})
}
)
The example suggested by @NicE is very helpful. I am including a minimal example below:
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(dataTableOutput('tbl'),
plotOutput('plot1')),
server = function(input, output) {
output$tbl = renderDataTable({
datatable(iris, options = list(lengthChange = FALSE))
})
output$plot1 = renderPlot({
filtered_data <- input$tbl_rows_all
hist(iris[filtered_data, "Sepal.Length"])
})
}
)
This will generate a histogram of Sepal.Length
from the iris
data set for the filtered data in the DT::datatable
.
Note: This assumes the following versions of DT
and shiny
:
DT_0.0.39 shiny_0.11.1.9005
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With