Unused Arguments in R Error

Unused arguments in R

Change the definition of multiply to take additional unknown arguments:

multiply <- function(a, b, ...) {
# Original code
}

R: unused argument in levels

From the help page?as.factor it shows that the function only takes one argument (in your case the filtered_table$column), and therefore the error message indicates that there's not another argument to match up with the second one you've specified in the function call. To specify the levels explicitly, you may need to use the factor() function.

ERROR: unused argument (output.results = TRUE)

The argument is print.results based on the args of the function

> args(betaconv.ols)
function (gdp1, time1, gdp2, time2, conditions = NULL, beta.plot = FALSE,
beta.plotPSize = 1, beta.plotPCol = "black", beta.plotLine = FALSE,
beta.plotLineCol = "red", beta.plotX = "Ln (initial)", beta.plotY = "Ln (growth)",
beta.plotTitle = "Beta convergence", beta.bgCol = "gray95",
beta.bgrid = TRUE, beta.bgridCol = "white", beta.bgridSize = 2,
beta.bgridType = "solid", print.results = FALSE)
NULL
betaconv.ols(GDP_NUTS2$t2000, 2000, GDP_NUTS2$t2019, 2019, print.results = TRUE)

-output

Absolute Beta Convergence 
Model coefficients (Estimation method: OLS)
Estimate Std. Error t value Pr (>|t|)
Alpha 1.537689e-01 0.048509886 3.169847 0.05048663
Beta -1.341938e-02 0.005137275 -2.612158 0.07953682
Lambda 7.110647e-04 NA NA NA
Halflife 9.748018e+02 NA NA NA
Model summary
Estimate F value df 1 df 2 Pr (>F)
R-Squared 0.6946059 6.823372 1 3 0.07953682

R : Function is returning Unused Arguments

As pointed out in the comments, if you name arguments, you need to name them according to the function definition. Also, the lower and upper bounds are not expressions, but must be constants (i.e. numbers, not names such as random_1).

Such constraints could be handled for instance through penalties: in the objective function, compute whether a variable is outside its range; if it is, subtract a penalty from the objective function (if you maximise) or add a penalty (if you minimise).

There are optimisation methods in which you could handle such constraints directly when creating new solutions. One such method is Local Search. Here is a (rough) example, in which I assume that you want to maximise. I use the implementation in package NMOF (which I maintain). The algorithm expects minimisation, but that is no problem: just multiply the objective function value by -1. (Note that many optimisation algorithms expect minimisation models.)

The key ingredient to Local Search is the neighbourhood function. It takes a given solution as input and produces a slightly changed solution (a neighbour solution). Local Search then takes a random walk through the space of possible solutions (with steps as defined in the neighbourhood function), accepting better solutions, but rejecting solutions that lead to worse objective function values.

library("NMOF")
nb <- function(x, ...) {
## randomly pick one element in x and a small change
i <- sample.int(length(x), 1)
stepsize <- c(1, 1, 1, 1, 0.5, 0.5, 0.5)
x[i] <- x[i] + runif(n = 1,
min = -stepsize[i],
max = stepsize[i])

## 'repair' the solution
x <- pmin(x, c(120, 120, 120, 120, 1, 1, 1))
x <- pmax(x, c( 80, x[1], 85, x[2], 0, 0, 0))

x
}

An initial solution:

x0 <- c(100, 100, 100, 100, 0.5, 0.5, 0.5)
## [1] 100.0 100.0 100.0 100.0 0.5 0.5 0.5

A random step:

nb(x0)
## [1] 100.000 100.000 100.000 100.000 0.759 0.500 0.500

Running the algorithm:

sol <- LSopt(function(x) -do.call(fitness, as.list(x)),
list(neighbour = nb,
x0 = x0,
nI = 500))
-sol$OFvalue
## 0.998
sol$xbest
## [1] 112.329 112.331 111.777 112.331 1.000 0.771 1.000

If such a method would be acceptable you, perhaps this tutorial on heuristics is useful.


Here would be a solution with PSO, with NMOF::PSopt.

sol <- PSopt(function(x) -do.call(fitness, as.list(x)),
list(nG = 25, ## number of generations
nP = 20, ## population size
repair = function(x) {
x <- pmin(x, c(120, 120, 120, 120, 1, 1, 1))
x <- pmax(x, c( 80, x[1], 85, x[2], 0, 0, 0))
x
},
min = c( 80, 80, 85, 85, 0, 0, 0),
max = c(120, 120, 120, 120, 1, 1, 1)))

-sol$OFvalue
## 0.998
sol$xbest
## [1] 98.551 98.551 110.750 108.639 1.000 0.312 1.000

Error: unused arguments (alist(, drop = FALSE)), R Shiny application. Have I indexed the function incorrectly?

Ok, rewrote the code and it works more or less:

    packages <- c("shiny", "ggplot2")
if (length(setdiff(packages, rownames(installed.packages()))) > 0)
{
install.packages(setdiff(packages, rownames(installed.packages())))
}


require(shiny)
require(ggplot2)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Rating Curve Generator"),

# Sidebar
sidebarLayout(
sidebarPanel(
fileInput("file",
"Browse to file",
multiple=TRUE,
accept=c("text/csv","text/comma-serparated-values,text/plain",".csv", ".txt"),
placeholder = "Browse to .csv file"
),#end of fileinput

#downloadButton("downloadData", "Save To File")


),#end sidebarPanel

# show uploaded data

mainPanel(
tabsetPanel(id = "inTabset",
tabPanel(title = "Input File Contents", tableOutput("contents")),
tabPanel(title = "Time Series",
plotOutput("plot1",
click = "plot1_click",
brush = brushOpts(
id = "plot1_brush")
),#end plotouput
actionButton("exclude_toggle", "Toggle points"),
actionButton("exclude_reset", "Reset")

)#end of tabPanel

)#end of tabsetPanel

)#end of main panel

)#of sidebarLayout

)#end of ui
########################################################
#Define server logic required to plot data with a brush#
########################################################
server <- function(input, output) {

##################################################
#Step 1: Get Data, show it to the user to verify#
##################################################



#rbind all data if multiple files are selected.
datamerge <- reactive({

if(is.null(input$file))
return()
else
{
nfiles = nrow(input$file)
csv = list()
for (i in 1 : nfiles)
{

csv[[i]] = read.csv(input$file$datapath[i])
}
lAg<-do.call(rbind, csv) # rbind the datasets
#Values<-rep(TRUE,nrow(lAg))
#cbind(lAg,Values)

}

})#close datamerge reactive function

#mergeData<-datamerge()
#vals<-reactiveValues()

vals<-reactiveValues(
keeprows = rep(TRUE, NROW(isolate(datamerge()))))





#output the merged data to contents tab and show to user
output$contents<-renderTable({
datamerge()

})#end output$contents (first tab)



################################
#Step 2: Plot vals #
################################
output$plot1 <- renderPlot({

# Plot the kept and excluded points as two separate data sets
#vals$keeprows=rep(TRUE,nrow(datamerge()))
# print(vals$keeprows)

keep <- datamerge()[ vals$keeprows, , drop = FALSE]
exclude <- datamerge()[!vals$keeprows, , drop = FALSE]


ggplot(keep, aes(Gage_ft, Flow_cfs)) + geom_point() +
geom_smooth(method = lm, fullrange = TRUE, color = "black")+
geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25)
#end ggplot

})#end renderPlot

# Toggle points that are clicked
observeEvent(input$plot1_click, {
res <- nearPoints(datamerge(), input$plot1_click, allRows = TRUE)

vals$keeprows <- xor(isolate(vals$keeprows), res$selected_)
})

#toggle point that are brusted when button is clicked
observeEvent(input$exclude_toggle, {
res <- brushedPoints(datamerge(), input$plot1_brush, allRows = TRUE)

vals$keeprows <- xor(vals$keeprows, res$selected_)
})

observeEvent(input$exclude_reset, {
vals$keeprows <- rep(TRUE, nrow(datamerge()))
})

}# end of server


# Run the application
shinyApp(ui = ui, server = server)


Related Topics



Leave a reply



Submit