如果声明不起作用

时间:2017-09-06 14:36:59

标签: r if-statement shiny reactive

我正在创建一个Shiny App,它从两个输入开始创建一个有序列表。该列表用于对依赖于它们的关系的数据库对象进行排序。

由于我确定了可能的输入,我已经插入了一些if语句但是没有用(我得到的结果是整个数据集而不是我选择的部分)。

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

  • 环境(EnvName,A1,A2,A3作为列),我的想法是我需要完成添加环境的完整模式名称,并根据区域更改env前缀
  • 关系:所有父子关系的列表 -Schemas:所有模式名称的列表,分为不同的区域,第1部分和第2部分,以允许构建全名。

编辑:我已更新代码。请注意,我已尝试使用RunOrder作为被动部分的输出,但我在闪亮的应用程序而不是表格中出现此错误:

'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))

1 个答案:

答案 0 :(得分:1)

我设法修复了代码,我发布这个代码以防其他人有用。问题出现在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))