R Shiny RenderUI 输出格式问题:删除 HTML 文本并重新格式化外观。

huangapple go评论70阅读模式
英文:

R Shiny RenderUI output formatting issue: removing an html text and reforming the appearances

问题

以下是您的R Shiny代码的翻译部分:

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 = TRUE),
      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(x[[1]], x[[2]], x[[3]])
      })
    }
  })
  
  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)

希望这对您有所帮助。如果您有其他问题,请随时提问。

英文:

I have a following R shiny code:

library(tidyverse)
library(shiny)
library(shinythemes)
library(data.table)
library(ggplot2)
library(GGally)
main_page &lt;- tabPanel(
title = &quot;Analysis&quot;,
titlePanel(&quot;Analysis&quot;),
sidebarLayout(
sidebarPanel(
title = &quot;Inputs&quot;,
fileInput(&quot;csv_input&quot;, &quot;Select CSV File to Import&quot;, accept = &quot;.csv&quot;),
selectInput(&quot;site_var&quot;, &quot;1. Identify the site variable&quot;, choices = NULL),
selectInput(&quot;s_cov&quot;,  &quot;2. Select covariates&quot;,      choices = NULL, multiple = T),
strong(&quot;3. Impose constraints:&quot;), actionButton(&quot;constraintBtn&quot;, &quot;&quot;, icon = icon(&quot;circle-plus&quot;), class=&quot;btn btn-link&quot;),
uiOutput(&quot;constraintInputs&quot;),
actionButton(&quot;run_button&quot;, &quot;Run Analysis&quot;, icon = icon(&quot;play&quot;), class = &quot;btn-primary&quot;)
),
mainPanel(
tabsetPanel(
tabPanel(
title = &quot;Plot&quot;,
plotOutput(&quot;s_plot&quot;)
),
)
)
)
)
ui &lt;- navbarPage(
title = &quot;Synthetic Purposive Sampling&quot;,
theme = shinytheme(&#39;flatly&#39;),
main_page
)
server &lt;- function(input, output){
options(shiny.maxRequestSize=10*1024^2) 
data_input &lt;- reactive({
req(input$csv_input)
read.csv(input$csv_input$datapath)
})
observeEvent(data_input(),{
choices &lt;- c(names(data_input()))
choices_numeric &lt;- c(names(data_input() %&gt;% select(is.numeric)))
updateSelectInput(inputId = &quot;site_var&quot;, choices = choices)
updateSelectInput(inputId = &quot;s_cov&quot;,  choices = choices_numeric)
})
constraints &lt;- reactiveValues(inputs = list())
observeEvent(input$constraintBtn, {
constraints$inputs &lt;- c(constraints$inputs, list(
column(4, selectInput(paste0(&quot;var_&quot;,  length(constraints$inputs) + 1), &quot;Var:&quot;, choices = input$s_cov)),
column(4, selectInput(paste0(&quot;txt_&quot;,  length(constraints$inputs) + 1), &quot;Condition:&quot;, choices = c(&quot;&lt;&quot;, &quot;&gt;&quot;), selected = NULL)),
column(4, numericInput(paste0(&quot;val_&quot;, length(constraints$inputs) + 1), &quot;Value:&quot;, value = NULL))
))
})
output$constraintInputs &lt;- renderUI({
if (length(constraints$inputs) &gt; 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 &lt;- eventReactive(input$run_button,{
ggpairs(data = data_input(), columns = c(input$s_cov))
})
output$s_plot       &lt;- 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:

R Shiny RenderUI 输出格式问题:删除 HTML 文本并重新格式化外观。

I'm looking to fix two issues with the appearance:

  1. I would like to remove the "div col-sm-4" texts from the output.
  2. I would like the three input elements to appear in a single row with equal widths (and possibly with a minimum margin)

Any help would be greatly appreciated!

答案1

得分: 1

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)
  }
})
英文:

I don't have your CSV file so I can't test. I would try:

output$constraintInputs &lt;- renderUI({
  if(length(constraints$inputs) &gt; 0) {
    do.call(fluidRow, constraints$inputs)
  }
})

huangapple
  • 本文由 发表于 2023年5月7日 14:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定