我有一个简单的问题,我认为我只是没有找到正确的地方,或者RStudio没有按预期行事.我想知道是否有一个选项可以输出所有我的降价代码块的结果,以转到绘图窗口或控制台.
我开始使用R笔记本写入R Markdown.说我输入命令
head(cars)
Run Code Online (Sandbox Code Playgroud)
进入我的.Rmd文档.我按Ctrl + Enter,然后运行该行.向上将汽车数据帧的前6行弹出到我的脚本中.我看到该行已在控制台中运行,但输出不在控制台中.
为什么这会让我烦恼?有时我的代码块很长.然后,我不得不烦恼地滚动到块的末尾以查看我的结果.或者,我键入head(car),运行该行,然后决定我要查看tail(car)并运行该行.在控制台中,我能够看到两个结果(数据帧的头部和尾部),但是在.Rmd文件中,因为我没有同时运行它们,我的"head"调用的结果消失了.这些似乎是小问题但是当它们一遍又一遍地发生时令人沮丧!我知道如果我直接输入控制台,我可以看到结果,但这会中断我的工作流程.
我尝试了什么:除了谷歌搜索,我已经进入RStudio - >工具 - > R Markdown,并确保选项"显示输出内联所有降价文档"未选中 - 没有任何区别.
我在这里错过了一个选项吗?我正在使用R Studio版本1.0.136.
谢谢!
这是代码(使用.Rmd笔记本文件)
---
title: "R Notebook"
output: html_notebook
---
```{r}
# run just this line
head(cars)
# run just this line
tail(cars)
# Both show up below (here in the file) but do not go to console
```
Run Code Online (Sandbox Code Playgroud)
我喜欢dplyr的"progress_estimated"功能,但我无法弄清楚如何在dplyr链中使用进度条.我在这里放置了一个代码可重复的示例.
我有一个非常大的data.frame像这样:
cdatetime latitude longitude
1 2013-01-11 06:40:00 CST 49.74697 -93.30951
2 2013-01-12 15:55:00 CST 49.74697 -93.30951
3 2013-01-07 20:30:00 CST 49.74697 -93.30951
Run Code Online (Sandbox Code Playgroud)
我想用图书馆计算每个日期的日出时间
library(dplyr)
library(StreamMetabolism)
Run Code Online (Sandbox Code Playgroud)
我可以让dplyr的progress_estimated栏在循环中工作,例如:
丑陋的循环(工程)
p <- progress_estimated(nrow(test))
for (i in 1:nrow(test)){
p$tick()$print()
datetime = as.POSIXct(substr(test$cdatetime[i], 1, 20), tz = "CST6CDT")
test$sunrise[i] <- sunrise.set(test$latitude[i], test$longitude[i], datetime, "CST6CDT", num.days = 1)[1,1]
}
Run Code Online (Sandbox Code Playgroud)
但是我怎么能把它嵌入我的函数中,所以我可以避免使用循环?
更喜欢使用:
SunriseSet <- function(dataframe, timezone){
dataframe %>%
rowwise() %>%
mutate(# calculate the date-time using the correct timezone
datetime = as.POSIXct(substr(cdatetime, 1, 20), tz = timezone), …Run Code Online (Sandbox Code Playgroud) 我试图在我的情节中使用女性符号♀.它非常微弱(嗯,它在我的实际图形上看起来很微弱),所以我希望能让它变得大胆.
df <- data.frame(x = c(0, 1), y = c(0, 1))
ggplot(df, aes(x, y)) + geom_point() +
theme_bw() +
annotate("text", x = 0.5, y = 0.7, label = "2016 ?",
size = 7, hjust = 0, colour = "grey50")
Run Code Online (Sandbox Code Playgroud)
我尝试了以下,但似乎没有工作:
ggplot(df, aes(x, y)) + geom_point() +
annotate("text", x = 0.5, y = 0.7, label = "2016~bold(?)",
size = 7, hjust = 0, parse = TRUE)
# error message: Error in parse(text = as.character(lab)) : <text>:1:11: unexpected '<'
#1: 2016~bold(<
^ …Run Code Online (Sandbox Code Playgroud) 我正在尝试填充数据框中的缺失值,但我不想要所有可能的变量组合 - 我只想基于三个变量的分组来填充:coursecode,year和week.
我已经查看了tidyr库中的complete(),但即使在查看使用tidyr :: complete with group_by和https://blog.rstudio.org/2015/09/13/tidyr之后我也无法使用它.-0-3-0 /
我有观察员在一年中的特定星期收集不同课程的数据.例如,数据可能会在我的大型数据集中收集数周1-10,但我只关心在特定课程年组合中发生的缺失周数.例如,
例:
library(dplyr)
library(tidyr)
df <- data.frame(coursecode = rep(c("A", "B"), each = 6),
year = rep(c(2000, 2000, 2000, 2001, 2001, 2001), 2),
week = c(1, 3, 4, 1, 2, 3, 2, 3, 5, 3, 4, 5),
values = c(1:12),
othervalues = c(12:23),
region = "Big")
df
coursecode year week values othervalues region
1 A 2000 …Run Code Online (Sandbox Code Playgroud) 我正在处理类似于此表但更大的遗传数据:
ID allele.a allele.b
A 115 90
A 115 90
A 116 90
B 120 82
B 120 82
B 120 82M
Run Code Online (Sandbox Code Playgroud)
我的目标是针对每个 ID 突出显示哪些等位基因与每个 ID 组第一行列出的等位基因不匹配。我需要将数据导出到格式良好的 excel 文件。
这是我想要的:
我可以使用以下脚本到达那里,但实际脚本涉及大约 67 个“ID”、1000 行数据和 37 列。运行大约需要 5 分钟,所以我希望找到一种可以显着减少处理时间的解决方案。也许来自 tidyverse 的“做”解决方案 - 不知道会是什么样子。
这是我的脚本,包括一个测试 data.frame。还包括一个更大的测试 data.frame 用于速度测试。
library(xlsx)
library(openxlsx)
library(tidyverse)
# Small data.frame
dframe <- data.frame(ID = c("A", "A", "A", "B", "B", "B"),
allele.a = c("115", "115", "116", "120", "120", "120"),
allele.b = c("90", "90", "90", "82", …Run Code Online (Sandbox Code Playgroud) 我在 RStudio 中使用 R Markdown,并且更喜欢使用黑色背景编写代码。我的表格在使用 kable 的输出 html 文档中格式化得很好。但是,我注意到我无法读取 kable 的输出,因为文本颜色是黑色:
但如果我使用 pander,它知道使用白色文本......
我怎样才能让 kable 在 RStudio 中使用白色文本,或者让 pander 在我的报告中生成更漂亮的表格?
我正在分析存储由疾病模拟模型输出的数据的大表(300 000 - 500 000 行)。在模型中,景观中的动物会感染其他动物。例如,在下图中的示例中,动物a1感染了景观中的每只动物,并且感染从动物移动到动物,分支为感染“链”。
在下面的示例中,我想获取存储有关每只动物的信息的表(在下面的示例中, table = allanimals)并仅切出有关 Animald2感染链的信息(我已将d2's 链突出显示为绿色)这样我就可以计算出该感染链的平均栖息地价值。
虽然我的 while 循环有效,但当表存储数十万行并且链有 40-100 个成员时,它就像糖蜜一样缓慢。
关于如何加快速度的任何想法?希望得到tidyverse解决。我知道我的示例数据集“看起来足够快”,但我的数据确实很慢......
示意图:
以下示例数据的所需输出:
AnimalID InfectingAnimal habitat
1 d2 d1 1
2 d1 c3 1
3 c3 c2 3
4 c2 c1 2
5 c1 b3 3
6 b3 b2 6
7 b2 b1 5
8 b1 a2 4
9 a2 a1 2
10 a1 x 1
Run Code Online (Sandbox Code Playgroud)
示例代码:
library(tidyverse)
# make some data …Run Code Online (Sandbox Code Playgroud) 我使用以下命令创建了一个由三层(小提琴图、箱图和蜂群图)组成的图表
library(ggplot2)
library(ggbeeswarm)
df <- data.frame(var1 = sample(c("Condition A", "Condition B"), 50, replace =T),
var2 = sample(c("Group 1", "Group 2"), 50, replace =T),
value = sample(c(1,2,3,4,5,6,7,8,9,10), 50, replace =T))
ggplot(df, aes(x=var1, y=value, fill = var2, group = interaction(var1,var2))) +
geom_violin(position = position_dodge(0.8)) +
geom_boxplot(position = position_dodge(0.8), width = .6, outlier.shape = NA) +
# from ggbeeswarm
geom_quasirandom(method = "pseudorandom", dodge.width=0.8, size = 3, alpha = 0.6) +
scale_fill_manual(values=alpha(c("#848484","#0F185B"), .3)) +
scale_color_manual(values=c("#848484","#0F185B")) +
theme_bw() +
theme(text = element_text(size=15))
Run Code Online (Sandbox Code Playgroud)
剧情是 这样的。 然而,我希望小提琴是箱线图下方的简单阴影区域,即具有透明填充,但没有轮廓,这样箱线和小提琴的线条就不会冲突,并且图形更容易查看。 …
我正在尝试使用ggplot2.
如果我使用 coord_equal 来处理点的大小和形状,我可以非常接近,但是......
示例数据和代码library(ggplot2)
df <- data.frame()
Years <- 1990:2020
for(i in 1:length(Years)) {
Year <- Years[i]
week <-1:52
value <- sort(round(rnorm(52, 50, 30), 0))
df.small <- data.frame(Year = Year, week = week, value = value)
df <- bind_rows(df, df.small)
}
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10)) +
coord_equal()
Run Code Online (Sandbox Code Playgroud)
使用我的真实数据,我想“拉伸”图形,以便我可以更清楚地看到它(我的时间序列更短),并且当我不使用 coord_equal 时,正方形不会填充图形区域:
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = …Run Code Online (Sandbox Code Playgroud) 我有一个像下面这样的图,我需要在其中显示一个图标题和一些长的方面标签。在ggplot2,它看起来很好。
代表:
library(ggplot2)
library(stringr)
library(plotly)
iris$Species2 <- paste(iris$Species, "... some text to make the label really long and hard to put on a facet label")
iris$Species2 <- str_wrap(iris$Species2, 20)
g <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
labs(title = "This title isn't helping anyone") +
facet_wrap(~Species2)
g
Run Code Online (Sandbox Code Playgroud)
但是,转换为动态图并没有按预期工作......刻面标签被切断并进入标题:
gp <- ggplotly(g)
gp
Run Code Online (Sandbox Code Playgroud)
之前有一个关于 this 的 SO question,但看起来 OP 没有尝试答案 - 没有人发现建议的答案没有按预期工作。
plotly当涉及到方面时,我对奇怪的行为并不陌生- 请参阅github 上的对话,但我不太了解plotly修改对象以强制它具有更长的 strip.background。
希望有人可以帮助我修改对象gp …
r ×10
ggplot2 ×4
dplyr ×2
r-markdown ×2
tidyverse ×2
complete ×1
console ×1
excel ×1
facet-wrap ×1
fill ×1
fonts ×1
ggplotly ×1
knitr ×1
openxlsx ×1
output ×1
pander ×1
performance ×1
plotmath ×1
progress-bar ×1
r-plotly ×1
raster ×1
tidyr ×1
time-series ×1
while-loop ×1