我想传递arrange(){dplyr}一个变量名的向量来排序.通常我只需输入我想要的变量,但我正在尝试创建一个函数,其中排序变量可以作为函数参数输入.
df <- structure(list(var1 = c(1L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 4L, 4L
), var2 = structure(c(10L, 1L, 8L, 3L, 5L, 4L, 7L, 9L, 2L, 6L
), .Label = c("b", "c", "f", "h", "i", "o", "s", "t", "w", "x"
), class = "factor"), var3 = c(7L, 5L, 5L, 8L, 5L, 8L, 6L, 7L,
5L, 8L), var4 = structure(c(8L, 5L, 1L, 4L, 7L, 4L, 3L, 6L, 9L,
2L), .Label = c("b", "c", "d", "e", "f", "h", "i", …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ggvis为2014赛季创建一个NFL力量表.这些数据来自FootballOutsiders.com,后来我将制作一个Shiny应用程序,在该季节更新时会自动从网站上删除数据.下面的例子非常接近,但我想做一些修改.我想要...
在图表的每个单元格中包含"防御"的数值,因此可视化类似于原始的"df"数据框.
自定义色标,使正值越来越橙色,负值越来越蓝(即更负 - 更蓝).
#2的替代方案可以是从橙色到蓝色的渐变,并且当"防御"的值接近零时,使不透明度降低到0.5.
能够选择NA的颜色,因为它当前在图表中显示为黑色.
我一直在摆弄周围add_scale()和props(),但没有迄今工作.
这是图表:

这是数据:
df <- structure(list(team = c("ARI", "ATL", "BAL", "BUF", "CAR", "CHI",
"CIN", "CLE", "DAL", "DEN", "DET", "GB", "HOU", "IND", "JAX",
"KC", "MIA", "MIN", "NE", "NO", "NYG", "NYJ", "OAK", "PHI", "PIT",
"SD", "SEA", "SF", "STL", "TB", "TEN", "WAS"), w1 = c(17.5, -5.8,
-12.6, 8.7, -6.8, -13.8, -8.7, 4, -4.6, 0.9, -11.4, -25.9, 4.2,
-0.2, 4.9, 4.2, 4.2, -5.7, 2.4, 13.5, -0.8, 10.3, -5.6, 10.9,
8.2, -16.4, 14.4, 13.8, …Run Code Online (Sandbox Code Playgroud) 我有一个带左侧边栏的Shiny应用程序,我想将mainPanel中的图形对齐到右侧.我已经尝试添加style = "align:right"到mainPanel中的每个元素,以及包装我能想到的所有内容div(..., style = "align:right").这些都没有效果.
您可以在RStudio库中使用此示例.我想将"表"选项卡中的输出对齐到右侧.这是ui.R的相关部分:
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
Run Code Online (Sandbox Code Playgroud) 我正在开发一个模型,预测一个年龄组的生育能力.我目前有一个这样的数据框,其中行是年龄,列是年.每个细胞的价值是该年度的特定年龄生育率:
> df1
iso3 sex age fert1953 fert1954 fert1955
14 AUS female 13 0.000 0.00000 0.00000
15 AUS female 14 0.000 0.00000 0.00000
16 AUS female 15 13.108 13.42733 13.74667
17 AUS female 16 26.216 26.85467 27.49333
18 AUS female 17 39.324 40.28200 41.24000
Run Code Online (Sandbox Code Playgroud)
但是,我想要的是每一行都是一个队列.因为行和列表示各个年份,所以可以通过获得对角线来获得群组数据.我正在寻找这样的结果:
> df2
iso3 sex ageIn1953 fert1953 fert1954 fert1955
14 AUS female 13 0.000 0.00000 13.74667
15 AUS female 14 0.000 13.42733 27.49333
16 AUS female 15 13.108 26.85467 41.24000
17 AUS female 16 26.216 40.28200 …Run Code Online (Sandbox Code Playgroud) 在Jupyter Lab中,我希望将代码从编辑器发送到Python控制台以执行,最好使用键盘快捷键。该文档似乎没有提供执行此操作的方法,但是它是IDE的一个基本方面,我想可能是可能的。
对于DateR 中的对象,是否可以"%Y-%m-%d"在保留其Date类的同时选择与默认值不同的打印格式?该format()函数将其转换回character字符串。
# I start with a character string and convert it to a date
date_char <- "01-05-2015"
date <- as.Date(date_char, format = "%d-%m-%Y")
# By default, R re-formats the date as "%Y-%m-%d"
date
# [1] "2015-05-01"
# I want to keep the Date class, but with the original format
# format() converts it back to a character variable
str(format(date, "%d-%m-%Y"))
# chr "01-05-2015"
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个生成字符串向量的函数,每个函数都被评估为全局环境中的表达式.问题是eval(parse(text = x))仅在函数环境中进行求值.
作为一个假设的例子,假设我想用NA替换几个变量的值,但前提是它们低于某个截止值.
set.seed(200)
df <- as.data.frame(matrix(runif(25), nrow=5, ncol=5))
df
V1 V2 V3 V4 V5
1 0.5337724 0.83929374 0.4543649 0.3072981 0.46036069
2 0.5837650 0.71160009 0.6492529 0.5667674 0.09874701
3 0.5895783 0.09650122 0.1537271 0.1317879 0.20659381
4 0.6910399 0.52382473 0.6492887 0.9221776 0.92233983
5 0.6673315 0.23535054 0.3832137 0.6463296 0.31942681
cutoff.V1 <- 0.9
cutoff.V2 <- 0.5
cutoff.V3 <- 0.1
cutoff.V4 <- 0.7
cutoff.V5 <- 0.4
Run Code Online (Sandbox Code Playgroud)
而不是一遍又一遍地复制和粘贴相同的行,改变每行中的相同文本......
df$V1[df$V1 < cutoff.V1] <- NA
df$V2[df$V2 < cutoff.V2] <- NA
df$V3[df$V3 < cutoff.V3] <- NA
df$V4[df$V4 < cutoff.V4] …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Shiny 应用程序中创建一个定时计数器。当它每秒递增时,它应该用一些取决于计数器的新特性刷新一个图。这是一个基于“Old Faithful”应用程序的示例。它不起作用,但它得到了这个想法。我试图reactiveTimer()刷新情节,并用reactiveValues().
服务器
library(shiny)
shinyServer(function(input, output) {
refreshPlot <- reactiveTimer(intervalMs = 1000)
vals <- reactiveValues(counter = 0)
output$distPlot <- renderPlot({
refreshPlot()
vals$counter <- isolate(vals$counter) + 1
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
n_bins <- input$bins + vals$counter
bins <- seq(min(x), max(x), length.out = n_bins)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
Run Code Online (Sandbox Code Playgroud)
用户界面
library(shiny) …Run Code Online (Sandbox Code Playgroud)