按分类变量过滤后的绘图(错误过滤器(),尝试应用非函数)

Posted

技术标签:

【中文标题】按分类变量过滤后的绘图(错误过滤器(),尝试应用非函数)【英文标题】:Plot after filtering by categorical variable (error filter(), attempt to apply non-function) 【发布时间】:2022-01-17 02:22:48 【问题描述】:

罗马历史迷在这里。所以我创建了一个小数据框,其中包含军团部分legionsfifthtirteenth),以及他们的moralehighmediumlow)。

我想可视化各个军团的士气差异。为此,我将为军团创建一个条形图,按士气过滤。

所以在 X 轴上,我将有 fifthtirteenth,以及我们的士气选择过滤的浓度。

这就是我所拥有的。 (请注意这是一个玩具示例,实际上x、y 和因子变量有很多变量,不幸的是没有罗马字)

# Shiny
library(shiny)
library(shinyWidgets)

# Data
library(readxl)
library(dplyr)

# Plots
library(ggplot2)



Legion <- c("Fifth", "Fifth", "Fifth","Fifth","Fifth","Fifth", "Fifth", "Fifth","Fifth","Fifth","Tirteenth","Tirteenth", "Tirteenth", "Tirteenth","Tirteenth", "Tirteenth","Tirteenth", "Tirteenth", "Tirteenth","Tirteenth")
Morale <- c("High", "High", "Low","High", "Medium", "Low","High", "Medium", "Low", "High", "High", "High", "Low","High", "Medium", "Low","High", "Medium", "Low", "High")
romans <- data.frame(Legion, Morale)


not_sel <- "Not Selected"

# main page display in the shiny app where user will input variables and plots will be displayed
main_page <- tabPanel(
  title = "Plotter",
  titlePanel("Plotter"),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("xlsx_input", "Select XLSX file to import", accept = c(".xlsx")),
      selectInput("num_var_1", "Variable X axis", choices = c(not_sel)),
      selectInput("num_var_2", "Filter Y axis", choices = c(not_sel)), uiOutput("binning"),
      br(),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          plotOutput("plot_1")
        )
      )
    )
  )
)



# Function for printing the plots with two different options
# When there is not a selection of the biomarker (we will take into account var_1 and var_2)
# And when there is a selection of the biomarker (we will take into account the three of them)
draw_barplot <- function(data_input, num_var_1, num_var_2, biomarker)
  print(num_var_1)
  
  if(num_var_1 != not_sel & num_var_2 != not_sel & biomarker == not_sel)
    ggplot(data = data_input, aes(x = .data[[num_var_1]])) +
      geom_bar() + 
      theme_bw()
  
  
  else if(num_var_1 != not_sel & num_var_2 != not_sel & biomarker != not_sel)
    ggplot(data = data_input, aes(x = .data[[num_var_1]])) +
      geom_bar() + 
      theme_bw()
  



ui <- navbarPage(
  main_page
)


server <- function(input, output)
  
  # Dynamic selection of the data. We allow the user to input the data that they want 
  data_input <- reactive(
    #req(input$xlsx_input)
    #inFile <- input$xlsx_input
    #read_excel(inFile$datapath, 1)
    romans
  )
  
  # We update the choices available for each of the variables
  observeEvent(data_input(),
    choices <- c(not_sel, names(data_input()))
    updateSelectInput(inputId = "num_var_1", choices = choices)
    updateSelectInput(inputId = "num_var_2", choices = choices)
  )
  
  
  # We select the binning level that we want for the plot of the Y axis
  output$binning <- renderUI(
    req(input$num_var_2, data_input())
    a <- unique(data_input()[[input$num_var_2]])
    pickerInput(inputId = 'selected_bins',
                label = 'Select binning for plot',
                choices = c(a[1:length(a)]), selected=a[1], multiple = TRUE,
                options = list(`actions-box` = TRUE)) #options = list(`style` = "btn-warning"))
  )
  
  
   
  num_var_1 <- eventReactive(input$run_button, input$num_var_1)
  num_var_2 <- eventReactive(input$run_button, input$num_var_2)
  

  ##### BoxPlot ----------------------------------------------------------------
  
  plot_1 <- eventReactive(input$run_button,
    req(input$selected_bins, data_input())
    df <- data_input() %>% dplyr::filter(num_var_1() == input$selected_bins())
    draw_barplot(df, num_var_1())
  )
  
  output$plot_1 <- renderPlot(plot_1())
  


# Connection for the shinyApp
shinyApp(ui = ui, server = server)

但是,我收到了下一个错误: error

这显然是在情节的eventReactive中。

【问题讨论】:

谢谢@YBS,但错误仍然存​​在。 Problem with filter()input ..1. [34mi[39m Input ..1 is num_var_1() %in% input$selected_bins(). [31mx[39m attempt to apply non-function 该错误是由于input$selected_bins()。请改成input$selected_bins 【参考方案1】:

试试这个,它对我有用。

  plot_1 <- eventReactive(input$run_button,
    req(input$selected_bins, data_input(),input$num_var_2)
    df <- data_input() %>% dplyr::filter(.data[[input$num_var_2]] %in% input$selected_bins ) 
    draw_barplot(df, num_var_1(), num_var_2(), biomarker = "selected")
  )
  

【讨论】:

以上是关于按分类变量过滤后的绘图(错误过滤器(),尝试应用非函数)的主要内容,如果未能解决你的问题,请参考以下文章

ShinyApp中过滤数据变量的命名问题

使用选择性覆盖的Drupal视图的术语(而非分类)的自定义分类法页面

django模板:过滤后的点

在 R 中使用 dplyr 进行过滤时,为啥过滤掉的变量级别会保留在过滤后的数据中? [复制]

数据可视化分析历年电影票房(数据读取过滤分类绘图)

Weka 过滤分类器 arrayOutOfBoundsException