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