小编Jas*_*lns的帖子

带有 R 的 Shiny 应用程序中的相关输入

说我有一个shiny::sliderInput

...
sliderInput("input_1", "Title_1",
            min = 1, max = 10, value = 5)
...
Run Code Online (Sandbox Code Playgroud)

是否可以引用minmax和/或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 shiny

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

突出显示R中刻面ggplot2图中的最小和最大点

我正在使用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)

这并没有真正引导我到任何地方.谢谢:)

visualization r ggplot2

2
推荐指数
1
解决办法
6846
查看次数

如何将scales::percent或scales::percent_format()应用于R中的prop.table以将数字格式化为百分比

考虑以下示例:

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()可以为每个人解决这个问题?

formatting r crosstab percentage plyr

2
推荐指数
1
解决办法
6687
查看次数

通过scale_x_date()和ggplot2创建具有月x轴的年度图

请考虑以下数据:

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 ggplot2 lubridate

2
推荐指数
1
解决办法
2081
查看次数

使用R从没有空格或分隔符的字符串中提取单词

  • babybag - 婴儿包
  • badshelter - 坏庇护所
  • 他们现代的角落 - 现代化的角落商店
  • hamptonfamilyguidebook - 汉普顿家庭指南

有没有办法使用R从字符串中提取没有空格或其他分隔符的单词?我有一个URL列表,我想弄清楚URL中包含哪些单词.

input <- c("babybag", "badshelter", "themoderncornerstore", "hamptonfamilyguidebook")
Run Code Online (Sandbox Code Playgroud)

r

2
推荐指数
1
解决办法
128
查看次数

给定R中的父子关系,如何展平分层数据结构

我有描述父子关系的数据:

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)

在视觉上,这看起来像: …

r data-manipulation hierarchical hierarchical-data tidyr

2
推荐指数
1
解决办法
58
查看次数

如何在 R 中打印函数名称及其底层源代码

假设我有一个定义如下的函数:

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)

如果无法打印定义该函数的代码,是否有办法catprint以这样的方式可以在正常结果前myFun加上文本“myFun <-”?

r

2
推荐指数
1
解决办法
44
查看次数

使用预测包从预测中获取预测点估计和间隔

我需要一种方法来打印预测值.

我需要打印深蓝色线值,如果可能的话,还需要打印下图中灰色区域的值.

打印该值或打印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)

情节

plot r prediction forecasting

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

使用clickAction = NULL将networkD3中的节点链接到网站

有没有办法使用节点为纽带,以使用功能的外部网站的方式forceNetwork()networkD3r?我在想也许修改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]

r networkd3 htmlwidgets

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

重复3位字符序列的行值 - 整洁的数据

给出以下数据框:

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)

我想我可以利用一些东西 …

r dplyr tidyr data-munging

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

在r中使用rvest在多个网页上刮表

我是网络抓取的新手,我正试图在多个网页上刮取表格.这是网站: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是因为它是我开始学习的那个.

r web-scraping rvest

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

如何在 tidyverse 中的列表列上使用 pull 后设置名称

鉴于以下情况:

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 dplyr purrr tidyverse

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