I have a following R shiny code:
library(tidyverse)
library(shiny)
library(shinythemes)
library(data.table)
library(ggplot2)
library(GGally)
main_page <- tabPanel(
title = "Analysis",
titlePanel("Analysis"),
sidebarLayout(
sidebarPanel(
title = "Inputs",
fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
selectInput("site_var", "1. Identify the site variable", choices = NULL),
selectInput("s_cov", "2. Select covariates", choices = NULL, multiple = T),
strong("3. Impose constraints:"), actionButton("constraintBtn", "", icon = icon("circle-plus"), class="btn btn-link"),
uiOutput("constraintInputs"),
actionButton("run_button", "Run Analysis", icon = icon("play"), class = "btn-primary")
),
mainPanel(
tabsetPanel(
tabPanel(
title = "Plot",
plotOutput("s_plot")
),
)
)
)
)
ui <- navbarPage(
title = "Synthetic Purposive Sampling",
theme = shinytheme('flatly'),
main_page
)
server <- function(input, output){
options(shiny.maxRequestSize=10*1024^2)
data_input <- reactive({
req(input$csv_input)
read.csv(input$csv_input$datapath)
})
observeEvent(data_input(),{
choices <- c(names(data_input()))
choices_numeric <- c(names(data_input() %>% select(is.numeric)))
updateSelectInput(inputId = "site_var", choices = choices)
updateSelectInput(inputId = "s_cov", choices = choices_numeric)
})
constraints <- reactiveValues(inputs = list())
observeEvent(input$constraintBtn, {
constraints$inputs <- c(constraints$inputs, list(
column(4, selectInput(paste0("var_", length(constraints$inputs) + 1), "Var:", choices = input$s_cov)),
column(4, selectInput(paste0("txt_", length(constraints$inputs) + 1), "Condition:", choices = c("<", ">"), selected = NULL)),
column(4, numericInput(paste0("val_", length(constraints$inputs) + 1), "Value:", value = NULL))
))
})
output$constraintInputs <- renderUI({
if (length(constraints$inputs) > 0){
lapply(constraints$inputs, function(x){
# fluidRow(column(width = 4, x[[1]]), column(width = 4, x[[2]]), column(width = 4, x[[3]]))
fluidRow(x[[1]], x[[2]], x[[3]])
#do.call(tagList, constraints$inputs)
})
}
})
s_plot <- eventReactive(input$run_button,{
ggpairs(data = data_input(), columns = c(input$s_cov))
})
output$s_plot <- renderPlot(s_plot())
}
shinyApp(ui = ui, server = server)
that generates a sidebar menu, where, if a user clicks on a plus sign, it generates three additional input elements that look like this:
I'm looking to fix two issues with the appearance:
Any help would be greatly appreciated!
I don't have your CSV file so I can't test. I would try:
output$constraintInputs <- renderUI({
if(length(constraints$inputs) > 0) {
do.call(fluidRow, constraints$inputs)
}
})