闪亮的应用程序忽略%>%管道到group_by(),mutate()和top_n()

时间:2017-11-07 08:23:26

标签: r shiny reactive-programming piping

根据amrrs的回答

更新

我有一个闪亮的应用程序,下面有可重现的代码,我无法找出正确的被动({})引用调用。我尝试聚合过滤数据时得到的错误,以及我不知道如何解决的错误,

" 错误:尝试在get1index中选择少于一个元素"

该应用有两个标签:

  • TAB 1:数据过滤器:用户导入csv(下面的示例);选择 要在图上显示的列;滑块用于过滤数据;显示过滤的散点图。请注意,我在%>%的过滤器调用中避免使用df_filt,因为它不起作用。因为我希望我的滑块限制和默认值基于用户选择的列,我使用了renderUI({})和uiOutput(),这使得引用语法有点麻烦(例如,filedata()[[input$selectcol1]]) - 这是表示法导致错误?

enter image description here

  • TAB 2:汇总表:应该是已有数据的汇总摘要 在TAB 1中过滤:group_by()选中的列; mutate()来计算数量 记录; top_n()查找每个组中的最高值。

enter image description here 以下是我的服务器和ui文件:

SERVER.R

library(shiny)
library(tidyverse)
library(plotly)

shinyServer(function(input, output) {

  filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }
    temp<-read.csv(infile$datapath)
    #return
    temp[order(temp[, 1]),]
  })

  output$selectcol1 <- renderUI({
    df <-filedata()
    if (is.null(df)) return(NULL)

    items=names(df)
    names(items)=items
    selectInput("selectcol1", "Height",items)
  })

  output$selectcol2 <- renderUI({
    df <-filedata()
    if (is.null(df)) return(NULL)

    items=names(df)
    names(items)=items
    selectInput("selectcol2", "Width",items)
  })

  output$selectcol3 <- renderUI({
    df <-filedata()
    if (is.null(df)) return(NULL)

    items=names(df)
    names(items)=items
    selectInput("selectcol3", "Height x Width",items)
  })

  output$selectcol4 <- renderUI({
    df <-filedata()
    if (is.null(df)) return(NULL)

    items=names(df)
    names(items)=items
    selectInput("selectcol4", "Hover Text",items)
  })

  output$minheight <- renderUI({
    sliderInput("height","Min. Height", min = 0, 
                max = max(filedata()[[input$selectcol1]], na.rm=TRUE),
                value = min(filedata()[[input$selectcol1]], na.rm=TRUE)
    )})

  output$minwidth <- renderUI({
    sliderInput("width","Min. Width", min = 0, 
                max = max(filedata()[[input$selectcol2]], na.rm=TRUE),
                value = min(filedata()[[input$selectcol2]], na.rm=TRUE)
    )})

  output$heightxwidth <- renderUI({
        sliderInput("hxw","Height x Width", min = 0,
                    max = max(filedata()[[input$selectcol3]], na.rm=TRUE),
                    value = min(filedata()[[input$selectcol3]], na.rm=TRUE)
        )})

  df_filt <- reactive({
    filter(filedata(),filedata()[[input$selectcol1]]>input$height &
             filedata()[[input$selectcol2]]>input$width &
             filedata()[[input$selectcol3]]>input$hxw)
  })

    df_filt_top <- reactive({
      df_filt() %>% 
        group_by(df_filt()[[input$selectcol6]], df_filt()[[input$selectcol5]]) %>%
        mutate(NumberOfRecords = n(),
               maxHxW = max(df_filt()[[input$selectcol3]])) %>%
        ungroup() %>%
        group_by(df_filt()[[input$selectcol6]], df_filt()[[input$selectcol5]]) %>%
        top_n(1, df_filt()[[input$selectcol3]])
    })

      output$filedata = renderDataTable({
        df_filt_top()
    })

  output$distPlot <- renderPlotly({

    p <- ggplot() + 
      geom_point(data=filedata(), aes(filedata()[[input$selectcol1]],
                                      y=filedata()[[input$selectcol2]], 
                                      text=paste("M: ",filedata()[[input$selectcol4]])),
                 colour="green", alpha=0.35) +
      geom_point(data=df_filt(), aes(x=df_filt()[[input$selectcol1]],
                                       y=df_filt()[[input$selectcol2]],
                                       text=paste("M: ",df_filt()[[input$selectcol4]])),
                 colour="orange", alpha=0.5) +
      xlab("Height") + ylab("Width") +
      scale_y_log10() +
      scale_x_log10()

    ggplotly(p)

  })
})

UI.R

library(shiny)
library(plotly)
library(tidyverse)


shinyUI(fluidPage(tabsetPanel(

  # Application title
  tabPanel("Data Filters", " ",
           fluidRow(
             titlePanel("Plot"),

             # Sidebar with a slider input for number of bins
             sidebarLayout(
               sidebarPanel(
                 fileInput('datafile', 'Choose CSV file',
                           accept=c('text/csv', 'text/comma-separated-values,text/plain')),
                 uiOutput("selectcol1"),
                 uiOutput("minheight"),
                 uiOutput("selectcol2"),
                 uiOutput("minwidth"),
                 uiOutput("selectcol3"),
                 uiOutput("heightxwidth"),
                 uiOutput("selectcol4"),
                 uiOutput("hovertext")

               ),

               # Show a plot of the generated distribution
               mainPanel(
                 plotlyOutput("distPlot")
               ))
           )),
  tabPanel("Summary Table", " ",
           fluidRow(
             titlePanel(h2("Aggregate Data"), br()),

             column(width = 3,
                    " ",
                    dataTableOutput('filedata'))
           ))
)))

CSV样本 FID,RecordName,ID_1,PrimaryType,ID_2,状态,类名,宽度,高度,HeightxWidth,马萨诸塞 1,A,611922,TYPE_A,52308,非活动,1,0.29,0.356,0.1032,0.9332 2,A,611923,TYPE_A,52308,非活动,1,0.4,0.242,0.0968,0.9332 3,B,458938,TYPE_A,70863,活动,2,5,0.72,3.6,5.6225 4,B,458939,TYPE_A,70863,活动,2,1,2.06,2.06,5.6225 5,B,458940,TYPE_A,70863,活动,2,1,2.83,2.83,5.6225 6,B,458941,TYPE_A,70863,活动,2,1,1.14,1.14,5.6225 7,B,458942,TYPE_A,70863,活动,2,3,1.24,3.72,5.6225 8,B,458943,TYPE_A,70863,活动,2,1,4.3,4.3,5.6225 9,B,458944,TYPE_A,70863,活动,2,1.2,2.73,3.276,5.6225 10,B,458945,TYPE_A,70863,活动,2,56.5,0.41,23.165,5.6225 11,B,458946,TYPE_A,70863,活动,2,15,1,15,5.6225 12,B,462017,TYPE_A,70863,活动,2,1,12.06,12.06,5.6225 13,B,471678,TYPE_A,70863,活动,2,1,19.45,19.45,5.6225 14,B,471679,TYPE_A,70863,活动,2,3,1.05,3.15,5.6225 15,B,471680,TYPE_A,70863,活动,2,1,4.67,4.67,5.6225 16,B,471681,TYPE_A,70863,活动,2,2,1.6,3.2,5.6225 17,B,471682,TYPE_A,70863,活动,2,1,1.27,1.27,5.6225 18,B,471683,TYPE_A,70863,活动,2,1,1.93,1.93,5.6225 19,B,471684,TYPE_A,70863,活动,2,3,1.47,4.41,5.6225 20,B,471685,TYPE_A,70863,活动,2,1,1.06,1.06,5.6225 21,B,471686,TYPE_A,70863,活动,2,7.96,1.28,10.1888,5.6225 22,B,471687,TYPE_A,70863,活动,2,1.54,3.06,4.7124,5.6225 23,B,555816,TYPE_A,70863,活动,2,0.2,1.84,0.368,5.6225 24,B,555817,TYPE_A,70863,活动,2,0.15,6.1,0.915,5.6225 25,B,555818,TYPE_A,70863,活动,2,0.3,1.65,0.495,5.6225 26,B,555819,TYPE_A,70863,活动,2,0.2,9.39,1.878,5.6225 27,B,555820,TYPE_A,70863,活动,2,0.4,7.14,2.856,5.6225 28,B,555821,TYPE_A,70863,活动,2,0.25,3.22,0.805,5.6225 29,B,555822,TYPE_A,70863,活动,2,0.5,2.84,1.42,5.6225 30,B,555823,TYPE_A,70863,活动,2,0.4,7.34,2.936,5.6225 31,B,555824,TYPE_A,70863,活动,2,0.2,2.82,0.564,5.6225 32,B,555825,TYPE_A,70863,活动,2,0.35,7.23,2.5305,5.6225 33,B,555826,TYPE_A,70863,活动,2,0.2,1.05,0.21,5.6225 34,B,555827,TYPE_A,70863,活动,2,0.15,2.28,0.342,5.6225 35,B,555828,TYPE_A,70863,活动,2,0.4,1.23,0.492,5.6225 36,B,555829,TYPE_A,70863,活动,2,0.15,1.28,0.192,5.6225 37,C,58431,TYPE_A,25871,活动,3,3.14,6.67,20.9438,52.9908 38,C,58432,TYPE_A,25871,活动,3,1.96,10.83,21.2268,52.9908 39,C,58433,TYPE_A,25871,活动,3,0.21,4.16,0.8736,52.9908 40,C,58434,TYPE_A,25871,活动,3,0.21,16,3.36,52.9908 41,C,58435,TYPE_A,25871,活动,3,4.08,26.36,107.5488,52.9908 42,C,58436,TYPE_A,25871,活动,3,7.78,0.22,1.7116,52.9908 43,C,58437,TYPE_A,25871,活动,3,6.37,1.6,10.192,52.9908 44,C,58438,TYPE_A,25871,活动,3,3.35,3.4,11.39,52.9908 45,C,58439,TYPE_A,25871,活动,3,2.44,6.5,15.86,52.9908

1 个答案:

答案 0 :(得分:1)

问题出在Server.R的第79行

您的代码:指的是被动对象df_filt

df_filt_top <- reactive({ df_filt %>%

应更改为使用df_filt()调用该反应式表达式:

df_filt_top <- reactive({ df_filt() %>%

但由于代码混乱,看起来你会面临新的问题。