gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:36:57 24 4
gpt4 key购买 nike

我正在尝试在 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com