3

我想使用数据表中过滤的数据来创建一个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")]

非常感谢您的任何想法

4

1 回答 1

1

所以你的代码有点“混乱”,有一些编译器错误,一些代码丢失,我不得不手动输入你的数据。不是每个人都会这样做......我也不确定应该在哪里data发生data_melt这些事情,所以我只是去了data_melt。无论如何,我让它工作了,我不得不承认这是一个强大而迷人的功能。我希望这是你想要的,虽然我没有看到你所有的错误信息。

您的主要错误是设置rownames=F,因为行名是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)

这里有几个屏幕截图来展示它的工作原理:

在此处输入图像描述

产量:

在此处输入图像描述

于 2016-01-15T23:13:38.117 回答