ggplot使用来自datatable(DT)和reactive小部件的过滤数据来选择x轴和y轴

ggplot使用来自datatable(DT)和reactive小部件的过滤数据来选择x轴和y轴,r,ggplot2,datatable,shiny,dt,R,Ggplot2,Datatable,Shiny,Dt,我想使用datatable中过滤的数据来创建ggplot。我还实现了两个小部件,在过滤数据表之后,它们将允许用户选择x&y轴来显示。在设置小部件等之后,我遇到了一个错误: Error in `[.data.frame`(data_melt, filtered_data) : undefined columns selected 我不知道为什么会这样 我的数据示例: Rundheit Diff Charge Ord..Nr. Block.Nr.

我想使用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值必须保留在我的数据中

用户界面:

不工作,例如使用:

data_filt <- data_melt[filtered_data, ]

Error in seq.int(0, to0 - from, by) : 'to' cannot be NA, NaN or infinite
以及:

data_filt <- data_melt[filtered_data, input$xaxis]
我需要
输入$yaxis
并将其实现。。。 因此,我尝试:

data_filt <- data_melt[filtered_data, c(input$xaxis, input$yaxis)]
我用这段代码播放了一段信息,即使指定了列的名称,它也会抛出一个错误,我甚至不能指定多个

我尝试过以下方法:

[filtered_data, "Rundheit")

[filtered_data, c("Rundheit", "Diff")]
非常感谢您提供的任何想法

因此您的代码有点“凌乱”,有一些编译器错误,一些代码缺失,我不得不手工输入您的数据。不是每个人都会这么做。。。我也不确定
data
data\u-melt
的事情应该发生在哪里,所以我选择了
data\u-melt
。不管怎样,我让它工作了,我必须承认这是一个强大而迷人的功能。我希望这是您想要的,尽管我没有看到您所有的错误消息本身

您的主要错误是设置了
rownames=F
,因为
input$tabelle\u rows\u all
使用这些行名来过滤表。我还向
ggplot
调用添加了一个
nrow
保护,以防止它被空数据帧阻塞

以下是工作代码:

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)
库(闪亮)
图书馆(shinydashboard)
图书馆(dplyr)
图书馆(GG2)
图书馆(DT)

rr您的
renderPlot
未关闭。请修复。对不起,我只是复制了我的代码没有完全。。renderPlot在我的脚本中是关闭的。当问问题时,最好提供一个最低限度的工作示例。感谢tipp,对不起,我是新来的。我刚刚更新了我的帖子。这个答案有用吗?
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
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")]
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)