【问题标题】:Shiny: How to adjust the width of the tabsetPanel?Shiny:如何调整 tabsetPanel 的宽度?
【发布时间】:2013-09-30 14:12:35
【问题描述】:

这是我的app 嵌入在我的网站中。我想摆脱我的应用程序下方的滚动小部件,这是由于 tabsetPanel 的宽度造成的。

我使用此代码嵌入应用程序:

<iframe width="800" height="480" frameborder="0" src="http://spark.rstudio.com/alstated/meanvar/"></iframe>

应用代码:

ui.R

library(shiny)

# Define UI for application that plots random distributions 
shinyUI(pageWithSidebar(
  headerPanel(title = ""),
  sidebarPanel(
    sliderInput("size",
                "Number of Observations",
                min = 10,
                max = 200,
                value = 95),

    sliderInput("mu",
                "Mean",
                min = -100, 
                max = 100,
                value = 0),

    sliderInput("sd",
                "Standard Deviation",
                min = 1,
                max = 6,
                value = 3),

    checkboxInput(inputId = "indiv_obs",
                  label = "Show individual observations",
                  value = FALSE),

    checkboxInput(inputId = "density",
                  label = "Show density estimate",
                  value = FALSE),

    conditionalPanel(condition = "input.density == true",
                     sliderInput(inputId = "bw_adjust",
                                 label = "Bandwidth Adjustment",
                                 min = 0.2,
                                 max = 2,
                                 value = 1,
                                 step = 0.2))
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Plot", 
               plotOutput(
                 outputId = "histogram", 
                 height = "400px",
                 width = "400px")),
      tabPanel("Summary",
               verbatimTextOutput(outputId = "datsummary"))
    ))
)
)

server.R

library(shiny)

# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {

  data <- reactive(rnorm(
    n = input$size, 
    mean = input$mu, 
    sd = input$sd
  ))

  output$histogram <- renderPlot({

    hist(data(),
         probability = TRUE,
         xlab = "Data",
         ylab = "Density",
         main = "Histogram of Random Samples")

    if(input$indiv_obs){
      rug(data())
    }

    if(input$density){
      dens <- density(data(), adjust = input$bw_adjust)
      lines(dens, col = "blue")
    }

  })

  output$datsummary <- renderPrint(summary(data()))
})

非常感谢任何帮助!

【问题讨论】:

  • 我刚试了一下,在应用的右侧,而不是下方有一个滚动条

标签: r shiny


【解决方案1】:

我现在想通了,我在 Shiny Homepage 上检查了应用程序的 html 代码。并且通过将选项class 设置为span1、span2、span3 等,使用html 中的&lt;div&gt;(文档划分)标签调整tabsetPanel。 span 的后缀越高,分割的宽度就越大。我只是在我的 ui.R 代码中添加 div() 标签:

div(
      tabsetPanel(
        tabPanel("Plot", 
                 plotOutput(
                   outputId = "histogram", 
                   height = "400px",
                   width = "400px")),
        tabPanel("Summary",
                 verbatimTextOutput(outputId = "datsummary"))
        ), class = "span7")

【讨论】:

  • 我发现这个答案很有帮助。我还想指出关于 google 群组的方便讨论:goo.gl/L8vuOf
  • 我可以继续这个问题并询问如何调整height吗?
  • 我意识到这个答案是很久以前的了。我刚刚尝试过,但它对我不起作用。 @ChriiSchee 下面的回答效果很好。
【解决方案2】:

另一种调整宽度(和高度)的方法:

添加

 ),  style='width: 1000px; height: 1000px' # end of tabsetPanel

在 tabsetPanel 之后,这种情况下不需要 div()

【讨论】:

  • 这会将样式转移到我相信 tabsetpanel 所在的下一个元素。在我的情况下,tabsetpanel 位于一个具有它自己的样式 = 'width: 255px; 的列内。等'并导致样式错误。检查元素时,列应警告:height:400px width:255px, without the required ;将它们分开,显然面板的高度会移动到包装柱。所以你确实需要 div
【解决方案3】:

上述示例在某些情况下有效。我需要一个更通用的解决方案。以下解决方案可以针对任何情况进行定制。例如,您的 Shiny 应用仍然可以响应。

我的解决方案

首先,在 tabsetPanel 之前创建一个 div 并给它一个类。其次,编写一段 jQuery/javascript 来查找该 div 的父元素。第三,使用 javascript/jQuery 改变这个父元素的类。您可以将类更改为您喜欢的任何内容,例如 col-sm-12、col-sm-11、其他一些标准 Bootstrap 类,或者您可以添加和创建自己的类。第四,将自定义 jQuery/javascript 添加到您的 Shiny 应用程序(确保将 .js 文件保存在 Shiny 应用程序的 www 文件夹中)。

这是我的例子:

Javascript/jQuery (general.js):

$(function (){
    $("div.delParentClass").parent().removeClass("col-sm-8").addClass("col-sm-12");

/* In addClass you can specify the class of the div that you want. In this example, 
I make use of a standard Bootstrap class to change the width of the tabset: col-sm-12. 
If you want the tabset to be smaller use: col-sm-11, col-sm-10, ... or add
and create your own class. */

});

闪亮的用户界面

mainPanel(
    tags$head(tags$script(src="general.js")),
    div(class="delParentClass",
        tabsetPanel(
          tabPanel("Some panel", .....),
          tabPanel("Some panel", .....),
          tabPanel("Some panel", .....)
        )
    )
)

【讨论】:

    【解决方案4】:

    调整sidebarPanel 和tabsetPanel 宽度的另一种方法是分别修改col-sm-4 和col-sm-8 CSS 类的width 属性。
    使用 tag$head 和 tag$style 可以将 CSS 直接添加到 Shiny UI。
    详情请见https://shiny.rstudio.com/articles/css.html。
    这不是一个优雅的解决方案,但它可以正常工作。

    闪亮的用户界面

    shinyUI(fluidPage(
      tags$head(
        tags$style(HTML("   
         .col-sm-4 { width: 25%;}
         .col-sm-8 { width: 75%;}
        "))
      ),       
      headerPanel(title = ""),
      sidebarPanel(
        sliderInput("size",
                    "Number of Observations",
                    min = 10,
                    max = 200,
                    value = 95),
    
        sliderInput("mu",
                    "Mean",
                    min = -100, 
                    max = 100,
                    value = 0),
    
        sliderInput("sd",
                    "Standard Deviation",
                    min = 1,
                    max = 6,
                    value = 3),
    
        checkboxInput(inputId = "indiv_obs",
                      label = "Show individual observations",
                      value = FALSE),
    
        checkboxInput(inputId = "density",
                      label = "Show density estimate",
                      value = FALSE),
    
        conditionalPanel(condition = "input.density == true",
                         sliderInput(inputId = "bw_adjust",
                                     label = "Bandwidth Adjustment",
                                     min = 0.2,
                                     max = 2,
                                     value = 1,
                                     step = 0.2))
      ),
      mainPanel(
        tabsetPanel(
          tabPanel("Plot", 
                   plotOutput(
                     outputId = "histogram", 
                     height = "400px",
                     width = "400px")),
          tabPanel("Summary",
                   verbatimTextOutput(outputId = "datsummary"))
        ))
    )
    )
    

    【讨论】:

      猜你喜欢
      • 2016-06-06
      • 2018-05-29
      • 1970-01-01
      • 2021-08-31
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      相关资源
      最近更新 更多