Calling functions from a external environment and printing results from them

Hello, i'm trying to load a environment with some functions that i want to apply in some variables that will be inputed by the user.
I tried three diffrent ways using load() and also try to use RDS but without success.
The initial idea was to apply the functions just for those with "1" for the value of the checkbox for respective company, but as i was already having problems just to print a result from a function, i trying to solve first how to include the result from the function in the output table. The error is "Warning: Error in data.frame: arguments imply differing number of rows: 1, 0
[No stack trace available]"
But i can't figure it out what is that mean

Follow the code:

#> Code comment
R_code <- 
library(shiny)
library(DT)
#load("C:/Users/Filipe Chagas/Desktop/OneDrive/Motor.test/company_models.RData")  
#app.R

ui <- fluidPage(
  sidebarPanel(
    h1("Multi-simulator test"),
    numericInput("age","Age",18,min=18,max=90),
    verbatimTextOutput("textAge"),
    numericInput("dlage","Drive-license Age",1,min=1,max=80),
    numericInput("pp","Power rate (Horsepower/Gross-Weight)",0.1,min=0.1,max=10),
    numericInput("cc","Cilinders",1,min=1,max=10000),
    numericInput("kids","Have kids? (yes=1,no=0)",0,min=0,max=1),
    numericInput("car_year","Year of the car",1950,min=1950,max=3000),
    numericInput("lisbon","Live in Lisbon? (yes=1,no=0)",0,min=0,max=1),
    tags$hr(),
    checkboxInput("CoA", "Company A", TRUE),
    checkboxInput("CoB", "Company B", TRUE),
    checkboxInput("CoC", "Company C", TRUE)),
  mainPanel(
    
    tableOutput("table")
    
  )
)



server <- function(input, output) {
load("C:/Users/Filipe Chagas/Desktop/OneDrive/Motor.test/company_models.RData")  
# Reactive value for selected dataset ----
# 
# reactive({if(input$CoA==1){
#   isolate(PremiumCoA <- CompanyA(input$age,input$dlage,input$pp,input$ywc))
#   }})
# 
  
  
  
  data <- reactive({
    #ifelse(input$CoA==1,premiumCoA<-CompanyA(input$age,input$dlage,input$pp,input$ywc),premiumCoA <- NA)
    
    
    data.frame(Age=input$age,Car_Age=(2019 - input$car_year),
                               Dl_age=input$dlage,Power_rate=input$pp,
                               Cilinders=input$cc,Kids=input$kids,
                               Lisbon=input$lisbon,PremiumCoA=CompanyA(input$age,input$dlage,input$pp,input$ywc)
                               )})
  # Table of selected dataset ----
  output$table <- renderTable({
    data()
  })

}

shinyApp(ui = ui, server = server)



Company Models code:

#> Code comment
R_code <- 

CompanyA <- function(age,dlage,pp,ywc){exp(0.2*as.numeric(age) + 0.35*as.numeric(dlage) + 0.03*as.numeric(pp) - 0.17*as.numeric(ywc) )}

CompanyB <- function(age,dlage,cc,kids){exp(0.15*as.numeric(age) + 0.28*as.numeric(dlage) + 0.48*as.numeric(cc) + 0.2*as.numeric(kids))}

CompanyC <- function(age,car_age,ywc,lisbon){exp(0.1*as.numeric(age) + 0.14*as.numeric(car_age) - 0.08*as.numeric(ywc) + 0.1*as.numeric(lisbon))}


If you want to store a function, store the code that generates it in a script and source() that script. For example, I made a script called FunctionStore.R containing the code

CompanyA <- function(x, y) { 
  x^2 + sqrt(y)
  }

I can then (after clearing my environment) do

>source("FunctionStore.R")
>CompanyA(2, 9)
[1] 7

Whether that will work without modification in shiny, I don't know.

1 Like

I tried to use as a source, it create the functions, but the same error appears when i try to show a result from a call of one of them, in the data frame.

  data <- reactive({
    #ifelse(input$CoA==1,premiumCoA<-CompanyA(input$age,input$dlage,input$pp,input$ywc),premiumCoA <- NA)
    
    
    data.frame(Age=input$age,Car_Age=(2019 - input$car_year),
                               Dl_age=input$dlage,Power_rate=input$pp,
                               Cilinders=input$cc,Kids=input$kids,
                               Lisbon=input$lisbon,PremiumCoA=CompanyA(input$age,input$dlage,input$pp,input$ywc)
                               )})
  # Table of selected dataset ----
  output$table <- renderTable({
    data()
  })


The problem is not with your functions (assuming that you are sourcing them correctly), that error is because you dont have an input called input$ywc and you are passing that as an argument for CompanyA() function.

1 Like

I can not believe I let this go, I'm ashamed. Thank you very much, that's right, the functions are working normally!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.