闪亮:如何调整tabsetPanel的宽度?

时间:2013-09-30 14:12:35

标签: r shiny

这是我的网站中嵌入的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()))
})

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:17)

我现在想一想,我在Shiny Homepage上检查应用程序的html代码。通过将选项<div>设置为classspan1span2等,使用html中的span3(文档分部)标记调整tabsetPanel 。 span的后缀越高,除法的宽度越大。我只需在我的ui.R代码中添加div()标记:

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

答案 1 :(得分:8)

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

添加

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

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

答案 2 :(得分:2)

上述示例在某些情况下有效。我需要一个更通用的解决方案。以下解决方案可以适应任何情况。例如,您的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", .....)
        )
    )
)

答案 3 :(得分:1)

调整sidebarPaneltabsetPanel宽度的另一种方法是基于分别修改widthcol-sm-4 CSS类的col-sm-8属性。
使用tag$headtag$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"))
    ))
)
)