我遇到了数据表过滤器的问题。我的数据非常大(> 5000000行),包括一些数字和因子列。数据表用于过滤数字列滑块,如果不是范围的“步骤”,一切都会很好。 有没有办法让步骤更精细??
示例代码:
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()
})
}
)
感谢您的帮助!
干杯
我能以某种方式使用JQuery吗?我在stackoverflow which might be usefull上发现了这个问题,但是我不知道如何将它实现到我的数据表中。
答案 0 :(得分:1)
您可以在服务器端进行过滤,而不是在DT
中进行过滤 1)为每个数字列添加sliderInput
(可能正在使用loop
或lapply
)
2)为子项目创建reactive
3)渲染子分段数据
例如
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()
})
}
)