如何从其他包(例如 R Shiny 中的 DiagrammeR)渲染绘图?

How to render plot from other packages such as DiagrammeR in R Shiny?

我正在尝试在 Shiny 中创建情节,但它不起作用。它是空白的。 我认为这是因为 lavaanPlot 使用 DiagrammeR svg 图形。

这是代码

library(shiny)
library(lavaan)
library(lavaanPlot)

ui <- fluidPage(
    titlePanel("Text Input"),
    sidebarLayout(
        sidebarPanel(
            textAreaInput(inputId = "text1", 
                      label = "Syntax"), 
                      value = "Enter formula"),
        mainPanel(
           plotOutput("plot"),
           verbatimTextOutput("text1"),
           verbatimTextOutput("regout")
)))

server <- function(input, output, session) {
  formula <- reactive({
    tryCatch({
      input$text1
    }, error = function(e) NULL)
  })
  model <- reactive({
    if(!is.null(formula()))
    tryCatch({
        cfa(formula(), data = HolzingerSwineford1939)
      }, error = function(e) NULL)
    })
  results <- reactive({
    if(!is.null(model())){
      summary(model(), fit.measures = TRUE)
    }
  }) 
  output$regout <- renderPrint({
      results()
    })
  plot2 <- reactive({
      lavaanPlot(model = model)
    })
  output$plot <- renderPlot({
      plot2
    })
}
shinyApp(ui = ui, server = server)

我有时会收到此错误:

Error: trying to get slot "ParTable" from an object (class "reactiveExpr") that is not an S4 object 

在输出图的ui.R代码中是

mainPanel(plotOutput("plot")

但是,在 server.R 输出中 $plot 的名称是 plot2,

output$plot <- renderPlot({plot2......

也许原因就在上面

我按照推荐将绘图作为函数引用,然后使用 renderGrVizgrVizOutput 并且它起作用了。

我这样做了:

output$plot2 <- renderGrViz({
      lavaanPlot(model = model())
    })

还有这个

grVizOutput("plot2", width = "50%", height = "100px")