我经常使用大型 3D 数组(纬度、经度和时间),例如 720x1440x480 的大小。通常,我需要对每个纬度和经度随时间进行操作,例如,获取平均值(导致 2D 数组)或及时获取滚动平均值(导致 3D 数组),或更复杂的函数。
我的问题是:哪种包装(或方式)最有效和最快?
我知道一个选项是基础 R,使用 apply 函数和滚动函数与提供 rollapply 函数的包 zoo 混合。另一种方式是使用 tidyverse,另一种方式是使用 data.table。以及这些包之间的组合。但有没有最快的?
例如,如果我有这个数据立方体:
data <- array(rnorm(721*1440*480),dim = c(721,1440,480))
Run Code Online (Sandbox Code Playgroud)
哪些维度是纬度、经度和时间,如下所示:
lat <- seq(from = -90, to = 90, by = 0.25)
lon <- seq(from = 0, to = 359.75, by = 0.25)
time <- seq(from = as.Date('1980-01-01'), by = 'month', length.out = 480)
Run Code Online (Sandbox Code Playgroud)
我通常需要做这样的事情(这是在基础 R + 动物园):
# Average in time
average_data <- apply(data, 1:2, mean)
# Rolling mean, width of window = …Run Code Online (Sandbox Code Playgroud) 是否可以进行格式化hoverlabel以使背景颜色透明并且可以通过标签看到绘图?
我可以通过例如将其设置为纯色,hoverlabel = list(bgcolor = '#fff')但看起来如果我尝试添加透明度,颜色字符串的该部分将被忽略。与 相同bgcolor = 'rgba(255,255,255,0.05)',也不起作用。看起来标记有opacity设置,但悬停标签则没有。
谢谢!!
当我[WARNING] Deprecated: --self-contained. use --embed-resources --standalone使用htmlwidgets::saveWidget(..., selfcontained = TRUE).
我不知道如何使用“嵌入资源”或“独立”作为函数中的参数。这只是最近开始的,想知道是否有其他人发生过此警告,或者知道为此使用的正确参数。更新了 RStudio 和软件包,但警告仍然存在。
使用 R 4.2.1、RStudio 2022.07.2+576“斑点 Wakerobin”、htmlwidgets1.5.4。
library(plotly)
library(htmlwidgets)
fig <- plot_ly(x = 1:10, y = 1:10, type = "scatter", mode = "lines")
htmlwidgets::saveWidget(partial_bundle(fig), file = "plotly.html", selfcontained = TRUE)
utils::browseURL("plotly.html")
Run Code Online (Sandbox Code Playgroud) 我是 r-plotly 新手,试图找出如何处理不在数据上的点击。似乎使用event_data("plotly_click")I 获取数据中点上的事件,但到目前为止还没有弄清楚如何针对不接近数据但仅在绘图的白色部分上的点击执行此操作。
图中的闪亮点击事件可以做到这一点,我只得到点击的 x 和 y。我想要类似的,但是为了情节。
我可以指定点击事件来自绘图上的任何位置,而不仅仅是数据上吗?
编辑:令人惊讶的是,这在情节中还不存在。查看此功能请求
https://github.com/plotly/plotly.js/issues/2696
https://github.com/ropensci/plotly/issues/1194
因此,在添加此功能之前,我想我的问题是有哪些选项可以做到这一点?看起来像是一个基本功能,我希望对 JavaScript/Shiny/Plotly 有更多了解的人能够破解它。
我想创建一个传单地图,您可以在其中选择多个多边形,这将selectizeInput()在一个闪亮的应用程序中更新。这将包括删除选定的多边形(当它在selectizeInput().
我稍微更改/更新了此处答案中的代码(使用 sf 而不是 sp 和更多 dplyr,我可以在其中计算出基本 R 是什么)。
多边形可能可以通过observeEvent绑定来更新input$clicked_locations,但不确定具体如何。
这是代码:
library(shiny)
library(leaflet)
library(sf)
library(dplyr)
#load shapefile
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
st_transform(4326)
shinyApp(
ui = fluidPage(
"Update selectize input by clicking on the map",
leafletOutput("map"),
"I would like the selectize input to update to show all the locations clicked,",
"but also when items are removed here, they are removed on the map too, so linked to the map.",
selectizeInput(inputId …Run Code Online (Sandbox Code Playgroud) 我正在使用 R、ggplot 和 plotly 构建一个包含转推的堆叠条形图。如果单击条形图的一部分,我希望打开一个新的浏览器选项卡,并显示该特定日期的推文以及指定的转推量。但是,当我单击下面示例中的其中一个条时,会打开一个不同的链接,表明 url 未与这些条正确连接。我该如何解决这个问题?
我以前从未工作过,甚至从未见过 JavaScript,所以答案很可能就在那里。该图最终将出现在 Shiny 应用程序中。
library(rtweet)
library(ggplot2)
library(plotly)
# Get tweets
tweets <- get_timeline("BBC", n = 10)
# Create dataframe
data <- data.frame("retweet_count" = tweets$retweet_count,
"week" = c(1,1,1,2,2,3,4,5,5,6),
"url" = tweets$status_url)
# Create ggplot
ggplot(data = data,
aes(x = week,
y = retweet_count,
label = url)) +
geom_bar(stat = 'sum',
fill = "darkblue")
# Convert to plotly
p <- ggplotly(
p,
tooltip = c("y", 'label'))
# Add URL data to plot
p$x$data[[1]]$customdata <- data$url …Run Code Online (Sandbox Code Playgroud) 我是一个新手程序员,如果我不清楚或遗漏相关信息,请原谅。我编写了一个闪亮的应用程序,它从另一组代码导入数据帧。我想使用应用程序中的用户输入更新该数据框。我已经使用下面的代码将数据帧作为反应变量上传:
数据
current.shiny <- data.frame("Task" = as.character(c("Task 1", "Task 2", "Task 3")), "Completed" = as.character(c("Yes", "NO", "Yes")),"Date.Completed" = as.Date(c("2020-10-19","2020-10-20", "2020-10-21")))
Run Code Online (Sandbox Code Playgroud)
用户界面
ui<- fluidPage(
# Application title
titlePanel("Week of 11.02.2020"),
# Sidebar with reactive inputs
sidebarLayout(
sidebarPanel(
selectInput(inputId = "task.choice", label = "Task",
choices = c(as.list(current.shiny$Task))),
selectInput(inputId = "completed", label = "Completed?",
choices = c("Yes" = "Yes", "No" = "No")),
dateInput(inputId = "date.completed", label ="Date Completed"),
actionButton("update","Update Sheet")
),
# a table of reactive outputs
mainPanel(
mainPanel(
#DT::dataTableOutput("dt_table", width = 500) …Run Code Online (Sandbox Code Playgroud) 考虑以下 reprex 生成的图。请注意,ggplot 具有合理的图例,而在plotly 中,图例大量重复,每次相同类别(“制造商”)出现在每个方面时都会有一个条目。如何使情节图例更好地匹配 ggplot2 的图例?
library(plotly)
library(ggplot2)
p <- mpg %>%
ggplot(aes(year)) +
geom_ribbon(aes(ymin=cty, ymax=hwy, fill = manufacturer), alpha=0.2) +
geom_line(aes(y = hwy, col=manufacturer)) +
facet_wrap(~class)
p
Run Code Online (Sandbox Code Playgroud)
plotly::ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
我有一个data.frame包含 1100 万行的医院数据。
Columns: ID (chr), outcome (1|0), 20x ICD-10 codes (chr).
Rows: 10.6 million
Run Code Online (Sandbox Code Playgroud)
我希望使数据整洁,以便将诊断代码建模为二进制结果。
我通常会使用pivot_longer或 Base R函数,但由于内存(32GB RAM,运行最新 R x64 的 Windows 服务器),aggregate结果很大,而且我的机器很挣扎。data.frame
我将拆分data.frame和 ,pivot_longer并手动添加列以允许data.frame在之后进行绑定,或者单独对每个拆分进行建模data.frame。
有没有一种方法可以用来减少数据大小或实现我所缺少的类似目标?
我不太明白为什么当我将由 制作的图转换ggplot为plotly使用时图例消失了ggplotly。情节帮助页面没有任何信息。我认为他们的示例在该页面上甚至无法正常工作。任何帮助是极大的赞赏!
library(scales)
packageVersion("ggplot2")
#> [1] '3.4.0'
library(plotly)
packageVersion("plotly")
#> [1] '4.10.1'
data <- data.frame(
stringsAsFactors = FALSE,
Level = c("Fast","Fast","Fast","Fast",
"Fast","Fast","Slow","Slow","Slow",
"Slow","Slow","Slow"),
Period = c("1Year","3Month","1Year","3Month",
"1Year","3Month","1Year","3Month",
"1Year","3Month","1Year","3Month"),
X = c(0.002,0.002,0.1,0.1,0.9,0.9,
0.002,0.002,0.1,0.1,0.9,0.9),
Y = c(1.38,1.29,1.61,1.61,1.74,0.98,
1.14,0.97,1.09,1.1,0.94,0.58)
)
Run Code Online (Sandbox Code Playgroud)
plt <- ggplot(data = data,
aes(x = X,
y = Y,
shape = Period,
color = Level)) +
geom_point(alpha = 0.6, size = 3) +
labs(x = " ",
y = "Value") + …Run Code Online (Sandbox Code Playgroud) r ×10
plotly ×6
r-plotly ×4
ggplot2 ×3
shiny ×3
javascript ×2
tidyverse ×2
arrays ×1
data.table ×1
html ×1
htmlwidgets ×1
large-data ×1
leaflet ×1
legend ×1
r-leaflet ×1
r-sf ×1
reactive ×1
tidyr ×1