我正在使用 ' segmented
' 包来查找 R 中线性回归的断点
library(tidyverse)
library(segmented)
df <- data.frame(x = c(1:10), y = c(1,1,1,1,1,6:10))
lm_model <- lm(y ~ x, data = df)
seg_model <- segmented(obj = lm_model, seg.Z = ~ x)
Run Code Online (Sandbox Code Playgroud)
但是,如果我在 a 中运行相同的模型purrr:map
,则分段会失败。
map_test <- df %>%
nest() %>%
mutate(map_lm = map(data, ~lm(y ~ x, data = .)),
param_map_lm = map(map_lm, tidy))
map_lm_model <- map_test[[2]][[1]]
map_seg_model <- segmented(obj = map_lm_model, seg.Z = ~ x)
Run Code Online (Sandbox Code Playgroud)
“is.data.frame(data) 中出现错误:对象‘.’ 未找到”
当从映射输出中提取的 lm 中获取 lm obj 时,segmented 无法找到底层数据。 …
使用bookdown
我们可以从下载按钮下载不同的文件格式,例如
---
bookdown::gitbook:
download:
- ["test.pdf", "PDF File"]
- ["test.html", "HTML File"]
- ["test.csv", "Data test.csv"]
---
Run Code Online (Sandbox Code Playgroud)
在四开本中,我们只有下载选项:(pdf、epub 和 docx 中的一项或多项)。有人知道如何通过下载按钮包含数据(例如.csv
或)以包含在导航栏或最好的侧边栏上吗?.qs
编辑 1:在 @shafee 的精彩回答之后,这是我的书目前的样子以及我想在其中添加下载数据的选项。
通过单击向下箭头,将打开一个下拉菜单,我想在其中添加“下载数据”选项(最佳情况是多个数据集,即多个数据集)。这是一个例子.yml
:
---
project:
type: book
book:
title: "Quarto Book"
chapters:
- index.qmd
- intro.qmd
sidebar:
pinned: true
repo-url: https://www.rstudio.com/
search:
location: sidebar
type: textbox
downloads: [pdf, epub]
format:
html:
# include-after-body: custom.html
theme: cosmo
---
Run Code Online (Sandbox Code Playgroud)
编辑2:可能有一种方法可以tools
使用sidebar
. 也许有人知道如何正确生成数据的 URL,以便直接下载。
sidebar:
pinned: …
Run Code Online (Sandbox Code Playgroud) 我正在使用四开来准备一些幻灯片(即,revealjs输出),但我找不到缩放嵌入图像的方法(例如,来自维基百科)。看来“宽度”参数应该可以,但百分比都不是
{width="50%"}
Run Code Online (Sandbox Code Playgroud)
也不是绝对值
{width=600}
Run Code Online (Sandbox Code Playgroud)
似乎对渲染的图片有任何影响。有什么线索吗?
干杯。
根据Quarto 文档,下图应该是屏幕宽度,但它只是位于屏幕的左侧。我怎样才能让它变大?添加#| layout-ncol: 1
也没有帮助。
---
title: "Test"
format: html
---
```{r}
#| column: screen
plot(pressure)
```
Run Code Online (Sandbox Code Playgroud) 有人找到了一种方法来突出显示 quarto html 文档中的特定代码行(类似于 Reveal.js 代码突出显示选项?)。例如,在下面的示例中,我想突出显示新添加的group_by()
代码行:
---
format: html
---
```{r}
library(dplyr)
iris |>
summarize(mean(Sepal.Length))
```
Add `group_by()`:
```{r}
iris |>
group_by(Species) |>
summarize(mean(Sepal.Length))
```
Run Code Online (Sandbox Code Playgroud)
然而,我发现这个包仅适用于 RMarkdown。非常感谢任何提示!:)
gt()
如何使用R 中的函数向数据框的标题(即列名称)添加颜色
gt_Head <-
gt_Head %>%
tab_header(
title = md("**Table of First 10 Observations**")
)%>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(
rows = 0
)
)
Run Code Online (Sandbox Code Playgroud) name <- c("Jon", "Bill", "Maria")
agenn <- c(23, 41, 32)
agelk <- c(23, 41, 32)
agepm <- c(23, 41, 32)
df <- data.frame(name, age,agelk,agepm)
print (df)
Run Code Online (Sandbox Code Playgroud)
我想删除列名称包含c("epm","enn","jkk")
.
我有一个关于 的问题tidyr::pivot_longer()
。
假设我从一个虚构的数据框开始。想象一下人们被问到关于两种类型的两个问题。SQ1_2 将是关于第一种类型的第二个问题。
set.seed(1234)
genres <- c("Crime", "Horror", "Love", "Sci-Fi", NA)
wide <- data.frame(
ID = 1:10,
Genre_1 = sample(genres, 10, replace = TRUE),
Genre_2 = sample(genres, 10, replace = TRUE),
SQ1_1 = sample(1:5, 10, replace = TRUE),
SQ1_2 = sample(1:5, 10, replace = TRUE),
SQ2_1 = sample(1:5, 10, replace = TRUE),
SQ2_2 = sample(1:5, 10, replace = TRUE)
)
Run Code Online (Sandbox Code Playgroud)
ID | 流派_1 | 流派_2 | SQ1_1 | SQ1_2 | SQ2_1 | SQ2_2 |
---|---|---|---|---|---|---|
1 | 科幻 | 科幻 | 3 | 5 | 2 | 2 |
2 | 恐怖 | 科幻 … |
语境
我正在这个网站上学习整洁评估,我看到一个例子:
x <- sym("height")
expr(transmute(starwars, bmi = mass / (!! x)^2))
#> transmute(starwars, bmi = mass/(height)^2)
transmute(starwars, bmi = mass / (!! x)^2)
#> # A tibble: 87 x 1
#> bmi
#> <dbl>
#> 1 26.0
#> 2 26.9
#> 3 34.7
#> 4 33.3
#> # ... with 83 more rows
Run Code Online (Sandbox Code Playgroud)
sym
然后,我在自己的代码中使用and模仿了上面的例子!!
,但是报错了。
library(survival)
library(rms)
data(cancer)
x = sym('meal.cal')
expr(cph(Surv(time, status) ~ rcs(!! x), data = lung))
# cph(Surv(time, status) ~ rcs(meal.cal), data …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 R Shiny 中的 renderTable 函数生成一个表,然后使用 downloadHandler 函数将该表/data.frame 下载为 csv 文件。不知怎的,我不断收到以下错误:
下载期间发生错误:下载http://127:0:0.1:3001/session/ 0303bd426fce88837ae277aa3b406dd/download/downloadData?w=时出错 - 服务器回复:内部服务器错误
下面是一个示例代码,我在其中生成一个简单的数据帧并尝试使用 downloadHander 下载它:
library(shiny)
# Define UI for data download app ----
ui <- fluidPage(
# App title ----
titlePanel("Downloading Data"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Button
downloadButton("downloadData", "Download")
),
# Main panel for displaying outputs ----
mainPanel(
tableOutput("table")
)
)
)
# Define server logic to display and download selected …
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数据框:
A <- data.frame(Red = c(1,0,0,0),
Blue = c(0,1,0,1),
Yellow = c(0,0,1,0))
Run Code Online (Sandbox Code Playgroud)
我想结合这些信息以获得此专栏:
B <- data.frame(Colour = c("Red", "Blue", "Yellow", "Blue"))
Run Code Online (Sandbox Code Playgroud)
即,出现 1 的列的名称。
我想从日历年变量(此处为 1991 到 2001)创建日历年的开始日期和结束日期,并找到了解决方案:
\nlibrary(lubridate)\nlibrary(dplyr)\ndf <- tibble(calendar_year = seq(1991, 2001, 1)) \n\ndf |>\n mutate(\n calendar_year_start_date = ymd("1991-01-01") + \n years(calendar_year - 1991),\n calendar_year_end_date = ymd("1991-12-31") + \n years(calendar_year - 1991 )\n )\n
Run Code Online (Sandbox Code Playgroud)\n结果:
\n# A tibble: 11 \xc3\x97 3\n calendar_year calendar_year_start_date calendar_year_end_date\n <dbl> <date> <date> \n 1 1991 1991-01-01 1991-12-31 \n 2 1992 1992-01-01 1992-12-31 \n 3 1993 1993-01-01 1993-12-31 \n 4 1994 1994-01-01 1994-12-31 \n 5 1995 1995-01-01 1995-12-31 \n 6 1996 1996-01-01 1996-12-31 \n 7 …
Run Code Online (Sandbox Code Playgroud)