JVicM
JVicM

Reputation: 67

Use content from a textAreaInput as argument to a function

I would like to know if it is possible to use the data described in a textAreaInput in a function.

I need the model described in the text field to be filled in the field specified in the function (my_model), replacing "dmodel()".

This is my code:

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(tidyverse)
library(readxl)
library(stringr)
library(psych)
library(MPsychoR)
library(ggplot2)
library(semPlot)
library(lavaan)

# Dashboard

ui <- fluidPage(
    titlePanel("CFA"),
    
    sidebarLayout(
        sidebarPanel(
            h4("Model")
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
            box(
                textAreaInput(
                    width = 2000,
                    "dmodel", "Insert the model"),
                
                br(),
                
                box(
                    width = 2000,
                    plotOutput("cfagra")
                ),
                
                box(
                    width = 2000,
                    verbatimTextOutput("cfares")
                )
                
            ),
        )
    )
)

# Server
server <- function(input, output, session) {
    
    # Copy the model for functions
    
    dmodel <- reactive({
        as.data.frame(input$dmodel)
    })

    # CFA Synthesis
    
    output$cfares <-
        renderPrint({
            
        # Calculation of CFA
            
            my_model <- 'dmodel()'
            fitModel <- lavaan::cfa(my_model, data = csv(),
                                    ordered = names(csv()))
            
            summary(fitModel, standardized=TRUE,fit.measures=TRUE)
            modindices(fitModel)
            
        })
    
# Plot
    
    output$cfagrafico <- renderPlot({
        
# Calculation of CFA
        
        my_model <- 'dmodel()'
        fitModel <- lavaan::cfa(my_model, data = csv(),
                                ordered = names(csv()))
        
        semPaths(fitModel, what = "est", edge.label.cex = 0.7,
                 edge.color = 1, esize = 1, sizeMan = 4.5, asize = 2.5,
                 intercepts = FALSE, rotation = 4, thresholdColor = "red",
                 mar = c(1, 5, 1.5, 5), fade = FALSE, nCharNodes = 4)
        
        summary(fitModel, standardized=TRUE,fit.measures=TRUE)
        modindices(fitModel)
    })
    
}

# App
shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 58

Answers (1)

jpdugo17
jpdugo17

Reputation: 7106

Based on lavaan documentation, my_model should be a string.

First,

dmodel <- reactive({
  input$dmodel
})

and then call the reactive value without quotes.

output$cfares <-
  renderPrint({
    
    # Calculation of CFA
    
    my_model <- dmodel()
    fitModel <- lavaan::cfa(my_model, data = csv(),
                            ordered = names(csv()))
    
    summary(fitModel, standardized=TRUE,fit.measures=TRUE)
    modindices(fitModel)
    
  })

Upvotes: 1

Related Questions