小编sym*_*ush的帖子

RMarkdown 和 RStudio 中的键盘快捷键 Ctrl+B 和 Ctrl+I

当写在RMarkdown我报告意识到,创下Ctrl+B大胆的Ctrl+I斜体不会强调标记文本。许多其他 Markdown 编辑器(例如 StackOverflow 上的编辑器)都可以完成这项工作。

目前,我Shift+*在标记了text我想以粗体显示后使用的那一刻,返回*text*将以斜体显示的返回。然而,为了做出大胆的事情,我需要这样做两次。并且此过程无法返回操作(删除 **)。

RStudio 中是否有隐藏按钮可以打开此选项?或者是否有其他解决方案可以解决此问题,例如执行此工作的包?

rstudio r-markdown

6
推荐指数
1
解决办法
507
查看次数

什么是回调机制以及它如何在 R 中应用

有人可以用更简单的非计算机科学术语向我解释 R ( taskCallback, getTaskCallbackNames, taskCallbackManager... 等) 中的回调功能是什么?我看过 R 帮助,但我发现信息有点太抽象了。

它的设计目的是什么,用户如何使用它们?

如果有人可以解释一般概念(用 R 和其他计算机语言),并用 R 提供示例,我将不胜感激,因为我从来没有真正理解过。它与递归函数有什么关系,还是我被名称回调误导了?

r

5
推荐指数
1
解决办法
3884
查看次数

将ggmap对象保存到可以重用的文件中?

是否可以将ggmap保存到本地文件?

背景:我需要一个大区域的高分辨率地图,这需要stamen服务器很长时间才能获得.我认为将地图保存一次,将其保存到文件然后继续处理本地副本会更好.

我获取地图的代码是:

library(ggmap)
map <- get_stamenmap(bbox = c(left = 8.7, bottom = 46.8, right = 9.7, top = 47.6), zoom = 14, maptype = c("toner"))
Run Code Online (Sandbox Code Playgroud)

我知道我可以将其保存为图像,例如:

png(filename=name, width=1280, height=1280)
print(map)
dev.off()
Run Code Online (Sandbox Code Playgroud)

但这没有用,因为我想稍后使用地图在其上绘制不同的点和多边形,这只有在地图知道图片上每个点的"lat"和"lon"时才有可能.

那么,有没有办法将地图保存在原始数据表格中?

gis plot r save ggmap

5
推荐指数
1
解决办法
3747
查看次数

带有自定义字体的 ggplot 在闪亮应用程序上无法正确显示

我可以自定义字体ggplot

\n\n
library(extrafont)\n\nwindowsFonts()\nfont_import(pattern = "comic", prompt = FALSE)\nloadfonts(device = "win")\nwindowsFonts()\n\nggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) + \n  geom_line(position="jitter", color="red", size=2) + theme_bw() +\n  theme(text=element_text(size=16,  family="Comic Sans MS"))\n
Run Code Online (Sandbox Code Playgroud)\n\n

这呈现为:

\n\n

在此输入图像描述

\n\n

有关该主题的更多信息,例如此处此处

\n\n
\n\n

我还可以将该图和 extrafont 集成到一个闪亮的应用程序中,该应用程序在本地运行,如下所示:

\n\n
library(ggplot2)\nlibrary(extrafont)\nlibrary(shiny)\n\nfont_import(paths = "www", pattern = "comic", prompt = FALSE)\nloadfonts()\nprint(fonts())\n\nui <- fluidPage(plotOutput("plot"),textOutput("fonts"))\n\nserver <- function(input, output) {\n   output$plot <- renderPlot({\n     ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) + \n       geom_line(position="jitter", color="red", size=2) + theme_bw() +\n       theme(text=element_text(size=16,  family="Comic Sans MS"))\n   })\n   output$fonts <- renderText(print(fonts()))\n}\n\nshinyApp(ui = ui, server = server)\n …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 shiny extrafont

5
推荐指数
1
解决办法
2039
查看次数

如何在ggplot2`geom_curve`函数中传递各个曲率参数?

我有一个df带有两个曲线定义的曲线,每个曲线定义包含两个点和一个曲率值。目标是使用ggplot2 geom_curve(或替代方法)绘制两条单独的曲线。

我可以使用以下命令生成预期的输出:

df <- data.frame(x = c(0,.2), y = c(0,.3), xend = c(1,.4), yend = c(1,.6), curvature = c(-.2,.4))
ggplot(df) + geom_curve(data = df[1, ], aes(x = x, y = y, xend = xend, yend = yend), curvature = df$curvature[1]) + geom_curve(data = df[2, ], aes(x = x, y = y, xend = xend, yend = yend), curvature = df$curvature[2])
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但这并不是真正的解决方案,因为在我的实际情况下,我有更多的曲线(而且我不知道事先有多少条曲线)。

如何将单个curvature参数传递给geom_curve通话?


我试过了:

df <- data.frame(x = c(0,0), y = c(0,0), …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
195
查看次数

更改 RMarkdown pdf 输出中图形标题的字体大小

我想让 R Markdown 文档中所有标题(图形和表格)的字体大小变小。我正在使用bookdown。最终输出是 pdf,我在 R Studio 中工作。要加载图片,我使用include_graphicsfrom的函数knitr,因为有人告诉我这是最好的方法(请参阅此处)。

我只找到了完全相同的问题,但对于 html 输出在这里

一个例子.rmd

---
output: pdf_document
---

Normal text has the same size as the captions.

```{r, echo = FALSE, out.width = '50%', fig.cap = "The caption has the same size as normal text."}
knitr::include_graphics('logo.png')
```
Run Code Online (Sandbox Code Playgroud)

如您所见,标题字体大小和常规文本字体大小完全相同,看起来不太好。我怎么解决这个问题?

r r-markdown bookdown

5
推荐指数
1
解决办法
3201
查看次数

我们如何在 Shiny 应用程序中使用“names()”或名称“reactiveVal”的替代方案?

names()我尝试在闪亮的应用程序内部使用reactiveVal但没有成功。


这可能吗?在闪亮的应用程序中命名反应值的替代方法是什么?


我的第一次尝试抛出一个错误:

names(r()) <- "something"
Run Code Online (Sandbox Code Playgroud)

名称错误(r())<-“某事”:赋值左侧无效(NULL)

我的第二次尝试也是如此:

names(r) <- "something"
Run Code Online (Sandbox Code Playgroud)

名称(r)<-“某物”中的错误:名称()应用于非向量


这是一个最小的示例应用程序:

library(shiny)

ui <- fluidPage(mainPanel(textOutput("text")))

server <- function(input, output) {
  r <- reactiveVal(1)
  # names(r) <- "something"
  output$text <- renderText(
    paste0("The reactiveVal is ", r(),". It's name is ", names(r()),"."))
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

r names reactive-programming shiny

5
推荐指数
1
解决办法
1602
查看次数

ggplot抛出错误“找不到标签”,而“标签”显然在那里

我可以画出下面的标签df使用geom_text

df <- data.frame(
  x = c(610, 426, 569, 253), 
  y = c(-226, -276, -364, -185), 
  label = c("accomplishments per week", "hours worked per week", "perceived adequacy of accomplishments", "energy level"),
  stringsAsFactors = FALSE
)

ggplot(df, aes(x, y)) + geom_text(aes(label = label))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但是,当尝试对真实数据使用相同的绘图机制时,出现错误:

Error in FUN(X[[i]], ...) : object 'label' not found

为什么会这样,我该如何解决?


这是我的真实数据df1

df1 <- structure(list(type = c("var", "var", "var", "var"),
                  id = c(1,2, 4, 7), 
                  x = c(610, 426, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

4
推荐指数
1
解决办法
100
查看次数

Partially evaluate expression when doing non-standard evaluation in R

I'm digging my head into R's non-standard evaluation mechanisms. Halfway there, I'm still stuck from time to time. ;-)

I made a little calculator that pretty-prints the calculation together with the result:

calculator <- function(e) cat(deparse(substitute(e)), "=", e)
Run Code Online (Sandbox Code Playgroud)

This works fine:

calculator(1 + 2)
Run Code Online (Sandbox Code Playgroud)

prints:

1 + 2 = 3
Run Code Online (Sandbox Code Playgroud)

However, when calling the calculator with:

a <- 1; b <- 2
calculator(a + b)
Run Code Online (Sandbox Code Playgroud)

the output is:

a + b = 3
Run Code Online (Sandbox Code Playgroud)

How can I tweak my calculator in …

r non-standard-evaluation

4
推荐指数
1
解决办法
54
查看次数

如何使用 R markdown 和 bookdown 将图形列表和表格列表添加到目录中

我有一份报告(pdf 输出),我想在附录中添加参考书目、图表列表和表格列表。我希望这三个元素出现在目录中。

我通过添加到 yaml 标头来添加参考书目。bibliography: bibliography.bib我直接用 LaTex 添加的图形列表和表格列表。

因此我的附录看起来像:

# Literature {-}

\listoffigures

\listoftables
Run Code Online (Sandbox Code Playgroud)

几乎有效:参考书目、图表列表和表格列表都正确呈现。

唯一的问题是,目录只显示一个条目Literature。不出现图表列表和表格列表。

我怎样才能解决这个问题?


我尝试在 yaml 标头的各个位置使用lof: yes和(来自此处的想法以及(来自同一来源)lot: yestoc_appendix: yes

我缺少什么?


我的完整标题如下所示:

---
title: something
header-includes: \usepackage{graphicx} \usepackage{float} \usepackage{xcolor} \usepackage{framed} \usepackage{longtable} \definecolor{shadecolor}{RGB}{240,240,240} \pagenumbering{roman} \usepackage{caption} \captionsetup{font=footnotesize}
output:
  bookdown::pdf_book:
    fig_caption: yes
    highlight: kate
    number_sections: yes
    toc: yes
date: | 
    |  
    | `r format(Sys.time(), '%B %d, %Y')`
documentclass: report
geometry: margin=1in
fontfamily: mathpazo
fontsize: 11pt
bibliography: bibliography.bib …
Run Code Online (Sandbox Code Playgroud)

markdown r r-markdown bookdown

4
推荐指数
1
解决办法
6962
查看次数

使用`deparse(substitute))`(或替代方法)时,如何处理空格?

我正在编写一些代码,将数学函数定义转换为有效的R代码。因此,我deparse(substitute))可以访问这些功能定义,以便可以将其更改为有效的R代码。


例如,我具有LN(x)^y应成为的功能log(x)^y。我可以使用to_r函数的第一个版本执行此操作:

to_r <- function(x) {
  parse(text = gsub("LN", "log", deparse(substitute(x))))
}
to_r(LN(x)^y)
Run Code Online (Sandbox Code Playgroud)

这将返回expression(log(x)^y)我所期望的。


我也得到类似的函数定义LN("x a")^y。为了处理这些,我可以扩展我的功能:

to_r_2 <- function(x) {
  parse(text = gsub(" ", "_", gsub("\"", "", gsub("LN", "log", deparse(substitute(x))))))
}
to_r_2(LN("x a")^y)
Run Code Online (Sandbox Code Playgroud)

这样返回expression(log(x_a)^y)就可以了。


但是,当我的输入变成这样时,LN("x a")*2^y将失败:

parse(text = gsub(" ", "_", gsub("\"", "", gsub("LN", "log", deparse(substitute(LN("x a")*2^y))))))
Run Code Online (Sandbox Code Playgroud)

解析错误(text = gsub(“”,“ _”,gsub(“ \”“,”“,gsub(” LN“,” log“,::1:9:意外输入1:log(x_a)_ ^

原因是在deparse(substitute(LN("x a")*2^y))前后引入空白*gsub那些带下划线的空白对我来说是个问题parse …

r

3
推荐指数
1
解决办法
92
查看次数

使用此列中的数据时排除“dplyr”“mutate_at”中的列

我想按一个特定的值重新调整所有变量(但是yeargender),并按 分组:dfyeargender

set.seed(1)
df <- data.frame(gender = c(rep("m", 5), rep("f", 5)), year = rep(1:5, 2), var_a = 1:10, var_b = 0:9)
df

   gender year var_a var_b
1       m    1     1     0
2       m    2     2     1
3       m    3     3     2
4       m    4     4     3
5       m    5     5     4
6       f    1     6     5
7       f    2     7     6
8       f    3     8     7
9       f    4     9     8
10      f …
Run Code Online (Sandbox Code Playgroud)

r dplyr

3
推荐指数
1
解决办法
1723
查看次数

`pivot_longer` 操作 - 实现预期输出的更简单方法?

我有一个df形式:

df <- tibble(
  id = c(1,2,3),
  val02 = c(0,1,0),
  val03 = c(1,0,0),
  val04 = c(0,1,1),
  age02 = c(1,2,3),
  age03 = c(2,3,4),
  age04 = c(3,4,5)
)
Run Code Online (Sandbox Code Playgroud)

我想把它变成整洁的格式,比如:

# A tibble: 9 x 4
     id year    val   age
  <dbl> <chr> <dbl> <dbl>
1     1 02        0     1
2     1 03        1     2
3     1 04        0     3
4     2 02        1     2
5     2 03        0     3
6     2 04        1     4
7     3 02        0     3
8     3 …
Run Code Online (Sandbox Code Playgroud)

r dplyr tidyr tidyverse

3
推荐指数
1
解决办法
62
查看次数