这是我使用plotly_click事件的基本闪亮应用程序的代码,可选择显示另一个图.我希望那个侧框图在模式弹出窗口中呈现,而不是在页面内侧面.
library(shiny)
library(plotly)
df1 <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
y = c(rnorm(10), rnorm(10, 3, 1)))
ui <- fluidPage(
column(6, plotlyOutput('scatter')),
column(6, plotlyOutput('box'))
)
server <- function(input, output) {
output$scatter <- renderPlotly({
plot_ly(df1, x = x, y = y, mode = 'markers', source = 'scatter')
})
output$box <- renderPlotly({
eventdata <- event_data('plotly_click', source = 'scatter')
validate(need(!is.null(eventdata),
'Hover over the scatter plot to populate this boxplot'))
plot_ly(df2, x = x, …Run Code Online (Sandbox Code Playgroud) 我有一堆来自同一主题的研究的“配对”观察结果,我正在尝试构建一个意大利面条图来可视化这些观察结果,如下所示:
library(plotly)
df <- data.frame(id = rep(1:10, 2),
type = c(rep('a', 10), rep('b', 10)),
state = rep(c(0, 1), 10),
values = c(rnorm(10, 2, 0.5), rnorm(10, -2, 0.5)))
df <- df[order(df$id), ]
plot_ly(df, x = type, y = values, group = id, type = 'line') %>%
layout(showlegend = FALSE)
Run Code Online (Sandbox Code Playgroud)
它产生了我正在寻找的正确情节。但是,代码以自己的颜色显示了每个分组的行,这真的很烦人,让人分心。我似乎无法找到摆脱颜色的方法。
额外问题:我实际上想使用color = state该变量并实际为斜线着色。
任何方法/想法?
这应该非常简单(我认为),但由于某种原因我无法让它工作。我有这样的数据:
df
x y
1 2016-08-12 1
2 2016-08-13 2
3 2016-08-14 3
4 2016-08-15 4
5 2016-08-16 5
str(df)
'data.frame': 5 obs. of 2 variables:
$ x: Date, format: "2016-08-12" "2016-08-13" ...
$ y: int 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
我按如下方式使用绘图,但它不会将 xaxis 范围调整为我指定的自定义值。相反,使用数据的范围:
library(plotly)
plot_ly(df, x = x, y = y) %>% layout(xaxis = list(range = c(as.Date('2016-08-01'), as.Date('2016-08-31'))))
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我有这样的数字数据:
df
x y
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
Run Code Online (Sandbox Code Playgroud)
并指定一个像这样的自定义范围,它工作得很好:
plot_ly(df, …Run Code Online (Sandbox Code Playgroud) 我只是想在启用ScrollX的情况下向右滚动时保持最左边的列固定,而无法使其正常工作。知道我需要做些什么吗?
library(shiny)
library(DT)
ui <- fluidPage(
fluidRow(mainPanel(DT::dataTableOutput('mtcars'), width = 12))
)
server <- server <- function(input, output, session) {
output$mtcars <- DT::renderDataTable({
mtcars %>%
DT::datatable(
selection = 'none', rownames = '', filter = 'none',
options = list(
paging = TRUE, searching = TRUE, info = FALSE,
sort = TRUE, scrollX = TRUE, fixedColumns = list(leftColumns = 1)
)
)
})
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
此处的版本信息:
> packageVersion('DT')
[1] ‘0.4.16’
> packageVersion('shiny')
[1] ‘1.1.0’
> version
_
platform …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数据框:
dput(df1)
structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), y = c(16449L, NA, NA,
16449L, 16450L, 16451L, NA, NA, 16455L, 16456L, NA, NA, 16756L,
NA, 16460L, 16464L, 16469L, NA, NA, 16469L)), .Names = c("x",
"y"), row.names = c(NA, -20L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
我需要y按如下方式改变列(使用dplyr):
df1 <- mutate(df1, y = ifelse(is.na(y), cummax(y), y))
Run Code Online (Sandbox Code Playgroud)
但是,cummax不适合我的情况处理 NA。如何通过其他方法获得相同的效果?
结果输出应将 NA 行y填充为 的最后一个非 …
所以,我有一个这样的列表,并希望转换为一个向量,其中空列表被NA替换.列表中的所有条目最多只有一个元素(感谢MongoDB,它只返回嵌套元素作为列表).
有没有更有效的方法来做这个而不是循环(申请家庭)?
dput(l)
list(structure(list(), .Names = character(0)), structure(list(
postcode = "27612"), .Names = "postcode"), structure(list(
postcode = "30127"), .Names = "postcode"), structure(list(
postcode = "35173"), .Names = "postcode"), structure(list(
postcode = "30047"), .Names = "postcode"), structure(list(
postcode = "87571"), .Names = "postcode"))
sapply(l, function(x) if (length(x)) unlist(x$postcode) else NA)
[1] NA "27612" "30127" "35173" "30047" "87571"
Run Code Online (Sandbox Code Playgroud)
输出正是我想要的,但是对于非常大的数据集的恐惧,这将是缓慢的.希望有更快的方法.