2016-12-22 19 views
2

以下のコードは、3行3列の入力テーブルを生成します(状況を簡略化するため、下の図を参照)。入力に基づいて、入力テーブルの下に9つのヒストグラムを生成する必要があります。重要なことは、ヒストグラムが入力ボックスと垂直に整列することです。現時点ではそうではありません。Shiny:renderTableのrownames()の幅を制御する方法は?

1つのオプションは、プロットを含める前に1のオフセットを使用することです。プロットが整列していることを確認するために、入力のrownames(alt1、alt2、a3)はオフセットと一致するように正確に1列を占めるように表示する必要があります。デフォルトでは、renderTableは自動調整を使用しているようです。私はオンラインで検索しましたが、rownamesの幅を制御する方法が見つかりませんでした。

誰でも助けてください。どんな入力も非常に高く評価されます。どうもありがとう。

library(shiny) 
library(grid) 
library(gridBase) 

u <- shinyUI(navbarPage(
"My Application", 
tabPanel("Component 1", 
     fluidPage(

     fluidRow(column(6, 
      tableOutput('decision_Matrix'),offset=0)  
     ), 
     fluidRow(column(5, 
      plotOutput('decision_Matrix_plots') 
      ,offset=1)  
     ) 
     )), 
tabPanel("Component 2") 
)) 

s <- shinyServer(

function(input,output) { 

    output$decision_Matrix <- renderTable({ 

     matrix_input <- matrix(data = NA,nrow = 3,ncol = 3) 
    for (j in 1:3) { 
    for (i in 1:3) { 
     matrix_input[i,j] <- paste0("<input id='C",j,"_A",i,"' type='number' class='form-control shiny-bound-input' value='",input[[paste0("C",j,"_A",i)]],"'>") 
    } 
    } 

    rownames(matrix_input) <- c("alt1","alt2","a3") 

    colnames(matrix_input) <- c("crit1","crit2","t3") 

    matrix_input 
},include.rownames = TRUE,sanitize.text.function = function(x) x) 

output$decision_Matrix_plots <- renderPlot({  

    layout(matrix(c(1,2,3,4,5,6,7,8,9),nrow = 3,ncol = 3)) 
    for (j in 1:3) { 
    for (i in 1:3) { 
     set.seed(123) 
     n <- input[[paste0("C",j,"_A",i)]] 
     if (is.null(n) || is.na(n) || n < 1) n <- 1 
     hist(rnorm(n),breaks = 10,main=sprintf("histogram of rnorm(%d)",n)) 
    } 
    } 
    recordPlot() 
}) 

}) 
shinyApp(ui = u,server = s) 

enter image description here

答えて

1

あなたはrownamesがあなたのアライメントをめちゃくちゃにされて言ったように。 fluidRowとcolumnを使用するShinyはBootstrapを使用しています。基本原理については、fluidRowとcolumnを参照してください。http://www.w3schools.com/bootstrap/bootstrap_grid_basic.asp

解決方法は、別の列にあなたのrownamesを置くことです。次の変更により、垂直方向の配置が固定されます。

... 
        fluidRow(column(1, 
            tableOutput('m_rownames'),offset=0, align="left"), 
          column(5, 
           tableOutput('decision_Matrix'),offset=0, align="right") 
        ), 
        fluidRow(column(5, 
            plotOutput('decision_Matrix_plots') 
            ,offset=1, align="right") 
        ) 
    .... 

      },include.rownames = FALSE,sanitize.text.function = function(x) x) 
      output$m_rownames <- renderTable({ c("alt1","alt2","a3") }) 

これは幅1の別の列にあなたのrownamesを置き、マトリクスとプロットはそれぞれ5つの対応する列に入れられます。しかし、これはあなたのrownamesと行列の水平方向の整列を混乱させる。私はこれを後で見る少しの時間があるかもしれませんが、今はそれを微調整するためにあなたに任せます。

これは、結果としてはるかにアライメントが最良の選択肢は3つの列を作ることです懸念しているよう enter image description here

+0

おかげエティエンヌですが、私は実際には同じ考えを持っていたが、それは数字の入力を含んでいるのでm_rownames内の行の高さは、テーブル内の行の高さは異なっています。私はこれを手動で修正するために以下を見つけました: "tags $ head(tags $ style(type =" text/css "、" #m_rownames table td(line-height:245%;} "))"。試行錯誤により245%が見つかりました。しかしこれは満足できるものではない。これは単純化されたケースです。私のテーブルでは、ラジオボタンを使用して垂直方向のスペースを増やしています。私はrownamesは別のテーブルにあるべきではないと思う。あなたがスムースを見つけたら教えてください。私もこれに取り組んでいます。 H – user1431694

+1

カラム内の項目の配置はブラウザによって決定され、ブラウザウィンドウのサイズ(および他のもの)に依存するため、項目を水平に並べることは非常に困難です。この依存関係を取り除くために 'fixedPage'を試すことができます。もう一つの選択肢はあなたがしたことです。その場合、ブラウザは行内の項目の配置を決定するので、同じ問題の2つのバージョンがあります。私はそこに解決策があるとは思わないが、HTMLやCSSフォーラムで見つけられるかもしれない。 –

1

です。それぞれには3つの入力と3つの対応する出力が含まれています。最初の列には、他の列の名前は表示されません。これにはいくつかの再編成が必要であり、最初の列が他の列と異なるため完全ではありません。ここでいくつかのコード

... 
    fluidRow(
      column(4, 
       tableOutput('decision_MatrixC1'),offset=0), 
      column(4, 
       tableOutput('decision_MatrixC2'),offset=0), 
      column(4, 
       tableOutput('decision_MatrixC3'),offset=0) 
    ), 
    fluidRow(
      column(3, 
       plotOutput('decision_Matrix_plotsC1') 
            ,offset=1), 
      column(4, 
       plotOutput('decision_Matrix_plotsC2') 
            ,offset=0), 
      column(4, 
       plotOutput('decision_Matrix_plotsC3') 
            ,offset=0) 
    ) 
    .... 
    output$decision_MatrixC1 <- renderTable({       
      matrix_input <- matrix(data = NA,nrow = 3,ncol = 1) 
      for (j in 1:1) { 
        for (i in 1:3) { 
           matrix_input[i,j] <- paste0("<input id='C",j,"_A",i,"' type='number' class='form-control shiny-bound-input' value='",input[[paste0("C",j,"_A",i)]],"'>") 
        }}       
      rownames(matrix_input) <- c("alt1","alt2","a3")       
      colnames(matrix_input) <- c("crit1") 

      matrix_input 
      },include.rownames = TRUE,sanitize.text.function = function(x) x) 

      output$decision_MatrixC2 <- renderTable({ 
        matrix_input <- matrix(data = NA,nrow = 3,ncol = 1) 
        for (j in 1:1) { 
          for (i in 1:3) { 
            matrix_input[i,j] <- paste0("<input id='C",j+1,"_A",i,"' type='number' class='form-control shiny-bound-input' value='",input[[paste0("C",j+1,"_A",i)]],"'>") 
          }}       
        rownames(matrix_input) <- c("alt1","alt2","a3") 
        colnames(matrix_input) <- c("crit2") 
        matrix_input 
      },include.rownames = FALSE,sanitize.text.function = function(x) x) 

とほぼ同じ第3列のプロット。明らかに、このコードではクリーンアップが必要ですが、できるだけコードの近くに置いておきたいと思いました。このような列に整理するのではなく、同様の方法で列に整理することができます。結果は

enter image description here

関連する問題