Calling library() in R with a variable as the argument
How about library(...,character.only = TRUE)
?
Passing variables to the library function
Here's a simpler version of your code (incorporating @csgillespie's suggestion):
p <- c("car","ggplot2","pastecs","psych")
for(i in seq_along(p)) {
if(!require(p[i], character.only=TRUE)) {
install.packages(p[i])
library(p[i], character.only=TRUE)
}
}
Note that your code does not work because of non-standard evaluation in library
and require
. The character.only
argument resolves this (per documentation ? library
):
character.only
a logical indicating whether package or help can be assumed to be character strings.
Forcing R to apply a function or call a variable inside an argument of set_args function
You can splice the argument using the bang bang operator !!
:
library(parsnip)
tree_numbers = c(500, 1000)
boost_tree() %>% set_args(tree_depth = !! tree_numbers[1])
#> Boosted Tree Model Specification (unknown)
#>
#> Main Arguments:
#> tree_depth = 500
#>
#> Computational engine: xgboost
Created on 2021-12-17 by the reprex package (v2.0.1)
Load R package from character string
Use the character.only
argument
foo <- "ggplot2"
library(foo,character.only=TRUE)
Using ~ call in R with dynamic variables
You can try this, you need to pass a character to variable. It's much easier that way and if you have 10 variables on X side, you can easily iterate through them:
getFormula <- function(variable){
as.formula(paste(variable,"~ Sepal.Length + Sepal.Width + Species"))
}
petal.length.formula <- getFormula("Petal.Length")
petal.width.formula <- getFormula("Petal.Width")
lm(petal.length.formula,data=iris)
Call:
lm(formula = petal.length.formula, data = iris)
Coefficients:
(Intercept) Sepal.Length Sepal.Width Speciesversicolor
-1.63430 0.64631 -0.04058 2.17023
Speciesvirginica
3.04911
You can also try reformulate, as suggested by @BenBolker and @MrFlick:
getFormula <- function(variable){
reformulate(c("Sepal.Length","Sepal.Width","Species"),
response = variable, intercept = TRUE)
}
lm(getFormula("Petal.Length"),data=iris)
Call:
lm(formula = getFormula("Petal.Length"), data = iris)
Coefficients:
(Intercept) Sepal.Length Sepal.Width Speciesversicolor
-1.63430 0.64631 -0.04058 2.17023
Speciesvirginica
3.04911
Related Topics
Replace Nan Values in a List with Zero (0)
Find Elements Not in Smaller Character Vector List But in Big List
Remove Duplicate Rows from Xts Object
Select Columns by Class (E.G. Numeric) from a Data.Table
Plot Negative Values in Logarithmic Scale with Ggplot 2
Gap in Polar Time Plot - How to Connect Start and End Points in Geom_Line or Remove Blank Space
How to Rename Element's List Indexed by a Loop in R
Several Substitutions in One Line R
Tooltip or Popover in Shiny Datatables for Row Names
How to Load a Matlab Struct into a R Data Frame
Running Out of Heap Space in Sparklyr, But Have Plenty of Memory
Ggplot Boxplot - Length of Whiskers with Logarithmic Axis
Generate Rows Between Two Dates into a Data Frame in R