【问题标题】:How to render plot from other packages such as DiagrammeR in R Shiny?如何从其他包(例如 R Shiny 中的 DiagrammeR)渲染绘图?
【发布时间】:2020-10-28 07:27:20
【问题描述】:

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

【问题讨论】:

  • 你调用响应式对象就好像它们是函数一样。所以在你的output$plot 调用中,使用plot2()
  • TyperWriter,在shiny.rstudio.com/tutorial/written-tutorial/lesson6中查找对dataInput()的引用
  • 我试过了,但在 output$plot 内使用 plot2() 不起作用

标签: r ggplot2 plot shiny ggplotly


【解决方案1】:

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

mainPanel(plotOutput("plot")

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

output$plot &lt;- renderPlot({plot2......

也许原因就在上面。

【讨论】:

  • 感谢您的帮助。我试过了,但没有用。
  • 您还需要在您的plot2 反应式中调用model() 作为函数..
  • DiagrammeR 对象在 Shiny 中正确渲染。我已经做过很多次了。正如其他响应者所指出的那样,问题在于您引用反应器的方式。你需要把它们当作函数来调用。因为,事实上,他们是。
【解决方案2】:

我将情节称为推荐的函数,然后使用 renderGrVizgrVizOutput 并且它起作用了。

我这样做了:

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

还有这个

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

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 2020-06-19
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2020-11-08
    • 2021-03-20
    相关资源
    最近更新 更多