2

I'm creating a Shiny App that creates a ordered list starting from two inputs. The list is meant to order database objects depening on their relation.

Since I'm definining the possible inputs, I have inserted some if statements but that's not working (I get as a result the whole dataset instead of the portion I have selected).

I'm working with three main dataframes:

  • Environments (EnvName, A1, A2, A3 as columns), the idea is that I need to complete the full schema name adding the environment and the env prefix changes depending on the area
  • Relationships: list of all parent child relationships -Schemas: list of all the schema names, divided in different areas and part 1 and part 2 to allow the construction of the full name.

Edit: I have updated the code. Note that I have tried using the RunOrder as the output of the reactive part but I got this error in the shiny app instead of the table:

'data' must be 2-dimensional (e.g. data frame or matrix)

Code below

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)

Small reproducible example below (fixed and currently working). The app, however, is still not responding to the inputs:

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))
Barbara
  • 1,118
  • 2
  • 11
  • 34
  • Most of the details of your code are not relevant to your actual question. It would be helpful if you could make a [mcve] with an emphasis on *minimal*. – John Coleman Sep 06 '17 at 14:41
  • Where is the `RunOrder` being defined in your code? (i meant in the server). The `reactive` output is `out` – akrun Sep 06 '17 at 14:45
  • @akrun I have now added the part on RunOrder and edited the section. – Barbara Sep 06 '17 at 16:07
  • @John Coleman I have tried adding an additional part to the code so that it can be reproduced. I would prefer not making it more minimal because I don't want to lose part of the information. I have seen many similar answers but nothing that worked for me. – Barbara Sep 06 '17 at 16:07
  • 1
    @Barbara It isn't really reproducible since it would require the setting up of a shiny server and the creation of several under-specified text files and dataframes. Your actual question seems to be about the semantics of `if`. Surely you can illustrate the problem with structures that are easy to reproduce (by just copy-pasting the code) rather than what might be hours of work to *maybe* reproduce your situation. – John Coleman Sep 06 '17 at 16:15
  • @JohnColeman I have now added a smaller example, that should be reproducible – Barbara Sep 06 '17 at 16:30
  • @Barbara Thanks! Unfortunately, I have to teach a couple of classes this afternoon so can't look at it right now. If no one has answered it by then, I'll look at it later on today. – John Coleman Sep 06 '17 at 16:40
  • @JohnColeman Ok thanks! – Barbara Sep 06 '17 at 16:42
  • What you have still isn't reproducible. In R "reproducible" means that someone can copy-paste your code into their own R-console and it would run. Your second example has a syntax error with the `else` and, more importantly, it depends on objects such as `Environment` which are unspecifiedd. Perhaps this might help: https://stackoverflow.com/q/5963269/4996248 – John Coleman Sep 06 '17 at 20:09
  • @JohnColeman I have now updated the code. The if statement is fixed and working properly but not the application, so there is something wrong with the reactive part. – Barbara Sep 07 '17 at 06:56

1 Answers1

2

I managed to fixed the code, I'm posting this in case it's useful for somebody else. The problem was both in the if statement and in the reactive function. Instead of using the reactive function I have assigned the inputs to a variable that I have then used for doing the other operations.

Relevant part of the updated code below

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))
Barbara
  • 1,118
  • 2
  • 11
  • 34