说我有一个shiny::sliderInput:
...
sliderInput("input_1", "Title_1",
min = 1, max = 10, value = 5)
...
Run Code Online (Sandbox Code Playgroud)
是否可以引用min、max和/或value不同的sliderInput?这样做的用例是使第二个输入依赖于第一个输入。调整第二个输入的最小值的东西永远不会小于来自 的值input_1。
类似的东西(这不起作用):
sliderInput("input_2", "Title_2",
min = input_1$value, max = 10, value = input_1$value)
Run Code Online (Sandbox Code Playgroud)
我的预感是这可能与renderUI,但不确定从哪里或如何开始?
我正在使用R中的内置economics(来自ggplot2包)数据集,并使用以下代码为同一图中的每个变量绘制了时间序列:
library(reshape2)
library(ggplot2)
me <- melt(economics, id = c("date"))
ggplot(data = me) +
geom_line(aes(x = date, y = value)) +
facet_wrap(~variable, ncol = 1, scales = 'free_y')
Run Code Online (Sandbox Code Playgroud)
现在,我进一步想要改进我的图表,对于每个系列,我想显示最小值和最大值的红点.所以我想如果我能找到每个时间序列的最小值和最大值的坐标,我就能找到一种在每个时间序列的开头和结尾绘制红点的方法.为此我使用了以下代码:
which(pce == min(economics$pce), arr.ind = TRUE)
which(pca == max(pca), arr.ind = TRUE)
Run Code Online (Sandbox Code Playgroud)
这并没有真正引导我到任何地方.谢谢:)
考虑以下示例:
tab <- table(mtcars$vs, mtcars$cyl, dnn = c("vs", "cylinder"))
prop.table(tab)
# cylinder
# vs 4 6 8
# 0 0.03125 0.09375 0.43750
# 1 0.31250 0.12500 0.00000
round(prop.table(tab)*100, 1)
# cylinder
# vs 4 6 8
# 0 3.1 9.4 43.8
# 1 31.2 12.5 0.0
Run Code Online (Sandbox Code Playgroud)
期望的输出:
# cylinder
# vs 4 6 8
# 0 3.1% 9.4% 43.8%
# 1 31.2% 12.5% 0.0%
Run Code Online (Sandbox Code Playgroud)
scales::percent(round(prop.table(tab))) 不起作用,因为没有适用的方法应用于plyr::round_any()class 的对象table。
我知道我缺少一个简单的解决方法。或者也许一个简单的包装器或拉取请求plyr::round_any()可以为每个人解决这个问题?
请考虑以下数据:
library(ggplot2)
library(lubridate)
date <- seq.Date(ymd("2015-01-01"), Sys.Date(), by = "day")
df <- data.frame(date = date,
value = seq_along(date) + rnorm(length(date), sd = 100))
# Add yday and year
df$yday <- yday(df$date)
df$year <- year(df$date)
head(df)
# date value yday year
# 1 2015-01-01 97 1 2015
# 2 2015-01-02 89 2 2015
# 3 2015-01-03 68 3 2015
# 4 2015-01-04 57 4 2015
# 5 2015-01-05 70 5 2015
# 6 2015-01-06 100 6 2016
Run Code Online (Sandbox Code Playgroud)
我想制作一个"年复一年"的情节,颜色分配给年份.我可以用以下方法做到这一点:
ggplot(df, aes(x = …Run Code Online (Sandbox Code Playgroud) 有没有办法使用R从字符串中提取没有空格或其他分隔符的单词?我有一个URL列表,我想弄清楚URL中包含哪些单词.
input <- c("babybag", "badshelter", "themoderncornerstore", "hamptonfamilyguidebook")
Run Code Online (Sandbox Code Playgroud) 我有描述父子关系的数据:
df <- tibble::tribble(
~Child, ~Parent,
"Fruit", "Food",
"Vegetable", "Food",
"Apple", "Fruit",
"Banana", "Fruit",
"Pear", "Fruit",
"Carrot", "Vegetable",
"Celery", "Vegetable",
"Bike", "Not Food",
"Car", "Not Food"
)
df
#> # A tibble: 9 x 2
#> Child Parent
#> <chr> <chr>
#> 1 Fruit Food
#> 2 Vegetable Food
#> 3 Apple Fruit
#> 4 Banana Fruit
#> 5 Pear Fruit
#> 6 Carrot Vegetable
#> 7 Celery Vegetable
#> 8 Bike Not Food
#> 9 Car Not Food
Run Code Online (Sandbox Code Playgroud)
在视觉上,这看起来像: …
假设我有一个定义如下的函数:
myFun <- function(x, y) {
return(2*x + y)
}
Run Code Online (Sandbox Code Playgroud)
myFun我可以通过在控制台中简单地键入(不带括号)来打印函数的定义:
> myFun
function(x, y) {
return(2*x + y)
}
Run Code Online (Sandbox Code Playgroud)
如何返回(或打印)名称和函数?也就是说,我希望控制台(打印)输出为:
myFun <- function(x, y) {
return(2*x + y)
}
Run Code Online (Sandbox Code Playgroud)
如果无法打印定义该函数的代码,是否有办法cat或print以这样的方式可以在正常结果前myFun加上文本“myFun <-”?
我需要一种方法来打印预测值.
我需要打印深蓝色线值,如果可能的话,还需要打印下图中灰色区域的值.
打印该值或打印2019预测值的代码是什么?
library(forecast)
timese <- ts(WWWusage, start = c(2008, 1), end = c(2016, 1), frequency = 12)
### Structural Time Series Model
# Trend likelihood
fit <- StructTS(timese, "trend")
### Make the plot
plot(forecast(fit, level = c(70, 90)),
sub = "Confidence Interval 70% ~ 90% or Determined by user",
ylab = "Y Axis Variable",
main = "Forecast Linear Structural Model @ Trend-Wise",
ylim = c(0, 400))
Run Code Online (Sandbox Code Playgroud)
有没有办法使用节点为纽带,以使用功能的外部网站的方式forceNetwork()在networkD3包r?我在想也许修改clickAction?
示例数据:
library(networkD3)
data(MisLinks)
data(MisNodes)
# Create a random URL in the nodes dataset
MisNodes$URL <- paste0("http://www.RANDOMLINK_", sample(1:100, NROW(MisNodes)), ".com")
head(MisNodes)
MyClickScript <- 'alert(d.index)'
forceNetwork(Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8,
clickAction = MyClickScript)
Run Code Online (Sandbox Code Playgroud)
期望的结果:当用户点击节点时,将打开一个新选项卡(例如window.open)指向该节点的关联URL - 我如何clickAction指向MisNodes$URL[d.index]?
给出以下数据框:
df <- data.frame(start = c("005", "010", "014"),
end = c("005", "013", "017"),
zone = c(3, 5, 7))
# df
# start end zone
# 1 005 005 3
# 2 010 013 5
# 3 014 017 7
Run Code Online (Sandbox Code Playgroud)
我想生成以下结果:
# key zone
# 1 005 3
# 2 010 5
# 3 011 5
# 4 012 5
# 5 013 5
# 6 014 7
# 7 015 7
# 8 016 7
# 9 017 7
Run Code Online (Sandbox Code Playgroud)
我想我可以利用一些东西 …
我是网络抓取的新手,我正试图在多个网页上刮取表格.这是网站:http://www.baseball-reference.com/teams/MIL/2016.shtml
我能够轻松地在一页上刮一张桌子rvest.有多个表,但我只想抓第一个,这是我的代码
library(rvest)
url4 <- "http://www.baseball-reference.com/teams/MIL/2016.shtml"
Brewers2016 <- url4 %>% read_html() %>%
html_nodes(xpath = '//*[@id="div_team_batting"]/table[1]') %>%
html_table()
Brewers2016 <- as.data.frame(Brewers2016)
Run Code Online (Sandbox Code Playgroud)
问题是我想要抓住可追溯到1970年的页面上的第一个表格.在表格上方的左上角有一个指定前一年的链接.有人知道我怎么做吗?
我也对不同的方法持开放态度,例如,除了rvest之外的其他方法可能会更好.我用rvest是因为它是我开始学习的那个.
鉴于以下情况:
library(tidyverse)
my_tibble <- tibble(
var_1 = c("label_a", "label_b", "label_c"),
var_2 = c("another_x", "another_y", "another_z"),
result = list(
data.frame(x = 1, y = 2, z = 3),
data.frame(x = 4, y = 5, z = 6),
data.frame(x = 5, y = 10, z = 11)
)
)
my_tibble
# # A tibble: 3 x 3
# var_1 var_2 result
# <chr> <chr> <list>
# 1 label_a another_x <df[,3] [1 x 3]>
# 2 label_b another_y <df[,3] [1 x 3]>
# …Run Code Online (Sandbox Code Playgroud) r ×12
dplyr ×2
ggplot2 ×2
tidyr ×2
crosstab ×1
data-munging ×1
forecasting ×1
formatting ×1
hierarchical ×1
htmlwidgets ×1
lubridate ×1
networkd3 ×1
percentage ×1
plot ×1
plyr ×1
prediction ×1
purrr ×1
rvest ×1
shiny ×1
tidyverse ×1
web-scraping ×1