在闪亮的应用程序中将 grVizOutput 包装在 renderUi/uiOutput 中

Wrapping grVizOutput in renderUi/uiOutput in shiny app

为了能够调整由 DiagrammeR 包生成的图形输出(已在此处解释:https://github.com/rich-iannone/DiagrammeR/issues/93),我需要将 grVizOutput 封装在 [=15] 中=] 在服务器中并通过调用它 uiOutput 在 ui.

这段代码不断返回以下错误:

Error in as.vector: cannot coerce type 'closure' to vector of type 'character'

注意:直接在 UI 中调用 grVizOutput 可以,但不要让我动态调整图表大小。

知道为什么这没有按预期工作吗?

library(shiny)
library(DiagrammeR)
library(magrittr)

##------------------------------------------
## ui function

ui <- shinyUI(fluidPage( 
  fluidRow(
    column(
      width = 8,
      uiOutput('d')
    )
  )
)
)


##------------------------------------------
## server function

server <- function(input, output){

  output$d <- renderUI({
    grVizOutput(
      renderGrViz({grViz("digraph boxes_and_circles {

              # a 'graph' statement
              graph [overlap = true, fontsize = 10]

              # several 'node' statements
              node [shape = box,
              fontname = Helvetica]
              A; B; C; D; E; F

              node [shape = circle,
              fixedsize = true,
              width = 0.9] // sets as circles
              1; 2; 3; 4; 5; 6; 7; 8

              # several 'edge' statements
              A->1 B->2 B->3 B->4 C->A
              1->D E->A 2->4 1->5 1->F
              E->6 4->6 5->7 6->7 3->8
      }")})
    )
  })
}


##------------------------------------------
## run app

shinyApp(ui = ui, server = server)

所以您需要另一个 output - render 活动。 renderGrViz 不能在 grVizOutput.

library(shiny)
library(DiagrammeR)
library(magrittr)

##------------------------------------------
## ui function

ui <- shinyUI(fluidPage( 
    fluidRow(
        column(
            width = 8,
            uiOutput('d')
        )
    ),
    sliderInput(inputId = "height", label = "Height", min = 0, max = 2000, value = 300, step = 200),
    sliderInput(inputId = "width", label = "Width", min = 0, max = 2000, value = 300, step = 200)
)
)


##------------------------------------------
## server function

server <- function(input, output){
    
    output$d <- renderUI({
        grVizOutput(
            "gvout", height = input$height, width = input$width
        )
    })
    
    output$gvout <- renderGrViz({grViz("digraph boxes_and_circles {

              # a 'graph' statement
              graph [overlap = true, fontsize = 10]

              # several 'node' statements
              node [shape = box,
              fontname = Helvetica]
              A; B; C; D; E; F

              node [shape = circle,
              fixedsize = true,
              width = 0.9] // sets as circles
              1; 2; 3; 4; 5; 6; 7; 8

              # several 'edge' statements
              A->1 B->2 B->3 B->4 C->A
              1->D E->A 2->4 1->5 1->F
              E->6 4->6 5->7 6->7 3->8
      }")})
}


##------------------------------------------
## run app

shinyApp(ui = ui, server = server)

您可以使用 shinyjqui 调整大小:

library(shiny)
library(shinyjqui)
library(DiagrammeR)

ui <- shinyUI(fluidPage( 
  fluidRow(
    column(
      width = 8,
      jqui_resizable(grVizOutput("grviz"))
    )
  )
)
)    

##------------------------------------------
## server function

server <- function(input, output){
  
  output[["grviz"]] <- renderGrViz({
    grViz("digraph boxes_and_circles {

              # a 'graph' statement
              graph [overlap = true, fontsize = 10]

              # several 'node' statements
              node [shape = box,
              fontname = Helvetica]
              A; B; C; D; E; F

              node [shape = circle,
              fixedsize = true,
              width = 0.9] // sets as circles
              1; 2; 3; 4; 5; 6; 7; 8

              # several 'edge' statements
              A->1 B->2 B->3 B->4 C->A
              1->D E->A 2->4 1->5 1->F
              E->6 4->6 5->7 6->7 3->8
      }")
  })
}
    
##------------------------------------------
## run app

shinyApp(ui = ui, server = server)