R Shiny,renderPlot 中的自定义网格grob

标签 r shiny

我正在尝试在 Shiny 中绘制自定义网格 grob 对象。

boxGrob <- function(labels, x=.5, y=.5) {
    grob(labels=labels, x=x, y=y, cl="box")
}

没有绘制任何内容,我没有收到任何错误。当然,我已经检查过它在 R 中是否有效。

任何的想法?

在下面的代码中,我尝试绘制 3 个图:
  • 一个现成的 grob 对象“gridPlot1”(有效),
  • 自定义 grob 对象“gridPlot2”( 不起作用 ),
  • 直接调用
    渲染自定义 grob 对象“gridPlot3”的一部分(有效)

  • ui.R
    library(shiny)
    
    # Define UI for application that draws a histogram
    shinyUI(fluidPage(
    
        # Show a plot of the generated distribution
        mainPanel(
          h1("gridPlot1: linesGrob"),
          plotOutput("gridPlot1"),
          h1("gridPlot2: custom grob 'boxGrob' (uses 'tableBox')"),
          plotOutput("gridPlot2"),
          h1("gridPlot3: direct call to 'tableBox'"),
          plotOutput("gridPlot3")
        )
    
    
    ))
    

    和 server.R
    library(shiny)
    library("grid") 
    
    shinyServer(function(input, output) {
    
      # ########################### #
      # BEGIN Custom grid functions #
      # ########################### #
      tableBox <- function(labels, x=.5, y=.5) {
        nlabel <- length(labels)
        tablevp <-
          viewport(x=x, y=y,
                  width=max(stringWidth(labels)) +
                    unit(4, "mm"),
                  height=unit(nlabel, "lines"))
        pushViewport(tablevp)
        grid.rect()
        if (nlabel > 1) {
          for (i in 1:(nlabel - 1)) {
            # fill <- c("white", "grey")[i %% 2 + 1]
            fill <- c("white")
            grid.clip(y=unit(i, "lines"), just="bottom")
            grid.rect(gp=gpar(fill=fill))
          }
        }
        grid.clip()
        grid.text(labels,
                  x=unit(2, "mm"), y=unit(nlabel:1 - .5, "lines"),
                  just="left")
        popViewport()
      }
      boxGrob <- function(labels, x=.5, y=.5) {
        grob(labels=labels, x=x, y=y, cl="box")
      }
      drawDetails.box <- function(x, ...) {
        tableBox(x$labels, x$x, x$y)
      }
      xDetails.box <- function(x, theta) {
        nlines <- length(x$labels)
        height <- unit(nlines, "lines")
        width <- unit(4, "mm") + max(stringWidth(x$labels))
        grobX(roundrectGrob(x=x$x, y=x$y, width=width, height=height),
              theta)
      }
      yDetails.box <- function(x, theta) {
        nlines <- length(x$labels)
        height <- unit(nlines, "lines")
        width <- unit(4, "mm") + max(stringWidth(x$labels))
        grobY(rectGrob(x=x$x, y=x$y, width=width, height=height),
              theta)
      }
    
      # ########################### #
      # ENDOF Custom grid functions #
      # ########################### #
    
    
    
      output$gridPlot1 <- renderPlot({
    #     plot.new()
        l <- linesGrob()
        # Draw it
        grid.draw(l)
      })
    
      output$gridPlot2 <- renderPlot({
    #     plot.new()
        total_size <- 1000
        boxInitalSize <- boxGrob(c("Hello world"),
                      x = 0.2,y = 0.95)
        grid.draw(boxInitalSize)
      })
    
    
      output$gridPlot3 <- renderPlot({
    #     plot.new()
        tableBox(c("ISBN", "title",
        "author", "pub"),
        x=0.5, y=0.7)
      })      
    })
    

    最佳答案

    您是否尝试过使用 plot()而不是 grid.draw() ?
    在下面找到一个简单的例子:

    sample <- data.table(x = 1:10, y_1 = seq(2,20,2), y_2 = seq(3,30,3))
    simple_plot <- function(){
       a <-   ggplot(data = sample, aes(x, y_1)) + geom_line()
       b <-   ggplot(data = sample, aes(x, y_2)) + geom_line()
       a <- ggplotGrob(a)
       b <- ggplotGrob(b)
    
       both <- arrangeGrob(a, b, nrow=2)
       return(plot(both))
    }
    
    UI <- fluidPage(
    
        # Show a plot of the generated distribution
        mainPanel(
            h1("gridPlot1: linesGrob"),
            plotOutput("gridPlot1"),
        )
    
    )
    
    SERVER <- function(input, output) {
        output$gridPlot1 <- renderPlot({ simple_plot() })
    }
    
    shinyApp(UI, SERVER)
    

    关于R Shiny,renderPlot 中的自定义网格grob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39012441/

    相关文章:

    r - ggplot2条形图删除轴标签和y轴上的0计数之间不必要的距离

    r - R Shiny:单个应用程序的用户身份验证

    r - 我的 r Shiny 应用程序需要 dockerfile 中的 odbc 驱动程序

    r - 加载 RData、Package 时有 Shiny 的进度条?

    shiny - 在 Shiny 中使用 includeHTML 显示 html 文件不适用于 renderUI()

    r - Shiny 的传单添加大量分离的多段线

    r - 使用 plotly 向现有散点图添加点

    r - 在 gtsummary 中添加 OR,使用 beta/log(OR) 和 SE

    R - 对多个群体的每个排列进行统计测试的最简洁方法

    r - 使用 Shiny 时的临时文件