Reputation: 3760
i am having a problem with datatable filter. My data is very big (> 5000000 rows), including some numerical and factor columns. Datatable uses to filter the numerical column slider, everything would be great if not the "steps" of the range. Is there any way to make the steps more finer?
Example code:
library(shiny)
library(DT)
library(ggplot2)
x <- as.numeric(1:1000000)
y <- as.numeric(1:1000000)
data <- data.frame(x,y)
shinyApp(
ui = fluidPage(dataTableOutput('tbl'),
plotOutput('plot1')),
server = function(input, output) {
output$tbl = renderDataTable({
datatable(data, filter = "top", options = list(
pageLength = 300, lengthMenu = c(100,200,300,400,500,600)
))
})
output$plot1 = renderPlot({
filtered_data <- input$tbl_rows_all
ggplot(data = filtered_data, aes(x = x,y = y)) + geom_line()
})
}
)
Thanks for the help!
Cheers
Can i use somehow JQuery? I found this question on stackoverflow which might be usefull, however i dont know how i can implement it to my datatable.
Upvotes: 2
Views: 2532
Reputation: 7871
You can filter on server side not in DT
1) Add sliderInput
for each numeric column ( may be using loop
or lapply
)
2) create reactive
for subseting
3) render subseted data
for example
library(shiny)
library(DT)
library(ggplot2)
q <- as.numeric(1:1000)
w <- as.numeric(1:1000)
e <- as.numeric(1:1000)
r <- as.numeric(1:1000)
t <- as.numeric(1:1000)
y <- as.numeric(1:1000)
u <- as.numeric(1:1000)
i <- as.numeric(1:1000)
o <- as.numeric(1:1000)
data <- data.frame(q,w,e,r,t,y,u,i,o)
shinyApp(
ui = fluidPage(
fluidRow(uiOutput("filter_ui")),
dataTableOutput('tbl'),
plotOutput('plot1')
),
server = function(input, output) {
output$filter_ui = renderUI({
lapply(colnames(data),function(i) {
column(
width = round(12 / ncol(data),0),sliderInput(
paste0("s",i),min = min(data[[i]]),
max = max(data[[i]]),step = 100 ,
value = c(min(data[[i]]),max(data[[i]])),label = i
)
)
})
})
data_1 = reactive({
data_ = data
for (i in colnames(data)) {
data_ = data_[data_[[i]] <= input[[paste0("s",i)]][2] &
data_[[i]] >= input[[paste0("s",i)]][1],]
}
data_
})
output$tbl = renderDataTable({
DT::datatable(data_1(), options = list(
pageLength = 300, lengthMenu = c(100,200,300,400,500,600)
))
})
output$plot1 = renderPlot({
ggplot(data = data_1(), aes(x = q,y = w)) + geom_line()
})
}
)
Upvotes: 2