R亮直方图作为可渲染的元素

R亮直方图作为可渲染的元素,r,plot,grid,shiny,R,Plot,Grid,Shiny,我使用数据帧从用户界面接收输入。为了简化,假设它是两行两列。根据输入,我想绘制分布图。挑战在于,我需要以相同的方式呈现分布,即以“矩阵”的形式呈现 我在下面提供了一个可复制的示例。我从错误消息中了解到:一旦我在输入矩阵中输入数据,接收矩阵就无法用绘图替换其元素(通常为数字)。也许我应该告诉接收数据帧,它的元素将不是“标准”,但我不知道如何。另外,也许我应该使用javascript或html来定义直方图 以下是我的ui.R和server.R的简化版本: ui.R: 服务器.R: shinyServ

我使用数据帧从用户界面接收输入。为了简化,假设它是两行两列。根据输入,我想绘制分布图。挑战在于,我需要以相同的方式呈现分布,即以“矩阵”的形式呈现

我在下面提供了一个可复制的示例。我从错误消息中了解到:一旦我在输入矩阵中输入数据,接收矩阵就无法用绘图替换其元素(通常为数字)。也许我应该告诉接收数据帧,它的元素将不是“标准”,但我不知道如何。另外,也许我应该使用javascript或html来定义直方图

以下是我的ui.R和server.R的简化版本:

ui.R:

服务器.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
})

})   
shinyServer(
功能(输入、输出){

输出$decision_Matrix如果使用
ggplot
(我会使用
grid.arrange
),我就不需要做那么多的研究了,但这也非常简单

请注意,我将第二个
可渲染
更改为
renderPlot
。我还使用
layout
对基础图形进行了排列,然后使用名为
recordPlot
gridBase
函数将基础图形转换为基于网格的绘图,并返回该结果

根据请求,我还将绘图缩放为800像素宽,并在
可渲染
上添加了一些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)
库(闪亮)
图书馆(网格)
图书馆(gridBase)

u您可以设置断点以查看
n
的值和类别。不清楚您希望在矩阵图[i,j]中有什么值。非常感谢Mike Wise。这确实很有效。我正在考虑使用Renderable而不是renderPlot,这样绘图在水平方向上与输入矩阵具有相同的维度。目前,绘图比页面顶部的输入数据框宽得多。我想我可以使用fluidPage和column来确保y align?我通过指定图像的大小,将两个元素的宽度调整为相同的,
renderable
,以及
renderPlot
。还有其他方法可以做到这一点,但我认为这是在不获得所有引导的情况下最简单的方法。这是一个优雅的解决方案!后续问题:是可以将图显示在输入表的顶部(而不是下面的单独面板)。我的意思是,例如,第一个柱状图将显示在表中[1,1]的位置,就在输入的下方(“上面示例中的“5”),其他图也一样。图不必像上面的例子那样大,但应该放在数字输入下的表格中。我昨天花了一整天的时间都没有成功。谢谢。
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)