R Shiny RenderUI 输出格式问题:删除 html 文本并重塑外观

问题描述 投票:0回答:1

我有以下 R 闪亮代码:

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)

生成侧边栏菜单,如果用户点击加号,它会生成三个额外的输入元素,如下所示:

我想解决两个外观问题:

  1. 我想从输出中删除“div col-sm-4”文本。
  2. 我希望三个输入元素以相同的宽度(并且可能具有最小边距)出现在一行中

任何帮助将不胜感激!

html r shiny fluid-layout renderui
1个回答
0
投票

我没有你的 CSV 文件,所以我无法测试。我会尝试:

output$constraintInputs <- renderUI({
  if(length(constraints$inputs) > 0) {
    do.call(fluidRow, constraints$inputs)
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.