"Error in server: unused arguments" - R 闪亮应用程序错误(服务器功能包括条件语句)

"Error in server: unused arguments" - R shiny app error (server function includes conditional statements)

我是 R shiny 的新手。我尝试创建一个应用程序,它根据 4 个参数给我排名 table。我已成功创建 UI 部分(输入部分)。但是,我无法 link 它正确地输出 table 。正如我之前提到的,我是 shiny 的新手,我无法理解错误。 当我 运行 应用程序时,Web 应用程序显示一秒钟然后消失,R 会话保持 运行 一条警告消息(下方)。

Warning: Error in server: unused arguments (input = list(<environment>, TRUE, function (x) 
x), output = list(<environment>, function (x) 
x))
  [No stack trace available]
Error in server(...) : 
  unused arguments (input = list(<environment>, TRUE, function (x) 
x), output = list(<environment>, function (x) 
x))

我的代码(我检查了 "ranks" 函数,它工作正常)

library(shiny)

rank_master = read.csv("master_rank.csv")

# Define UI for application that draws a histogram
ui <- fluidPage(
    titlePanel("Find the Best Supplier"),
    sidebarLayout(
        sidebarPanel(
            sliderInput("width", "Width", 28, 80, 40, post = " inches"),
            radioButtons("alloy", "Alloy Category: 0 = Low C & 1 = High C",
                         choices = c(0, 1), selected = 0),
            sliderInput("top", "Top X Suppliers",1,15,5, pre = "x = "),
            sliderInput("coil_ratio", "Exclusion", 0, 0.5, 0.1, 0.05)
        ),
        mainPanel("Best Suppliers for the Specified Parameters", 
                  dataTableOutput("results")
                  )

    )
)

# Define server logic required to draw a histogram
server <- function(width, alloy, top, coil_ratio){

    output$results = renderDataTable({

    new_table = subset(rank_master, ratio >= coil_ratio)   
    if (width >= 28 & width < 35 & alloy == 0){
        rank = as.data.frame(head(subset(new_table, width >= 28 & width < 35 & alloy_cat == 0, select = 
                                             c(supplier, mean_width_var, sd_width_var, rms, n_coils), top)))
        row.names(rank) = 1:nrow(rank)
        return(rank)
    } else if (width >= 28 & width < 35 & alloy == 1){
        rank1= as.data.frame(head(subset(new_table, width >= 28 & width < 35 & alloy_cat == 1, select =
                                             c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank1) = 1:nrow(rank1)
        return(rank1)
    } else if (width >= 35 & width < 50 & alloy == 0){
        rank2 = as.data.frame(head(subset(new_table, width >= 35 & width < 50 & alloy_cat == 0, select =
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank2) = 1:nrow(rank2)
        return(rank2)
    } else if (width >= 35 & width < 50 & alloy == 1){
        rank3 = as.data.frame(head(subset(new_table, width >= 35 & width < 50 & alloy_cat == 1, select = 
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils), top)))
        row.names(rank3) = 1:nrow(rank3)
        return(rank3)
    } else if (width >= 50 & width < 60 & alloy == 0){
        rank4 = as.data.frame(head(subset(new_table, width >= 50 & width < 60 & alloy_cat == 0, select =
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank4) = 1:nrow(rank4)
        return(rank4)
    } else if (width >= 50 & width < 60 & alloy == 1){
        rank5 = as.data.frame(head(subset(new_table, width >= 50 & width < 60 & alloy_cat == 1, select =
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank5) = 1:nrow(rank5)
        return(rank5)
    } else if (width >= 60 & width < 65 & alloy == 0){
        rank6 = as.data.frame(head(subset(new_table, width >= 60 & width < 65 & alloy_cat == 0, select =
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank6) = 1:nrow(rank6)
        return(rank6)
    } else if (width >= 60 & width < 65 & alloy == 1){
        rank7 = as.data.frame(head(subset(new_table, width >= 60 & width < 65 & alloy_cat == 1, select =
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank7) = 1:nrow(rank7)
        return(rank7)
    } else if (width >= 65 & width < 70 & alloy == 0){
        rank8 = as.data.frame(head(subset(new_table, width >= 65 & width < 70 & alloy_cat == 0, select =
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank8) = 1:nrow(rank8)
        return(rank8)
    } else if (width >= 65 & width < 70 & alloy == 1){
        rank9 = as.data.frame(head(subset(new_table, width >= 65 & width < 70 & alloy_cat == 1, select =
                                              c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank9) = 1:nrow(rank9)
        return(rank9)
    } else if (width >= 70 & width < 80 & alloy == 0){
        rank10 = as.data.frame(head(subset(new_table, width >= 70 & width < 80 & alloy_cat == 0, select =
                                               c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank10) = 1:nrow(rank10)
        return(rank10)
    } else if (width >= 70 & width < 80 & alloy == 1){
        rank11 = as.data.frame(head(subset(new_table, width >= 70 & width < 80 & alloy_cat == 1, select =
                                               c(supplier, mean_width_var, sd_width_var, rms, n_coils)), top))
        row.names(rank11) = 1:nrow(rank11)
        return(rank11)
    } else if(width < 28 | width > 80){
        "Please enter a width in the range of 28 to 80!"
    } else if(alloy != 0 | alloy != 1){
        "Incorrect Alloy Category! Please select 0 for Low Carbon Steel and 1 for High Carbon Steel"
    } else {
        "Incorrect parameters entered!"
    }
    })
}


shinyApp(ui, server)

谢谢:)

避免在服务器函数中放置额外的参数:

server <- function(width, alloy, top, coil_ratio){

应该是

server <- function(input, output) {

然后您可以在 output$results 函数中使用您的输入。例如if(input$width >= 28

我不知道 master_rank.csv 里有什么。如果您包含一个可重现的示例,我们可以更好地帮助您 https://whosebug.com/help/minimal-reproducible-example