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

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

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

问题

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

  1. library(tidyverse)
  2. library(shiny)
  3. library(shinythemes)
  4. library(data.table)
  5. library(ggplot2)
  6. library(GGally)
  7. main_page <- tabPanel(
  8. title = "Analysis",
  9. titlePanel("Analysis"),
  10. sidebarLayout(
  11. sidebarPanel(
  12. title = "Inputs",
  13. fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
  14. selectInput("site_var", "1. Identify the site variable", choices = NULL),
  15. selectInput("s_cov", "2. Select covariates", choices = NULL, multiple = TRUE),
  16. strong("3. Impose constraints:"), actionButton("constraintBtn", "", icon = icon("circle-plus"), class="btn btn-link"),
  17. uiOutput("constraintInputs"),
  18. actionButton("run_button", "Run Analysis", icon = icon("play"), class = "btn-primary")
  19. ),
  20. mainPanel(
  21. tabsetPanel(
  22. tabPanel(
  23. title = "Plot",
  24. plotOutput("s_plot")
  25. ),
  26. )
  27. )
  28. )
  29. )
  30. ui <- navbarPage(
  31. title = "Synthetic Purposive Sampling",
  32. theme = shinytheme('flatly'),
  33. main_page
  34. )
  35. server <- function(input, output){
  36. options(shiny.maxRequestSize=10*1024^2)
  37. data_input <- reactive({
  38. req(input$csv_input)
  39. read.csv(input$csv_input$datapath)
  40. })
  41. observeEvent(data_input(),{
  42. choices <- c(names(data_input()))
  43. choices_numeric <- c(names(data_input() %>% select(is.numeric)))
  44. updateSelectInput(inputId = "site_var", choices = choices)
  45. updateSelectInput(inputId = "s_cov", choices = choices_numeric)
  46. })
  47. constraints <- reactiveValues(inputs = list())
  48. observeEvent(input$constraintBtn, {
  49. constraints$inputs <- c(constraints$inputs, list(
  50. column(4, selectInput(paste0("var_", length(constraints$inputs) + 1), "Var:", choices = input$s_cov)),
  51. column(4, selectInput(paste0("txt_", length(constraints$inputs) + 1), "Condition:", choices = c("<", ">"), selected = NULL)),
  52. column(4, numericInput(paste0("val_", length(constraints$inputs) + 1), "Value:", value = NULL))
  53. ))
  54. })
  55. output$constraintInputs <- renderUI({
  56. if (length(constraints$inputs) > 0){
  57. lapply(constraints$inputs, function(x){
  58. fluidRow(x[[1]], x[[2]], x[[3]])
  59. })
  60. }
  61. })
  62. s_plot <- eventReactive(input$run_button,{
  63. ggpairs(data = data_input(), columns = c(input$s_cov))
  64. })
  65. output$s_plot <- renderPlot(s_plot())
  66. }
  67. shinyApp(ui = ui, server = server)

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

英文:

I have a following R shiny code:

  1. library(tidyverse)
  2. library(shiny)
  3. library(shinythemes)
  4. library(data.table)
  5. library(ggplot2)
  6. library(GGally)
  7. main_page &lt;- tabPanel(
  8. title = &quot;Analysis&quot;,
  9. titlePanel(&quot;Analysis&quot;),
  10. sidebarLayout(
  11. sidebarPanel(
  12. title = &quot;Inputs&quot;,
  13. fileInput(&quot;csv_input&quot;, &quot;Select CSV File to Import&quot;, accept = &quot;.csv&quot;),
  14. selectInput(&quot;site_var&quot;, &quot;1. Identify the site variable&quot;, choices = NULL),
  15. selectInput(&quot;s_cov&quot;, &quot;2. Select covariates&quot;, choices = NULL, multiple = T),
  16. strong(&quot;3. Impose constraints:&quot;), actionButton(&quot;constraintBtn&quot;, &quot;&quot;, icon = icon(&quot;circle-plus&quot;), class=&quot;btn btn-link&quot;),
  17. uiOutput(&quot;constraintInputs&quot;),
  18. actionButton(&quot;run_button&quot;, &quot;Run Analysis&quot;, icon = icon(&quot;play&quot;), class = &quot;btn-primary&quot;)
  19. ),
  20. mainPanel(
  21. tabsetPanel(
  22. tabPanel(
  23. title = &quot;Plot&quot;,
  24. plotOutput(&quot;s_plot&quot;)
  25. ),
  26. )
  27. )
  28. )
  29. )
  30. ui &lt;- navbarPage(
  31. title = &quot;Synthetic Purposive Sampling&quot;,
  32. theme = shinytheme(&#39;flatly&#39;),
  33. main_page
  34. )
  35. server &lt;- function(input, output){
  36. options(shiny.maxRequestSize=10*1024^2)
  37. data_input &lt;- reactive({
  38. req(input$csv_input)
  39. read.csv(input$csv_input$datapath)
  40. })
  41. observeEvent(data_input(),{
  42. choices &lt;- c(names(data_input()))
  43. choices_numeric &lt;- c(names(data_input() %&gt;% select(is.numeric)))
  44. updateSelectInput(inputId = &quot;site_var&quot;, choices = choices)
  45. updateSelectInput(inputId = &quot;s_cov&quot;, choices = choices_numeric)
  46. })
  47. constraints &lt;- reactiveValues(inputs = list())
  48. observeEvent(input$constraintBtn, {
  49. constraints$inputs &lt;- c(constraints$inputs, list(
  50. column(4, selectInput(paste0(&quot;var_&quot;, length(constraints$inputs) + 1), &quot;Var:&quot;, choices = input$s_cov)),
  51. column(4, selectInput(paste0(&quot;txt_&quot;, length(constraints$inputs) + 1), &quot;Condition:&quot;, choices = c(&quot;&lt;&quot;, &quot;&gt;&quot;), selected = NULL)),
  52. column(4, numericInput(paste0(&quot;val_&quot;, length(constraints$inputs) + 1), &quot;Value:&quot;, value = NULL))
  53. ))
  54. })
  55. output$constraintInputs &lt;- renderUI({
  56. if (length(constraints$inputs) &gt; 0){
  57. lapply(constraints$inputs, function(x){
  58. # fluidRow(column(width = 4, x[[1]]), column(width = 4, x[[2]]), column(width = 4, x[[3]]))
  59. fluidRow(x[[1]], x[[2]], x[[3]])
  60. #do.call(tagList, constraints$inputs)
  61. })
  62. }
  63. })
  64. s_plot &lt;- eventReactive(input$run_button,{
  65. ggpairs(data = data_input(), columns = c(input$s_cov))
  66. })
  67. output$s_plot &lt;- renderPlot(s_plot())
  68. }
  69. 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:

  1. output$constraintInputs <- renderUI({
  2. if(length(constraints$inputs) > 0) {
  3. do.call(fluidRow, constraints$inputs)
  4. }
  5. })
英文:

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

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

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:

确定