2016-12-20 89 views
1

我使用數據框從用戶界面接收輸入。爲了簡化,假設它是2行和2列。根據投入,我想繪製分佈圖。挑戰在於我需要以相同的方式呈現分佈,即以「矩陣」的格式呈現。R作爲renderTable元素的閃亮直方圖

我在下面提供了一個可重現的例子。我從錯誤消息中得出的結論如下:一旦我在輸入矩陣中輸入數據,接收矩陣就不能通過圖來代替其元素(通常是數字)。可能我應該告訴接收的數據框,它的元素不會是「標準的」,但我不知道如何。另外,也許我應該使用JavaScript或HTML來定義直方圖?

這是我ui.R和server.R的簡化版本:

ui.R:

shinyUI(navbarPage(
    "My Application", 
    tabPanel("Component 1", 
     fluidPage(

     tableOutput('decision_Matrix'), 

     tableOutput('decision_Matrix_plots') 
     )), 
tabPanel("Component 2") 
)) 

server.R:

shinyServer(

function(input, output) { 

output$decision_Matrix <- renderTable({ 

    matrix_input <- matrix(data=NA, nrow=2,ncol=2) 

    for (j in 1:2) { 
    for (i in 1:2) { 

     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") 
    colnames(matrix_input) <- c("crit1","crit2") 

    matrix_input 

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

output$decision_Matrix_plots <- renderTable({ 

    matrix_plots <- data.frame() 

    for (j in 1:2) { 
    for (i in 1:2) { 

     n <- input[[paste0("C", j,"_A",i)]] 

     matrix_plots[i,j] <- hist(rnorm(n),breaks=10) # here is where it is not correct I think 

    }} 

    matrix_plots 
}) 

}) 

任何幫助將是很大的讚賞。

謝謝!

^h

enter image description here

+0

您可以設置斷點,看看有什麼N'的'值和類。 – Jean

+0

目前還不清楚你想在matrix_plots [i,j]中擁有什麼樣的價值。 –

回答

1

我就不用了,如果你使用ggplot(我剛使用grid.arrange),但這被證明是相當簡單過研究爲多。

請注意,我將您的第二個renderTable更改爲renderPlot。我還安排了基本圖形layout,然後使用gridBase函數recordPlot將基礎圖形轉換爲基於網格的圖形並返回。

根據要求,我還將圖繪製爲800像素寬,並在renderTable上添加了一些CSS樣式以匹配。

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

u <- shinyUI(navbarPage(
    "My Application", 
    tabPanel("Component 1", 
     fluidPage(
     tableOutput('decision_Matrix'), 
     tags$head(tags$style("#decision_Matrix table {background-color: lightgray; width: 800px; }",media = "screen",type = "text/css")), 
     plotOutput('decision_Matrix_plots') 
     )), 
tabPanel("Component 2") 
)) 

s <- shinyServer(

function(input,output) { 

    output$decision_Matrix <- renderTable({ 
    matrix_input <- matrix(data = NA,nrow = 2,ncol = 2) 
    for (j in 1:2) { 
     for (i in 1:2) { 
     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") 
    colnames(matrix_input) <- c("crit1","crit2") 
    matrix_input 
    },include.rownames = TRUE,sanitize.text.function = function(x) x) 

    output$decision_Matrix_plots <- renderPlot(width=800,height=500,{ 
    layout(matrix(c(1,2,3,4),nrow = 2,ncol = 2)) 
    for (j in 1:2) { 
     for (i in 1:2) { 
     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

+0

非常感謝Mike Wise。這確實很好。我正在考慮使用renderTable而不是renderPlot,以便繪圖與輸入矩陣具有相同的水平尺寸。目前,這些地塊比頁面頂部的輸入數據框要寬得多。我認爲我可以使用fluidPage和列來確保它們對齊? – user1431694

+1

我調整了兩個元素的寬度,使用CSS命令調整'renderTable',通過指定圖像的大小調整'renderPlot'。還有其他方法可以做到這一點,但這些都是我認爲最簡單的方法,而且不需要進行任何引導。 –

+0

這是一個優雅的解決方案! – user1431694