我对bash脚本感到困惑.
我有以下代码:
function grep_search() {
magic_way_to_define_magic_variable_$1=`ls | tail -1`
echo $magic_variable_$1
}
Run Code Online (Sandbox Code Playgroud)
我希望能够创建一个包含命令的第一个参数并带有例如最后一行的值的变量名ls
.
所以说明我想要的东西:
$ ls | tail -1
stack-overflow.txt
$ grep_search() open_box
stack-overflow.txt
Run Code Online (Sandbox Code Playgroud)
那么,我应该如何定义/声明$magic_way_to_define_magic_variable_$1
以及如何在脚本中调用它?
我已经试过eval
,${...}
,\$${...}
,但我仍然感到困惑.
当我渲染一个小插图时
rmarkdown::render('/path/to/pkg/vignettes/my-vignette.Rmd')
Run Code Online (Sandbox Code Playgroud)
我遇到错误
processing file: draft-vignette.Rmd
Error in get0(oNam, envir = ns) :
lazy-load database '/home/username/R/x86_64-pc-linux-gnu-library/3.3/stringr/R/stringr.rdb' is corrupt
In addition: Warning messages:
1: In get0(oNam, envir = ns) : restarting interrupted promise evaluation
2: In get0(oNam, envir = ns) : internal error -3 in R_decompress1
Run Code Online (Sandbox Code Playgroud)
运行devtools::build_vignettes(pkg = '/path/to/pkg')
会产生类似的错误。
奇怪的是,
devtools::build(pkg = '/path/to/pkg')
Run Code Online (Sandbox Code Playgroud)
运行成功,生成压缩包文件,并正确呈现小插图。
我也尝试删除/重新安装stringr
软件包,但这没有帮助。
我只想渲染小插图而不渲染其他任何东西,那么如何解决上述错误?
我想将不同货币的某些价格转换为特定货币.假设我有这个:
library(data.table)
set.seed(100)
DT <- data.table(day=1:10, price=runif(10), currency=c("aud","eur"),
aud=runif(10) + 1, eur=runif(10) + 1.5)
DT
day price currency aud eur
1: 1 0.30776611 aud 1.624996 2.035811
2: 2 0.25767250 eur 1.882166 2.210804
3: 3 0.55232243 aud 1.280354 2.038349
4: 4 0.05638315 eur 1.398488 2.248972
5: 5 0.46854928 aud 1.762551 1.920101
6: 6 0.48377074 eur 1.669022 1.671420
7: 7 0.81240262 aud 1.204612 2.270302
8: 8 0.37032054 eur 1.357525 2.381954
9: 9 0.54655860 aud 1.359475 2.049097
10: 10 0.17026205 eur 1.690291 1.777724 …
Run Code Online (Sandbox Code Playgroud) 我试图找到在 GNU/linux 中进行全屏连续截图的最快方法,而无需任何人工干预。到目前为止,我得到:
$ time for i in {1..10}; do import -window root test-$i.png; done
real 0m9.742s
user 0m11.324s
sys 0m0.584s
$ time for i in {1..10}; do scrot test-$i.png; done
real 0m1.686s
user 0m1.528s
sys 0m0.060s
Run Code Online (Sandbox Code Playgroud)
但是,我想要比 scrot 更快的东西。系统时间是在一台不错的(硬件方面的)台式电脑(运行 Ubuntu Linux)中进行的。有趣的是它托管了一个 kvm 机器(CrunchBang Linux),它返回:
$ time for i in {1..10}; do import -window root test-$i.png; done
real 0m7.591s
user 0m6.096s
sys 0m0.196s
$ time for i in {1..10}; do scrot test-$i.png; done
real 0m2.921s
user 0m2.440s
sys 0m0.120s
Run Code Online (Sandbox Code Playgroud)
因此, …
我很久以前就问过这个问题,但还没找到答案.我不知道这是否在stackoverflow中是合法的,但我重新发布它.
我在R中有一个data.table,我想创建一个新列,找到每个月/月的每个价格的间隔.
可重复的例子:
set.seed(100)
DT <- data.table(year=2000:2009, month=1:10, price=runif(5*26^2)*100)
intervals <- list(year=2000:2009, month=1:10, interval = sort(round(runif(9)*100)))
intervals <- replicate(10, (sample(10:100,100, replace=T)))
intervals <- t(apply(intervals, 1, sort))
intervals.dt <- data.table(intervals)
intervals.dt[, c("year", "month") := list(rep(2000:2009, each=10), 1:10)]
setkey(intervals.dt, year, month)
setkey(DT, year, month)
Run Code Online (Sandbox Code Playgroud)
我刚尝试过:
DT
和intervals.dt
data.tables,intervalsstring
列,包含所有V*列到一个列字符串,(不是很优雅,我承认),最后findInterval()
但解决方案不适用于每一行(!)所以,之后:
DT <- merge(DT, intervals.dt)
DT <- DT[, intervalsstring := paste(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10)]
DT <- DT[, c("V1", "V2", "V3", …
Run Code Online (Sandbox Code Playgroud) 每当我运行它时,我会尝试抑制Saving 7 x 7 in image
输出ggsave()
,但似乎不可能.这可能吗?怎么做?
我尝试了以下,但没有任何作用:
capture.output()
sink()
最小的"工作"示例:
librrary(ggplot2)
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30))
plot.to.be.saved <- ggplot(df) + geom_point(aes(x = gp, y = y))
sink('/dev/null')
ggsave(filename = '~/.so.pdf', plot = plot.to.be.saved)
sink()
# Saving 7 x 7 in image
options(warn=-1)
no.output.please <- ggsave(filename = '~/.so.pdf', plot = plot.to.be.saved)
# Saving 7 x 7 in image
capture.output(ggsave(filename = '~/.so.pdf', plot = plot.to.be.saved), file = 'NUL')
# Saving …
Run Code Online (Sandbox Code Playgroud) 我有一个巨大的data.table
,我想绘制每组的直方图.在下面的例子中,我想说每个"线"有6个图(所以最后一行只有两个图,对于s
和t
组).
我该怎么做呢?
library(ggplot2)
library(data.table)
DT <- data.table(Group = rep(letters[1:20], each = 200),
Value = rnorm(4000))
hist.plot <- ggplot(DT, aes(x = Value)) +
geom_histogram(binwidth = 0.3, colour = 'black')
hist.plot + facet_grid(. ~ Group)
Run Code Online (Sandbox Code Playgroud)
fastLm()
当我用一次观察进行回归时,为什么返回结果?
在下面,为什么不lm()
和fastLm()
结果相等?
library(Rcpp)
library(RcppArmadillo)
library(data.table)
set.seed(1)
DT <- data.table(y = rnorm(5), x1 = rnorm(5), x2 = rnorm(5), my.key = 1:5)
# y x1 x2 my.key
# 1: -0.6264538 -0.8204684 1.5117812 1
# 2: 0.1836433 0.4874291 0.3898432 2
# 3: -0.8356286 0.7383247 -0.6212406 3
# 4: 1.5952808 0.5757814 -2.2146999 4
# 5: 0.3295078 -0.3053884 1.1249309 5
lm(y ~ 1 + x1 + x2, data = DT[my.key == 1])
# Coefficients:
# (Intercept) x1 x2
# -0.6265 …
Run Code Online (Sandbox Code Playgroud) r ×6
data.table ×3
ggplot2 ×2
armadillo ×1
bash ×1
dynamic ×1
imagemagick ×1
intervals ×1
linux ×1
performance ×1
r-markdown ×1
r-package ×1
rcpp ×1
screenshot ×1
syntax ×1
variables ×1
x11 ×1