2016-12-20 91 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