我想使用来自datatable的过滤数据来创建ggplot。我实现了两个小部件,在过滤数据表后,用户可以选择x& y轴显示。设置小部件后,我得到一个错误:
Error in `[.data.frame`(data_melt, filtered_data) : undefined columns selected
我不知道为什么会这样。
我的数据示例:
Rundheit Diff Charge Ord..Nr. Block.Nr.
1 0.24 0.20 754331 738 1
2 0.26 0.21 783345 738 2
3 0.25 0.15 795656 738 3
4 NA 0.14 798431 738 4
5 NA 0.12 799651 738 5
6 0.24 NA 805454 738 6
我的数据中的NA值必须
UI:
ui <- dashboardPage(
dashboardHeader(title = "WW"),
dashboardSidebar(
selectizeInput(inputId = "yaxis",
label = "Y-axis (Diagramm)",
choices = list("Rundheit" = "Rundheit",
"Diff" = "Diff"),
selected = c("Rundheit"), multiple=TRUE),
selectInput(inputId = "xaxis",
label = "X-axis (Diagramm)",
choices = names(data_melt),
selected = "Block.Nr.")
),
dashboardBody(
fluidRow(
tabBox(status = "primary", width = NULL, height = "1000px",
tabPanel(title="Tabelle filtern", div(style = 'overflow-y: scroll; max-height: 950px; position:relative;',
dataTableOutput("tabelle"))),
tabPanel("Diagramm", plotOutput("plot1")),
tabPanel("Histogramm", plotOutput("plot2"))))
))
服务器:
server <- function(input, output, session) {
output$tabelle <- renderDataTable({
datatable(data[, c("Rundheit", "Diff", "Charge.", "Ord..Nr.", "Block.Nr.")], class = 'cell-border stripe',
rownames=FALSE, filter="top",
options = list(lengthChange = FALSE, columnDefs = list(list(width = '200px', targets = "_all"), list(bSortable = FALSE, targets = "_all"))), callback=JS("
//hide column filters for two columns
$.each([0, 1], function(i, v) {
$('input.form-control').eq(v).hide()});",
"var tips = ['Rundheit', 'Diff', 'Charge',
'Ord..Nr.', 'Block.Nr.'],
header = table.columns().header();
for (var i = 0; i < tips.length; i++) {
$(header[i]).attr('title', tips[i]);}")) %>%
formatStyle("Rundheit", color = 'red', backgroundColor = 'lightyellow', fontWeight = 'bold')
})
output$plot1 <- renderPlot({
filtered_data <- input$tabelle_rows_all
data_filt <- data_melt[filtered_data]
ggplot(data=data_filt, aes_string(x = input$xaxis, y = input$yaxis), environment = environment())+ geom_line(aes(group=1), size=1) +
theme(axis.text.y=element_text(size=15), axis.text.x=element_text(size=15), axis.title.x = element_text(size=18, face="bold"),axis.title.y = element_text(size=18, face="bold"))
})
}
shinyApp(ui = ui, server = server)
有没有人知道它为什么不起作用,我如何定义列呢。
我看过帖子:http://stackoverflow.com/questions/30042456/using-filtered-datatables-in-shiny
但是对于代码:
[filtered_data, "name of the column"]
无法使用例如:
data_filt <- data_melt[filtered_data, ]
Error in seq.int(0, to0 - from, by) : 'to' cannot be NA, NaN or infinite
和
Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) :
'from' must be of length 1
以及:
data_filt <- data_melt[filtered_data, input$xaxis]
并且它给出了一个错误(取决于列的类型):
Error : ggplot2 doesn't know how to deal with data of class factor
Error : ggplot2 doesn't know how to deal with data of class numeric
我需要input$yaxis
才能实施......
所以我试过了:
data_filt <- data_melt[filtered_data, c(input$xaxis, input$yaxis)]
比我收到错误
Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) :
'from' must be of length 1
仅仅因为我使用此代码播放的信息,甚至通过指定列的名称它会引发错误,我甚至无法指定多于一个
我尝试过这样的事情:
[filtered_data, "Rundheit")
[filtered_data, c("Rundheit", "Diff")]
非常感谢您的任何想法
答案 0 :(得分:1)
因此,您的代码有点“混乱”,有一些编译器错误,一些代码丢失,我不得不手动输入您的数据。不是每个人都会这样做......我也不确定data
和data_melt
内容应该发生在哪里,所以我只是去了data_melt
。无论如何,我得到它的工作,我不得不承认这是强大而迷人的功能。我希望这是你想要的,虽然我没有看到你所有的错误信息。
您的主要错误是设置rownames=F
,因为rownames是input$tabelle_rows_all
用于过滤表格的名称。我还在nrow
调用中添加了ggplot
后卫,以防止它在空数据帧上窒息。
以下是工作代码:
library(shiny)
library(shinydashboard)
library(dplyr)
library(ggplot2)
library(DT)
rr <- c(0.24,0.26,0.25,NA,NA,0.24)
dd <- c(0.20,0.21,0.15,0.14,0.12,NA)
cc <- c(74331,783345,795656,798431,799651,805454)
oo <- rep(738,6)
bb <- 1:6
data_melt <- data.frame(Rundheit=rr,Diff=dd,Charge.=cc,Ord..Nr.=oo,Block.Nr.=bb)
ui <- dashboardPage(
dashboardHeader(title = "WW"),
dashboardSidebar(
selectizeInput(inputId = "yaxis",
label = "Y-axis (Diagramm)",
choices = list("Rundheit" = "Rundheit",
"Diff" = "Diff"),
selected = c("Rundheit"), multiple=TRUE),
selectInput(inputId = "xaxis",
label = "X-axis (Diagramm)",
choices = names(data_melt),
selected = "Block.Nr.")
),
dashboardBody(
fluidRow(
tabBox(status = "primary", width = NULL, height = "1000px",
tabPanel(title="Tabelle filtern",
div(style = 'overflow-y: scroll; max-height: 950px; position:relative;',
dataTableOutput("tabelle"))),
tabPanel("Diagramm", plotOutput("plot1")),
tabPanel("Histogramm", plotOutput("plot2"))))
))
server <- function(input, output, session) {
output$tabelle <- renderDataTable({
datatable(data_melt[, c("Rundheit", "Diff", "Charge.", "Ord..Nr.", "Block.Nr.")],
class = 'cell-border stripe',
filter="top",
options = list(lengthChange = FALSE,
columnDefs = list(list(width = '200px', targets = "_all"),
list(bSortable = FALSE, targets = "_all"))),
callback=JS("
//hide column filters for two columns
$.each([0, 1], function(i, v) {
$('input.form-control').eq(v).hide()});
var tips = ['Rundheit', 'Diff', 'Charge',
'Ord..Nr.', 'Block.Nr.'],
header = table.columns().header();
for (var i = 0; i < tips.length; i++) {
$(header[i]).attr('title', tips[i]);}")) %>%
formatStyle("Rundheit", color='red', backgroundColor='lightyellow', fontWeight='bold')
})
output$plot1 <- renderPlot({
filtered_data <- input$tabelle_rows_all
data_filt <- data_melt[filtered_data,]
if (nrow(data_filt>0)){
g <-ggplot(data=data_filt, aes_string( x=input$xaxis, y=input$yaxis),
environment=environment())+
geom_line(aes(group=1), size=1) +
theme(axis.text.y=element_text(size=15),
axis.text.x=element_text(size=15),
axis.title.x = element_text(size=18, face="bold"),
axis.title.y = element_text(size=18, face="bold"))
return(g)
} else {
return(NULL)
}
})
}
shinyApp(ui = ui, server = server)
这里有几个屏幕截图显示它正常工作:
产量: