library(tidyverse)
xy <- data.frame(xvar = 1:10, yvar = 1:10)
plotfunc <- function(data, x, y){
print(
ggplot(data, aes_(x = substitute(x), y = substitute(y))) +
geom_line()
)
}
plotfunc(xy, xvar, yvar)
上面的功能工作正常。以下功能没有。如何进行如下所示的正确分配?我知道他们实际上并不会做任何事情,但是我无论如何都不会开始正确使用我的语法。
plotfunc <- function(data, x, y){
rng <- range(substitute(x), na.rm = TRUE)
rescale <- (substitute(x) - rng[1]) / (rng[2] - rng[1])
print(
ggplot(data, aes_(x = substitute(x), y = substitute(y))) +
geom_line()
)
}
plotfunc(xy, xvar, yvar)
答案 0 :(得分:4)
您需要在xvar
的上下文中评估data.frame
:
plotfunc <- function(data, x, y){
rng <- range(eval(substitute(x),envir = data), na.rm = TRUE)
rescale <- (eval(substitute(x),envir = data) - rng[1]) / (rng[2] - rng[1])
print(
ggplot(data, aes_(x = substitute(x), y = substitute(y))) +
geom_line()
)
}
plotfunc(xy, xvar, yvar)
答案 1 :(得分:1)
这里是使用更多tidyverse编程风格的另一种选择:
<?xml version="1.0" encoding="UTF-8"?>
<t:BaseButtonTemplate xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:t="clr-namespace:Japanese.Templates"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.Btn" x:Name="this">
<StackLayout Padding="10"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand" >
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand, Source={x:Reference this}}"
CommandParameter="{Binding TapCommandParam, Source={x:Reference this}}"
NumberOfTapsRequired="1" />
</StackLayout.GestureRecognizers>
<Frame CornerRadius="25"
BorderColor="{Binding FrameBorderColor, Source={x:Reference this}}"
BackgroundColor="{Binding FrameBackgroundColor, Source={x:Reference this}}"
HorizontalOptions="Center" VerticalOptions="Center" HasShadow="false" Padding="0"
WidthRequest="50"
HeightRequest="50">
<Label TextColor="{Binding LabelTextColor, Source={x:Reference this}}"
Text="{Binding Text, Source={x:Reference this}}"
HorizontalOptions="Center" VerticalOptions="Center"
HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
FontFamily="FontAwesome5ProRegular" />
</Frame>
</StackLayout>
</t:BaseButtonTemplate>