r-按分类变量过滤后的图(error filter(),尝试应用非函数)



罗马历史爱好者在这里。因此,我创建了一个小数据帧,其中包含军团的部分legions(fifthtirteenth(和它们的morale(highmediumlow(。

我想想象一下各军团在士气上的差异。为此,我将为军团创建一个酒吧情节,通过士气过滤。

所以在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)

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

这显然是事件中的反应情节。

试试这个,它对我有用。

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")
})