0

I'm trying to create a plot in Shiny, but it is not working. It is blank. I think it is because lavaanPlot uses DiagrammeR svg graphics.

Here's the code

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)

I'm getting this error sometimes:

Error: trying to get slot "ParTable" from an object (class "reactiveExpr") that is not an S4 object 
writer_typer
  • 708
  • 7
  • 25

2 Answers2

0

In the ui.R codes of output plot is

mainPanel(plotOutput("plot")

but, in the server.R output$plot's name is plot2,

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

maybe the reason is on it.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Frsot yan
  • 1
  • 1
  • Thanks for your help. I tried that, but it did not work. – writer_typer Jul 08 '20 at 02:04
  • 1
    You need to call `model()` as a function in your `plot2` reactive as well.. – heds1 Jul 08 '20 at 03:15
  • 1
    `DiagrammeR` objects render correctly in Shiny. I've done it many times. As the other responders have indicated, the problem is in the way you are referencing your reactives. You ned to call them as if they are functions. Because, in fact, they are. – Limey Jul 08 '20 at 09:36
0

I referred to the plot as a function as recommended and then used renderGrViz and grVizOutput and it worked.

I did this:

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

and this

grVizOutput("plot2", width = "50%", height = "100px")
writer_typer
  • 708
  • 7
  • 25