css - Shiny conditionalPanel 可以水平使用吗?

标签 css r shiny shiny-reactivity

我正在使用 sidebarLayout() 开发一个 Shiny 的应用程序,我希望根据 sidebarPanel() 中的输入值在 mainPanel() 中并排显示一个或两个绘图。

如果只显示一个图,我希望该图占据 mainPanel() 水平空间的 100%。但是,如果要显示两个图,我希望每个图都占用 mainPanel() 空间的 50%。

我还希望每个特定列的内容占据其自己列中的所有可用水平空间。

我已经尝试了一些东西。

  1. 有一个包含列的 fluidRow() 和一个 conditionalPanel()

    • 但是,我无法让它工作,因为 fluidRow() 希望每个元素都提供宽度参数,而 conditionalPanel() 似乎不兼容。
  2. conditionalPanel() 在 splitLayout() 的右边

    • 这只会隐藏右侧,不允许左侧的图占据所有 mainPanel() 空间。
  3. 右侧 div 内的 conditionalPanel() 显示:表格单元格

    • 但这和上面1的结果是一样的
library(ggplot2)
library(shiny)

ui <- fluidPage(
  tags$head(
    tags$style("
               #my_container {
                 display: table;
                 width: 100%;
               }

               .col {
                 display: table-cell;
               }

               #col_left {
                 background-color: #ffa;
               }

               #col_right {
                 background-color: #faf;
               }
               ")
  ), 

  sidebarPanel(
    checkboxInput("checkbox", 
                  "View Both", 
                  value = TRUE), 
    width = 2
  ), 

  mainPanel(
    div(id = "my_container", 
        div(id = "col_left", 
            class ="col", 
            plotOutput("plot_output_1")),
        div(id = "col_right", 
            class = "col", 
            conditionalPanel("input.checkbox == 1", 
                             plotOutput("plot_output_2")))
    ), 
    width = 10
  )
)

server <- shinyServer(function(input, output) {
  output$plot_output_1 <- renderPlot({
    ggplot(
      data.frame(x = runif(3), y = rnorm(3)), 
      aes(x = x, y = y)) + 
      geom_point()
  })

  output$plot_output_2 <- renderPlot({
    ggplot(
      data.frame(x = runif(3), y = rnorm(3)), 
      aes(x = x, y = y)) + 
      geom_point()
  })
})

shinyApp(ui, server)
  1. 我还尝试添加 javascript 消息来更改 div 宽度。
    • 这似乎在右列被隐藏而左列(示例为黄色背景)随后显示的意义上起作用。但是,尽管由于对输入的依赖性而重新绘制,但左列中的图并未重新绘制以占用新空间。
library(ggplot2)
library(shiny)

ui <- fluidPage(
  tags$head(
    tags$style("
               #my_container {
                 display: table;
                 width: 100%;
               }

               .my_col {
                 display: table-cell;
               }

               #col_left {
                 background-color: #ffa;
               }

               #col_right {
                 background-color: #faf;
               }
               "
    ), 

    tags$script("
      Shiny.addCustomMessageHandler('n_show.onchange', function(value) {
        var col_left = document.getElementById('col_left');
        var col_right = document.getElementById('col_right');

        if(value == 'one') {
          col_left.style.width = '100%';
          col_right.style.width = '0%';
        } else {
          col_left.style.width = '50%'; 
          col_right.style.width = '50%';
        }
      });
      "
    )
  ), 

  sidebarPanel(
    selectInput(inputId = "n_show", label = "Number of Plots", choices = c("one", "two"), selected = "two"), 
    width = 2
  ), 

  mainPanel(
    div(id = "my_container", 
        div(id = "col_left", 
            class = "my_col", 
            plotOutput("plot_output_1")),
        div(id = "col_right", 
            class = "my_col", 
            conditionalPanel("input.n_show == 'two'", 
                             plotOutput("plot_output_2")))
    ), 
    width = 10
  )
)

server <- shinyServer(function(input, output, session) {
  output$plot_output_1 <- renderPlot({
    input$n_show

    ggplot(
      data.frame(x = runif(3), y = rnorm(3)), 
      aes(x = x, y = y)) + 
      geom_point()
  })

  output$plot_output_2 <- renderPlot({
    input$n_show

    ggplot(
      data.frame(x = runif(3), y = rnorm(3)), 
      aes(x = x, y = y)) + 
      geom_point()
  })

  observeEvent(input$checkbox, {
    session$sendCustomMessage("n_show.onchange", input$n_show)
  })
})

shinyApp(ui, server)

我怀疑这并不像我做的那么难,但我的 CSS 知识/技能似乎无法胜任这项任务 - 至少在 shiny 的背景下是这样。

最佳答案

renderUI 的解决方案:

library(ggplot2)
library(shiny)

ui <- fluidPage(
  tags$head(
    tags$style("
               #col_left {
                 background-color: #ffa;
               }
               #col_right {
                 background-color: #faf;
               }
               ")
    ), 

  sidebarPanel(
    checkboxInput("checkbox", 
                  "View Both", 
                  value = TRUE), 
    width = 2
  ), 

  mainPanel(
    uiOutput("plots"), 
    width = 10
  )
    )

server <- shinyServer(function(input, output) {

  output$plot_output_1 <- renderPlot({
    ggplot(
      data.frame(x = runif(3), y = rnorm(3)), 
      aes(x = x, y = y)) + 
      geom_point(size = 6)
  })

  output$plot_output_2 <- renderPlot({
    ggplot(
      data.frame(x = runif(3), y = rnorm(3)), 
      aes(x = x, y = y)) + 
      geom_point(size = 6)
  })

  output$plots <- renderUI({
    if(input$checkbox){
      fluidRow(
        column(6, div(id="col_left", plotOutput("plot_output_1"))),
        column(6, div(id="col_right", plotOutput("plot_output_2")))
      )
    }else{
      fluidRow(
        column(12, div(id="col_left", plotOutput("plot_output_1")))
      )
    }
  })
})

shinyApp(ui, server)

enter image description here

关于css - Shiny conditionalPanel 可以水平使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56485302/

相关文章:

r - 根据过去的值创建新的数据框列

r - 使用辅助导航的 shiny.router 和 navbarPage 的 URI 路由

javascript - 如何在 Shiny 中获得一个按钮来并行调用 JavaScript 和 R 代码?

jquery - 修改 JQuery 选择样式的 UL 以在没有 BootstrapJS 的情况下工作

html - 位置 : absolute; inside dynamic position: relative; element

javascript - 内网网页第二个按钮点击失败

javascript - Wordpress 在不刷新页面的情况下获取图像类别

r - gauher(k) 错误 : object 'gh' not found using difGenLord function

RMarkdown : bookdown with plotly

r - 使用传单和R在 map 上计算,解码和绘制路线