【问题标题】:Shiny dplyr filter doesn't work after file upload文件上传后闪亮的 dplyr 过滤器不起作用
【发布时间】:2019-09-06 14:15:13
【问题描述】:

我正在构建一个闪亮的应用程序,以允许我的一些非数据人员上传一些文件,一些转换、连接和摘要来自这些文件,并显示一些数字。

这里将使用一个文件和输出作为示例。我正在尝试在列上使用 dplyr 进行过滤条件,但在上传文件后出现此错误。

Listening on http://xxx.x.x.x:xxxx
Warning: Error in UseMethod: no applicable method for 'filter_' applied to an object of class "character"
  [No stack trace available]

请注意,我并没有尝试做一个反应条件(还),只是试图过滤掉不需要产生所需输出的变量(在这种情况下是 2 因子饼图)。是过滤出错还是上传文件的方式有误?

用于过滤和 ggplot 的管道在有光泽的情况下工作正常。

library(shiny)
library(tidyverse)
library(scales)

# Define UI for data upload app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Uploading Files"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv"))

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Data file ----
      plotOutput(outputId = "plots")

    )

  )
)

和我的服务器

# Define server logic to read selected file ----
server <- function(input, output) {

  observe({
    data <- input$file1
    if(is.null(data))
      return(NULL)

    df <- data$datapath %>%
      filter(DQ.File == "In Compliance" | DQ.File == "Out of Compliance") %>% 
      group_by(DQ.File) %>% 
      summarise (n = n()) %>% 
      mutate(DQ.File = recode(DQ.File,
                              "In Compliance" = "Drivers In Compliance",
                              "Out of Compliance" = "Drivers Out Of Compliance"),
             freq = round((n / sum(n)) * 100, 2),
             label = paste(DQ.File, "-", paste(freq, "%", sep = ""))) %>% 
      select(-c(n, DQ.File))

    output$plots = renderPlot({
      df %>% ggplot(aes(x = 1, y = freq, fill = label)) +
        coord_polar(theta = 'y') +
        geom_bar(stat = "identity", color = 'black') +
        scale_fill_manual(values = c("darkgreen", "red")) +
        theme_minimal()+
        theme(
          axis.title.x = element_blank(),
          axis.text = element_blank(),
          axis.title.y = element_blank(),
          panel.border = element_blank(),
          panel.grid = element_blank(),
          axis.ticks = element_blank(),
          plot.title = element_text(size=14, face="bold"),
          legend.title = element_blank(),
          axis.text.x = element_blank(),
          legend.background = element_rect(linetype = "solid"))
    })
  })
}

shinyApp(ui, server)

有没有办法在上传步骤中保留这些因素,还是我的问题在其他地方?

【问题讨论】:

    标签: r ggplot2 shiny dplyr


    【解决方案1】:

    不推荐在observe() 内渲染输出,在这种情况下甚至不需要。这是一个更好的方法 -

    server <- function(input, output, session) {
    
      df <- reactive({
        req(input$file1)
        read.csv(input$file1$datapath, header = T) %>%
        filter(DQ.File == "In Compliance" | DQ.File == "Out of Compliance") %>% 
        group_by(DQ.File) %>% 
        summarise (n = n()) %>% 
        mutate(DQ.File = recode(DQ.File,
                                "In Compliance" = "Drivers In Compliance",
                                "Out of Compliance" = "Drivers Out Of Compliance"),
               freq = round((n / sum(n)) * 100, 2),
               label = paste(DQ.File, "-", paste(freq, "%", sep = ""))) %>% 
        select(-c(n, DQ.File))
      })
    
      output$plots <- renderPlot({
        df() %>% 
          ggplot(aes(x = 1, y = freq, fill = label)) +
          coord_polar(theta = 'y') +
          geom_bar(stat = "identity", color = 'black') +
          scale_fill_manual(values = c("darkgreen", "red")) +
          theme_minimal()+
          theme(
            axis.title.x = element_blank(),
            axis.text = element_blank(),
            axis.title.y = element_blank(),
            panel.border = element_blank(),
            panel.grid = element_blank(),
            axis.ticks = element_blank(),
            plot.title = element_text(size=14, face="bold"),
            legend.title = element_blank(),
            axis.text.x = element_blank(),
            legend.background = element_rect(linetype = "solid"))
      })
    }
    

    【讨论】:

    • 酷,谢谢。不知道我在哪里捡到的。对 Shiny 来说还是新手,将进一步研究其目的。谢谢你的帮助。
    • @jarichardson 只需按顺序阅读 RSudio 的 Shiny 教程,就可以了。祝你好运!
    【解决方案2】:

    回答了我自己的问题,但也许是为了别人:

    使用“stringsAsFactors = TRUE”将 read.csv 添加到服务器中,但也许有更好的方法?

    server <- function(input, output) {
    
      observe({
        data <- input$file1
        if(is.null(data))
          return(NULL)
    
        df <- read.csv(data$datapath, stringsAsFactors = TRUE) %>%
          filter(DQ.File == "In Compliance" | DQ.File == "Out of Compliance") %>% 
          group_by(DQ.File) %>% 
          summarise (n = n()) %>% 
          mutate(DQ.File = recode(DQ.File,
                                  "In Compliance" = "Drivers In Compliance",
                                  "Out of Compliance" = "Drivers Out Of Compliance"),
                 freq = round((n / sum(n)) * 100, 2),
                 label = paste(DQ.File, "-", paste(freq, "%", sep = ""))) %>% 
          select(-c(n, DQ.File))
    
        output$plots = renderPlot({
          df %>% ggplot(aes(x = 1, y = freq, fill = label)) +
            coord_polar(theta = 'y') +
            geom_bar(stat = "identity", color = 'black') +
            scale_fill_manual(values = c("darkgreen", "red")) +
            theme_minimal()+
            theme(
              axis.title.x = element_blank(),
              axis.text = element_blank(),
              axis.title.y = element_blank(),
              panel.border = element_blank(),
              panel.grid = element_blank(),
              axis.ticks = element_blank(),
              plot.title = element_text(size=14, face="bold"),
              legend.title = element_blank(),
              axis.text.x = element_blank(),
              legend.background = element_rect(linetype = "solid"))
        })
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 2017-01-15
      • 2015-11-09
      相关资源
      最近更新 更多