library(dplyr)
func <- function() {
return(1)
return(2)
}
func_pipe <- function() {
1 %>% return()
return(2)
}
func()
# [1] 1
func_pipe()
# [1] 2
Run Code Online (Sandbox Code Playgroud)
为什么该func_pipe函数不在第一个处停止return?
如何在 RMarkdown 或 Quarto 的输出文件中并排放置 2 块代码?
代码
library(dplyr)
mtcars %>% select(gear)
Run Code Online (Sandbox Code Playgroud)
library(dplyr)
select(mtcars, gear)
Run Code Online (Sandbox Code Playgroud)
u <- rnorm(10000)
v <- rnorm(10000)
# `outer`
system.time(mat1 <- outer(u, v , `<`))
# user system elapsed
# 1.80 1.34 3.32
# `for` loop
system.time({
mat2 <- matrix(NA, nrow = length(u), ncol = length(v))
for(i in seq_along(v)) {
mat2[, i] <- u < v[i]
}
})
# user system elapsed
# 0.97 0.02 1.01
identical(mat1, mat2)
# [1] TRUE
Run Code Online (Sandbox Code Playgroud) 当我运行该应用程序时,我希望tab 2单击 ,这样我就可以先看到它,而不是tab 1。我怎样才能这样做呢?
# USER INTERFACE
ui <- fluidPage(
mainPanel(
tabsetPanel(
tabPanel("tab 1"),
tabPanel("tab 2"),
)
)
)
# SERVER
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud) 我知道这[]本身就是一个函数,但是是否有一个函数可以执行以下操作?
vect = c(1, 5, 4)
# Slicing by row index with []
vect[2]
# [1] 5
# Does this kind of function exist ?
slicing_func(vect, 2)
# [1] 5
# And for dataframes ?
Run Code Online (Sandbox Code Playgroud) c()与管道运算符一起使用的解决方法是添加大括号
library(dplyr)
mtcars %>% {
c(.$mpg, .$cyl)
}
Run Code Online (Sandbox Code Playgroud)
但它并不漂亮。
cR 中使用管道的函数是否有替代方案?
library(dplyr)
mtcars %>%
alternative_c(.$mpg, .$cyl) # Does such a function exist?
Run Code Online (Sandbox Code Playgroud) # Input
n <- 2
"abcd"
# Output
c("ab", "bc", "cd")
Run Code Online (Sandbox Code Playgroud)
I don't want to use a for loop or sapply