a minimal not working example:
variable1 <- quo("dataframe_name")
enquo(variable1) <- df %>% filter(...)
any help is appreciated.
a minimal not working example:
variable1 <- quo("dataframe_name")
enquo(variable1) <- df %>% filter(...)
any help is appreciated.
Why would you want to assign value to a quoted variable? What is the end goal of your approach?
In the first line of your code you quote input twice - once with quotes ("
) and once with rlang::quo
. You only need one. Your second line is very confusing to me, so it would help if you can explain it further.
This is an example of how quoting is supposed to work. Is it close to what you are trying to do?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
name <- rlang::quo(cyl)
mtcars %>%
dplyr::select(!!name)
#> cyl
#> Mazda RX4 6
#> Mazda RX4 Wag 6
#> Datsun 710 4
#> Hornet 4 Drive 6
#> Hornet Sportabout 8
#> Valiant 6
#> Duster 360 8
#> Merc 240D 4
#> Merc 230 4
#> Merc 280 6
#> Merc 280C 6
#> Merc 450SE 8
#> Merc 450SL 8
#> Merc 450SLC 8
#> Cadillac Fleetwood 8
#> Lincoln Continental 8
#> Chrysler Imperial 8
#> Fiat 128 4
#> Honda Civic 4
#> Toyota Corolla 4
#> Toyota Corona 4
#> Dodge Challenger 8
#> AMC Javelin 8
#> Camaro Z28 8
#> Pontiac Firebird 8
#> Fiat X1-9 4
#> Porsche 914-2 4
#> Lotus Europa 4
#> Ford Pantera L 8
#> Ferrari Dino 6
#> Maserati Bora 8
#> Volvo 142E 4
Created on 2018-04-11 by the reprex package (v0.2.0).
what I am looking for is to define the name of a dataframe though a variable
I was actually looking for a solution for the following and thought i can solve that with quote and unquote...
variable_for_dataframe <- "name_of_dataframe"
variable_for_dataframe <- df %>% ...
as a result I would get the new dataframe name_of_dataframe
your solution works for the right side (eg. for x in y <- x). I am looking to replace the left side...
I have to say, I've never had a need for something like this, so thank you for making me look for something I never knew existed . There is a function in base R called
assign
that does exactly what you want:
name <- 'abc'
assign(name, data.frame(x = 3))
abc
#> x
#> 1 3
Created on 2018-04-11 by the reprex package (v0.2.0).
Nevertheless, can you please elaborate when something like this is useful? I'm still trying to find a use-case.
Thank You, but it's not what I was looking for.
Let me elaborate with a use-case.
my current use-case look like this:
I have a list of dates (31.01.2018 / 28.02.2018 / 31.03.2018 / ...)
I have a dataframe df, where some works are done, for example it is filtered on with the dates
this runs every month and future_me doesn't want to mess around within the script in the OPERATIONAL part
so what my idea was, is to define the date as a variable, so that I don't have to mess around with the script in the OPERATIONAL part here too
thought something like the following would't work. unfortunately i works for the right side only..
INPUT:
var_date <- quo("31.03.2018")
var_dataframe <- quo("dataframe_03")
OPERATION:
!!var_dataframe <- df %<% filter(table_date == !!var_date)...
OUTPUT:
dataframe_03
If i want a dataframe_02, I just have to change INPUT
I am not sure, if it's clear now what I am looking for...?
But why do you care whether the name of the resulting dataframe is dataframe_03
or dataframe_02
? Do you mean that you want to save your dataframe somewhere as output?
Also, take a look at my first example with mtcars
and cyl
variable. You are quoting two times in this example as well. It won't work as expected. Moreover, I still don't see why assign
wouldn't work for you.
if I want to work with database_02, database_03, ... comparing how some variable have developed over time, I have to create multiple output dataframes
assign doesn't work, because I can't wrap the whole df %>% filter... part inside assign
name <- "abc"
name %<% assign(df %>% filter....)
didn't work
Why is your first arrow in a different direction? You can also do something like this:
name <- "abc"
temp <- df %>% operation(...)
assign(name, temp)
Cool! Your solution worked! Thank you very much for your patience.
the arrow was a typing mistake...