我想使用数据表中过滤的数据来创建一个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 <- 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")]
非常感谢您的任何想法