Shiny R:fluidRow

时间:2015-06-12 10:04:06

标签: r shiny

我正在使用fluidRow / Column布局创建一个闪亮的应用程序!

在第一行中,我想包含一个包含一个图像图的输出! 第二行包含更多绘图和无功输入(selectInput)。

无论我尝试什么(renderPLot,renderImage,renderUI)返回绘图,我总是在第一行下面有一个空白区域,它会炸掉行 - .-

请注意,我为第一行中的绘图提供了绘图功能。情节本身看起来非常精细,它只是情节下方的白色空间......

任何建议可能是这种行为的原因 以及如何摆脱空白?

谢谢!

Server.R

source("plotFunction.R")

shinyServer(function(input, output) {

   output$plot1 <- renderImage({

         outfile <- tempfile(fileext='.png')
         png(outfile, width=400, height=150)
         plotFunction(col_palette = heat.colors(4))
         dev.off()


         # Return a list containing the filename
         list(src = outfile,
                  contentType = 'image/png',
                  width = 400,
                  height = 150)

  }, deleteFile = TRUE)  # end of render image

}) # end of shinyServer  

UI.R

shinyUI(fluidPage(

   # 1st row
  fluidRow(
             column(   12, 
                       align = "center",
                       imageOutput("plot1")
              ) # end of column
), # end of 1st row 


# 2nd row
fluidRow(   
          column(   1,
                    selectInput("someSelection", 
                    label = "Select smth",
                    choices = c("smth","smth more") ),

                    selectInput("anotherSelection", 
                    label = "Select more",
                    choices = c("more","and more") )

            ) # end of column 


  ) # end of fluidRow

)) # end of shiny UI

plotFunction.R

 plotFunction <- function(col_palette) {

 mtrx = matrix(c(1,2,3,4),nrow = 4, ncol =3 ,byrow = T)

 par(oma = c(0,0,0,0),mar=c(2,2,0,2))  

 image(
        mtrx,  
        col = col_palette,
        xlab="",
        ylab = "",
        axes = F
   )
} # ned of plotFunction.R

HTML代码HTML Code

1 个答案:

答案 0 :(得分:6)

无论出于何种原因,放置图像的div的高度不会随着图像大小调整为400x150而调整大小。

我们可以通过使用HTML函数将图像放在div中,并使用一些内嵌CSS将高度修复为150px(或者您想要的任何其他内容)来为此做一个hacky修复。

library(shiny)

plotFunction <- function(col_palette) {
  mtrx = matrix(c(1,2,3,4),nrow = 4, ncol =3 ,byrow = T)
  par(oma = c(0,0,0,0),mar=c(2,2,0,2))  
  image(
    mtrx,  
    col = col_palette,
    xlab="",
    ylab = "",
    axes = F
  )
} # ned of plotFunction.R

server <- shinyServer(function(input, output) {
  output$plot1 <- renderPlot({
    plotFunction(col_palette = heat.colors(4))
  }, width = 400, height = 150)  # end of renderPlot
}) # end of shinyServer  

ui <- shinyUI(
  fluidPage(
    # 1st row
    fluidRow(   
      column(12,
        HTML("<div style='height: 150px;'>"),
        plotOutput("plot1"),
        HTML("</div>")
      )
    ),
    # 2nd row
    fluidRow(   
      column(1,
        selectInput("someSelection", 
          label = "Select smth",
          choices = c("smth","smth more")
        ),
        selectInput("anotherSelection", 
          label = "Select more",
          choices = c("more","and more")
        )
      ) # end of column 
    ) # end of fluidRow
  ) # end of fluidPage
) # end of shiny UI

shinyApp(ui = ui, server = server)