让我们看一个例子:
# 1. dplyr
mtcars %>% select(mpg, cyl, gear, everything())
# 2. data.table
as.data.table(mtcars)[, .(mpg, cyl, gear)]
Run Code Online (Sandbox Code Playgroud)
在“齿轮”之后应添加什么以具有与“ dplyr”情况相同的输出?
谢谢
有一个很好的例子:https://rstudio.github.io/DT/010-style.html,它允许创建 19 个中断和 20 个从“白色”到“红色”的 RGB 颜色值:
library(DT)
# 1. Data
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))
# 2. Create 19 breaks and 20 rgb color values ranging from white to red
brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
{paste0("rgb(255,", ., ",", ., ")")}
# 3. DT
datatable(df) %>% formatStyle(names(df), backgroundColor =
styleInterval(brks, clrs))
Run Code Online (Sandbox Code Playgroud)
但是如何从“绿色”到“红色”进行相同的着色呢?
谢谢!
有很多用于在 Web 应用程序中制作日历的 JS 库。
如何为 R Shiny 应用程序制作带有标签日期的优雅日历解决方案,例如在 Google 日历中?
谢谢!
让我们看下面的dplyr样式示例。
# 1. Data set
df <- data.table(
g1 = c(1, 1, 2, 1, 2, 2, 1),
g2 = c(2, 1, 3, 3, 1, 1, 2),
status = c(1, 0, 1, 0, 0, 1, 1),
date_obs = as.Date(c("2019-01-01", "2019-01-02", "2019-01-12", "2019-01-15",
"2019-01-20", "2019-01-24", "2019-01-30")))
# 2. Arrange data
df <- df %>%
arrange(g1, g2, date_obs)
# 3. Populate missing 'date_obs' and 'status' values
df_filled <- df %>%
group_by(g1, g2) %>%
complete(date_obs = seq.Date(min(date_obs), max(date_obs), by = "day")) %>% …Run Code Online (Sandbox Code Playgroud) 我有字符串“178960.02”,应该用浮点数转换为数字。
这仅生成没有“.02”的整数值“178960”。
如何具有浮动部分的双值
as.numeric("178960.02")
as.double("178960.02")
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个带有单选按钮的 R Shiny UI 组件,并且想手动更改代码中选定的值。
假设有 3 个值:“留在此处”、“执行”和“离开”。通过选择“Do it”,用户会看到操作按钮“Do it”。我想在用户单击操作按钮“Do it”后更改“stay_here”上选定的单选按钮值。这是代码。
library(shiny)
# UI
ui <- fluidPage(
fluidPage(
fluidRow(uiOutput("rbtn_test")),
fluidRow(uiOutput("uo_stay_here")),
fluidRow(uiOutput("uo_do_it")),
fluidRow(uiOutput("uo_go_away"))))
# SERVER
server <- function(input, output) {
# 1. Radio buttons
output$rbtn_test <- renderUI({
# 1.1. Values
actions_values <- c("stay_here", "do_it", "go_away")
names(actions_values) <- c("Stay here", "Do it", "Go away")
# 1.2. Selected value
action_selected <- "stay_here"
# 1.3. UI element
radioButtons(
"rbtn_test_selected", "Actions",
choices = actions_values,
selected = action_selected,
inline = TRUE)
})
# 2. 'Stay here' …Run Code Online (Sandbox Code Playgroud) 我有以下数据集
df <- data.table(
id = c(1),
field_a.x = c(10),
field_a.y = c(20),
field_b.x = c(30),
field_b.y = c(40))
Run Code Online (Sandbox Code Playgroud)
而且,我想把它变成
df_result <- data.table(
id = c(1),
field_name = c("field_a", "field_b"),
x = c(10, 30),
y = c(20, 40))
Run Code Online (Sandbox Code Playgroud)
通过使用“pivot_longer”函数并考虑后缀“.x”和“.y”。
我的真实数据中会有更多的字段。但我想看看如何处理 2 例如。
谢谢!
有一个选项可以在datatables.net网站上添加自定义按钮。如何在R Shiny应用中进行编码?一个按钮和观察者的基本R代码示例将非常有趣。
这是来自https://datatables.net/extensions/buttons/examples/initialisation/custom.html的 JS代码
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
{
text: 'My button',
action: function ( e, dt, node, config ) {
alert( 'Button activated' );
}
}
]
} );
} );
Run Code Online (Sandbox Code Playgroud)
谢谢 !
有一个数据集,其中前行和后行的特征值为零。如何以优雅的方式删除此类行?
# Library
library(tidyverse)
# 1. Input
data.frame(
id = c(1:10),
value = c(0, 0, 1, 3, 0, 1, 2, 8, 9, 0))
# 2. Delete leading and trimming rows with 'value = 0'
# ...
# 3. Desired outcome
data.frame(
id = c(3:9),
value = c(1, 3, 0, 1, 2, 8, 9))
Run Code Online (Sandbox Code Playgroud)
谢谢。
r ×9
dplyr ×3
shiny ×3
data.table ×2
dt ×2
button ×1
calendar ×1
dataframe ×1
label ×1
missing-data ×1
pivot ×1
radio-button ×1
rows ×1
tidyr ×1