我在搜索指定传递给 dplyr 中的 .fns 参数的自定义函数中的参数时遇到了一些麻烦。考虑这段代码:
data(iris)
ref_col <- "Sepal.Length"
iris_summary <- iris %>%
group_by(Species) %>%
summarise(
Sepal.Length_max = max(Sepal.Length),
across(
Sepal.Width:Petal.Width,
~ .x[which.max(get(ref_col))]
)
)
Run Code Online (Sandbox Code Playgroud)
这工作正常。然后我需要用自定义函数替换 lambda 函数,然后在内部传递请求的参数(在我的代码中自定义函数更复杂,并且嵌入到 dplyr 管道中并不方便)。请看下面的代码:
ref_col <- "Sepal.Length"
get_which_max <- function(x, col_max) x[which.max(get(col_max))]
iris_summary <- iris %>%
group_by(Species) %>%
summarise(
Sepal.Length_max = max(Sepal.Length),
across(
Sepal.Width:Petal.Width,
~ get_which_max(.x, ref_col)
)
)
Run Code Online (Sandbox Code Playgroud)
R 现在给出错误“未找到对象‘Sepal.Length’”,因为它正在为管道进程内的对象而不是 colname 提供服务。任何人都可以帮我解决这个问题吗?
我有两个脚本。test4.sh 调用 1.sh
\n我不\xe2\x80\x99不明白为什么 1.sh 的事情我按下 \xe2\x80\x9cd\xe2\x80\x9d 键。你能解释一下吗
这是1.sh
\nset -x\necho "Please enter some input: "\necho "enter $(echo $RANDOM)"\nsleep 2\nread input_variable\necho "You entered: $input_variable"\nRun Code Online (Sandbox Code Playgroud)\n这是test4.sh
\nset -x\nset +e\nmkfifo p_in\nbash 1.sh p_in<0 | sed -e 's/^/1\\.sh:: /' &\ncat - 1> p_in\nRun Code Online (Sandbox Code Playgroud)\n输出:
\npi@raspberrypi:~/tmp/lolttt $ bash test4.sh\n+ set +e\n+ mkfifo p_in\nmkfifo: cannot create fifo 'p_in': File exists\n+ bash 1.sh p_in\n+ cat -\n+ sed -e 's/^/1\\.sh:: /'\n+ echo 'Please enter some input: '\n1.sh:: Please enter some input:\n++ …Run Code Online (Sandbox Code Playgroud) 根据提供的答案修改工作:
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Get-Content ./case.csv | ForEach-Object ToUpper
FJKDLA,W
FKDSLAJF,FDJK;A
NLK;NBF;SDJF,DGDF
VNL;KKDF,BGNGFN
NVCL;V,RGS
NVKL;,THRN
VLKDF,TMMJYMF
FJDK,FDJK;A
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Get-Content ./case.csv | ForEach-Object ToLower
fjkdla,w
fkdslajf,fdjk;a
nlk;nbf;sdjf,dgdf
vnl;kkdf,bgngfn
nvcl;v,rgs
nvkl;,thrn
vlkdf,tmmjymf
fjdk,fdjk;a
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $TextInfo = (New-Object System.Globalization.CultureInfo("en-US")).TextInfo;
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Get-Content ./case.csv | ForEach-Object ToTitleCase
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> pwsh --version
PowerShell 7.3.4
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 23.04
Release: 23.04
Codename: …Run Code Online (Sandbox Code Playgroud) 我可以用管道将以下两个代码连接成一个吗?
v <- c(1:3)
names(v) <- letters[1:3]
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的:
v <- c(1:3) %>% rename_with(~ letters[1:3])
Run Code Online (Sandbox Code Playgroud)
当然这是行不通的;它说:
“UseMethod(“rename_with”) 中的错误:没有适用于“rename_with”的方法应用于“c('integer', 'numeric') 类的对象”
我想这样做的原因是我需要v使用包构造一个像上面这样的对象targets。
我已经适应在 R 和 RStudio 中使用新的本机管道。然而,当我直接通过管道进入View它时,会创建一个独特的新选项卡,该选项卡一直弄乱了我的窗口。是否有办法限制新的管道运算符仅更新单个选项卡,就像以前的管道运算符 ( %>%) 所做的那样?
我正在做一个操作系统课程,我们应该学习如何使用管道在进程之间传输数据.
我们得到了这段简单的代码,演示了如何使用管道,但我很难理解它.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main()
{
int pipefd [2], n;
char buff[100] ;
if( pipe( pipefd) < 0)
{
printf("can not create pipe \n");
}
printf("read fd = %d, write fd = %d \n", pipefd[0], pipefd[1]);
if ( write (pipefd[1],"hello world\n", 12)!= 12)
{
printf("pipe write error \n");
}
if( ( n = read ( pipefd[0] , buff, sizeof ( buff) ) ) <= 0 )
{
printf("pipe read error \n");
}
write ( 1, buff, …Run Code Online (Sandbox Code Playgroud) 我有一个小C服务器需要接受连接并分叉子进程.我需要的stderr子进程的去一个已经存在的命名管道,将stdout孩子转到stdout父,和stdin孩子的TP来自同一个地方作为stdin母公司.
我最初的尝试涉及popen()但我似乎永远不会得到我想要的.
最后,这个特定的解决方案只需要在Solaris中工作.谢谢.
编辑:更新了问题,希望能更准确地描绘我想要完成的事情.谢谢你对我耐心.
EDIT2:我还需要父进程获取子进程的返回值,然后对它做一些事情,如果这有任何区别.
void doWork(){
int fd[2];
int pret = pipe(fd);
close(0);
close(1);
int dret = dup2(fd[1], 1);
close(fd[1]);
while(1){
char buf[256];
system("whoami");
int rret = read(fd[0], buf, 256);
if(/* something interesting */){
return;
}
}
}
int main(int argc, char* argv[]){
int children = 2;
for(unsigned work = 0; work < children; ++work){
pid_t pid = fork();
if(pid == 0){
doWork();
break;
}
}
int status;
wait(&status);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个例子有什么问题?我正在尝试让每个子进程调用外部程序,然后从管道中读取该程序的输出.我的代码仅在子项设置为1时有效.
编辑:我正在尝试使用fork/pipes实现任务并行.父进程和子进程之间没有通信.每个子进程执行外部程序,读取输出,处理输出,并继续直到找到所需的输出.
我正在尝试将管道传递给另一个使用execv创建的进程.到目前为止,这是我的代码,但它不起作用.我到处寻找信息,但我找不到任何具体的关于通过管道vie exec.任何帮助表示赞赏.谢谢
int fd[2];
pipe(fd);
char passwrite[1];
sprintf(passwrite, "%d", fd[0]);
char arg[1];
arg[1]=passwrite;
int x;
x=fork();
if (x==0) {
execv("NewProg",arg);
}
Run Code Online (Sandbox Code Playgroud) 我有管道和重复使用的这个例子.它应该创建一个由管道连接的两个进程的环.这是代码:
#include <unistd.h>
#define READ 0
#define WRITE 1
main (int argc, char *argv[]) {
int fd[2];
pipe(fd); //first call to pipe
dup2(fd[READ],0);
dup2(fd[WRITE],1);
close(fd[READ]);
close(fd[WRITE]);
pipe(fd); //second call to pipe
if (fork()==0) {
dup2(fd[WRITE],1);
} else
dup2(fd[READ],0);
close(fd[READ]);
close(fd[WRITE]);
}
Run Code Online (Sandbox Code Playgroud)
从"两个过程的循环"我理解过程A的输出连接到过程B的输入,过程B的输出连接到过程A的输入.
在第一次调用pipe之后,以及dup2的两次成功调用之后,我认为标准输入和输出被重定向到我的新管道的输入和输出.
然后,第二次调用管道让我感到困惑,因为我认为它会覆盖我以前的fd值.在这一点上标准输入和输出发生了什么?
最后,fork调用:
孩子是否将标准输出重定向到管道?
父级是否将标准输入重定向到管道?
我在这里看不到戒指.
我希望自己清楚明白,因为我真的很困惑.
非常感谢你!!