如果语句在 Shiny 中不起作用

If statements not working in Shiny

我正在创建一个闪亮的应用程序,它从两个输入开始创建一个有序列表。该列表旨在根据它们的关系对数据库对象进行排序。

由于我正在定义可能的输入,所以我插入了一些 if 语句,但这不起作用(结果我得到的是整个数据集,而不是我选择的部分)。

我正在使用三个主要数据框:

编辑:我更新了代码。请注意,我尝试使用 RunOrder 作为反应部分的输出,但我在闪亮的应用程序而不是 table:

中得到了这个错误
'data' must be 2-dimensional (e.g. data frame or matrix)

下面的代码

library(shiny)

library(shinydashboard)
library(dplyr)



ui <- shinyUI(pageWithSidebar(
  headerPanel("Run Order List"),
  sidebarPanel(
    #declare 2 text inputs and submit button
    selectInput("EnvName", "Environment:", c("Env")),
    selectInput("Area", "Area:", c("A1", "A2","A3")),

    actionButton(
      inputId = "submit_loc",
      label = "Submit")

  ),
  mainPanel(
    DT::dataTableOutput("RunOrder")

  )
))

##
server <- shinyServer(function(input, output, session) {


  observeEvent(
    eventExpr = input[["submit_loc"]],
    handlerExpr = {

      input$EnvName
      input$Area

  #Loading the data



      Relationships <- read.csv("myfile", header = TRUE, sep = ";")
      Environments <- read.csv("myfile2", header = TRUE, sep = ";")
      Schemas <- read.csv("myfile3", header = TRUE, sep = ";")

  ##CreatingListofSchemas

  out <- reactive({  
    if (input$Area == "A1")
      {
        Environments <-subset(Environments, EnvName == input$EnvName)
        Relevant_schemas <- subset(Schemas, Area == "A1")
        Relevant_schemas $FullName <- paste(Relevant_schemas $Part1,Environment$A1,Relevant_schemas$Part2, sep="")
      }
    else if(input$Area == "A2"){

               Environments <-subset(Environments, EnvName == input$EnvName)
                            Relevant_schemas <- subset(Schemas, Area == "A2")
                            Relevant_schemas$FullName <- paste(Environment$A2,Relevant_schemas$Part1, sep="")
                            }
    else{

      Environments <-subset(Environments, EnvName == input$EnvName)
      Relevant_schemas <- subset(Schemas, Area == "A3")
      Relevant_schemas $FullName <- paste(Relevant_schemas$Part1,Environment$A3,Csa_schemas$Part2, sep="")}

    return(Relevant_schemas$FullName)
  })

  #Selecting environment

  Relationships <- subset(Relationships, grepl(paste(Relevant_schemas$FullName, collapse = "|"), Relationships$ParentDB))

 Level1Tables <- subset(Relationships, select = c(Parent),  !(Parent %in% c(Child)))

  Level1Tables <- unique(Level1Tables)
  colnames(Level1Tables)<- "tables"
  Level1Tables$Level <- 1

  RunOrder <- Level1Tables

  RemainingTables <- anti_join(AllTables, RunOrder, by = NULL, copy = TRUE)}


    output$RunOrder = DT::renderDataTable({
      RunOrder
    })
    })
})
##
shinyApp(ui = ui, server = server)

下面的小型可重现示例(已修复且当前有效)。但是,应用程序仍然没有响应输入:

ParentDB <- c('s1A1p1', 'A2', 'A3')
ParentTable <- c('Tab1', 'Tab2', 'Tab3')
Relationships <- data.frame(ParentDB, ParentTable)

EnvName <- c("Env")
A1 <- c('A1')
A2 <- c('A2')
A3 <- c('A3')

Environments <- data.frame(EnvName, A1,A2, A3)

Part1 <- c('s1', 's2','s3')
Part2 <- c('p1', 'p2','p3')
Area <- c('A1','A2', 'A3')
SchemaList <- data.frame(Part1,Part2,Area)
Schemas <- SchemaList

Area <- "A1"
if(Area =="A1")
{
  Environments <-subset(Environments, EnvName == "Env")
  SchemaList <- subset(Schemas, Area == "A1")
  SchemaList$FullName <- paste(SchemaList$Part1,Environments$A1,SchemaList$Part2, sep="")
} else if(Area == "A2")
    {

  Environments <-subset(Environments, EnvName == "Env")
  SchemaList <- subset(Schemas, Area == "A2")
  SchemaList$FullName <- paste(Environments$A2,SchemaList$Part1, sep="")
} else {

  Environments <-subset(Environments, EnvName == "Env")
  SchemaList <- subset(Schemas, Area == "A3")
  SchemaList$FullName <- paste(SchemaList$Part1,Environments$A3,SchemaList$Part2, sep="")}


#Selecting environment

Relationships <- subset(Relationships, grepl(paste(SchemaList$FullName, collapse = "|"), Relationships$ParentDB))

我设法修复了代码,我将其发布以防它对其他人有用。问题出在 if 语句和反应函数中。我没有使用反应函数,而是将输入分配给一个变量,然后我将其用于执行其他操作。

下面更新代码的相关部分

server <- shinyServer(function(input, output, session) {


  observeEvent(
    eventExpr = input[["submit_loc"]],
    handlerExpr = {

      AreaSelect <- input$Area
      EnvSelect <- input$EnvName

    if (AreaSelect  == "A1")
      {
        Environments <-subset(Environments, EnvName == EnvSelect)
        Relevant_schemas <- subset(Schemas, Area == "A1")
        Relevant_schemas $FullName <- paste(Relevant_schemas $Part1,Environments$A1,Relevant_schemas$Part2, sep="")
      }     else if(AreaSelect == "A2"){

               Environments <-subset(Environments, EnvName == EnvSelect)
                            Relevant_schemas <- subset(Schemas, Area == "A2")
                            Relevant_schemas$FullName <- paste(Environments$A2,Relevant_schemas$Part1, sep="")
   }     else {

      Environments <-subset(Environments, EnvName == EnvSelect)
      Relevant_schemas <- subset(Schemas, Area == "A3")
      Relevant_schemas $FullName <- paste(Relevant_schemas$Part1,Environments$A3,Csa_schemas$Part2, sep="")}

    return(Relevant_schemas$FullName)


  #Selecting environment

  Relationships <- subset(Relationships, grepl(paste(Relevant_schemas$FullName, collapse = "|"), Relationships$ParentDB))