R是否为Plotly折线图选择迹线? [英] R Shiny to select traces for Plotly line chart?

查看:87
本文介绍了R是否为Plotly折线图选择迹线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Shiny选择要在使用Plotly渲染的多线图中绘制的变量.我有很多变量,因此我想使用Shiny进行选择,而不是使用Plotly的交互式图例"click"选择机制. 示例数据:

I am trying to use Shiny to select variables I want to plot in a multi-line chart rendered using Plotly. I have many variables so I want to select using Shiny instead of using Plotly's interactive legend "click" selection mechanism. Example Data:

library(plotly)                       
# Example dataframe
foo <-data.frame( mon  = c("Jan", "Feb", "Mar"),
                  var_1 = c(100, 200, 300),
                  var_b = c(80, 250, 280),
                  var_three = c(150, 120,201)
                )

直接使用Plotly时,我可以使用如下代码手动添加跟踪:

When using Plotly directly I can manually add traces using code like this:

p <- plot_ly(x = foo$mon, y = foo$var_1, line = list(shape="linear"))
p <- add_trace(p, x = foo$mon, y = foo$var_b)
p <- add_trace(p, x = foo$mon, y = foo$var_three)
print(p)

现在,我想使用闪亮"复选框来选择希望在绘图上看到的变量.选择内容在input $ show_vars中捕获,但是如何遍历并绘制此变化的变量列表?这是我的app.R代码,可手动绘制其中一个变量.建议表示赞赏!

Now I want to use a Shiny checkbox to select the variables I wish to see on the plot. The selection is captured in input$show_vars , but how do I loop through and plot this changing list of variables? Here is my app.R code that manually plots one of the variables. Suggestions appreciated!

#------------------------------------------------------------------------------
# UI 
#------------------------------------------------------------------------------
ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            checkboxGroupInput('show_vars', 'Columns in the dataset', names(foo),
                               selected = c('mon', 'var_1')),
            helpText('Select the variables to show in the graph.')
        ),
        mainPanel(
            plotlyOutput("myPlot")
        )
    )
)
#------------------------------------------------------------------------------
# SERVER 
# Need to loop through input$show_vars to show a trace for each one?
#------------------------------------------------------------------------------
server <- function(input, output) {
    # a large table, reative to input$show_vars
    output$uteTable = renderDataTable({
        library(ggplot2)
        ute[, input$show_vars, drop = FALSE]
    })

    output$myPlot = renderPlotly({
        plot_ly(x=foo$mon, y=foo$var_1, line = list(shape="linear"))
        ## How to add the other traces selected in input$show_vars??
    })
}
shinyApp(ui = ui, server = server)

更新:我现在意识到我需要脚本来避免硬编码使用foo $ var_1的第一个图.该图应使用复选框中的任何一种可能的选择(减去$ mon,我已从选择列表中将其删除).当我尝试使第一个绘图语句成为条件时,我收到消息错误:最后一个绘图不存在".即,这不起作用:

UPDATE: I realize now that I need the script to avoid hard-coding the first plot to use foo$var_1. The plot should use any one of the possible selections in the checkboxes (minus $mon, which I have removed from the select list). When I try to make the first plot statement conditional I get the message "Error: The last plot does not exist." ie, this does not work:

output$myPlot = renderPlotly({
    # p <- plot_ly(x=foo$mon, y=foo$var_1, line = list(shape="linear"))
    for (item in input$show_vars) {
        if (item == 1){
            p <- plot_ly(x=foo$mon, y=foo[[item]], line = list(shape="linear"))
        }
        if(item > 1){
           p <- add_trace(p, x = foo$mon, y = foo[[item]], evaluate = TRUE)
        }
    }
    print(p)

推荐答案

查看这是否是您想要的.另外,您可能希望删除checkboxGroup中的前两个项目,以使它们不可移动(取决于您要的内容).

See if this is what you want. Also you probably want to remove the first two items in the checkboxGroup so that they are not removable (depending on what you want).

output$myPlot = renderPlotly({
    p <- plot_ly(x=foo$mon, y=foo$var_1, line = list(shape="linear"))
    ## How to add the other traces selected in input$show_vars??
    for (item in input$show_vars) {
        p <- add_trace(p, x = foo$mon, y = foo[[item]], evaluate = TRUE)
    }
    print(p)
})

这篇关于R是否为Plotly折线图选择迹线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆