【问题标题】:Combining renderUI, dataTableOutput, renderDataTable, and reactive结合renderUI、dataTableOutput、renderDataTable和reactive
【发布时间】:2020-04-29 02:41:21
【问题描述】:

这在某种程度上是this post的扩展:

我想在renderUI 中使用DT::renderDataTable,然后在reactive 中使用renderUIoutput

这就是我正在做的:

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(shiny))


#data.frames to be used in the server
set.seed(1)
coordinate.df <- data.frame(coordinate_id = paste0("c", 1:1000),x = rnorm(1000), y = rnorm(1000), stringsAsFactors = F)
feature.df <- data.frame(coordinate_id = rep(paste0("c", 1:1000), 10), feature_id = rep(paste0("f", 1:10), 1000), value = rnorm(10*1000), stringsAsFactors = F)
feature.rank.df <- feature.df %>% dplyr::select(feature_id) %>% unique() %>% dplyr::mutate(rank=sample(1:10,10,replace = F)) %>% dplyr::arrange(rank)

feature.color.vec <- c("lightgray","darkred")

server <- function(input, output)
{

  output$feature.idx <- renderUI({
    output$feature.table <- DT::renderDataTable(feature.rank.df, server = FALSE, selection = "single")
    DT::dataTableOutput("feature.table")
  })

  feature.plot <- reactive({
    if(!is.null(input$feature.idx)){
      feature.id <- feature.rank.df$feature_id[input$feature.idx]
      plot.title <- feature.id
      plot.df <- suppressWarnings(feature.df %>%
                                    dplyr::filter(feature_id == feature.id) %>%
                                    dplyr::left_join(coordinate.df,by = c("coordinate_id"="coordinate_id")))
      feature.plot <- suppressWarnings(plotly::plot_ly(marker=list(size=3),type='scatter',mode="markers",color=plot.df$value,x=plot.df$x,y=plot.df$y,showlegend=F,colors=colorRamp(feature.color.vec)) %>%
                                        plotly::layout(title=plot.title,xaxis=list(zeroline=F,showticklabels=F,showgrid=F),yaxis=list(zeroline=F,showticklabels=F,showgrid=F)) %>%
                                        plotly::colorbar(limits=c(min(plot.df$value,na.rm=T),max(plot.df$value,na.rm=T)),len=0.4,title="Value"))
    }
    feature.plot
  })

  output$outPlot <- plotly::renderPlotly({
    feature.plot()
  })
}


ui <- fluidPage(
  titlePanel("Results Explorer"),
  sidebarLayout(
    sidebarPanel(
      uiOutput("feature.idx")
    ),

    mainPanel(
      plotly::plotlyOutput("outPlot")
    )
  )
)

shinyApp(ui = ui, server = server)

它确实加载了feature.rank.dfdata.frame,但随后会将此错误消息打印到主面板:

Error: no applicable method for 'plotly_build' applied to an object of class "c('reactiveExpr', 'reactive')"

在侧面板的表格中选择行时不会绘制任何内容。

知道解决方案是什么吗?

【问题讨论】:

    标签: r datatable shiny plotly


    【解决方案1】:

    您可以通过以下代码替换您的服务器功能来解决此问题。

    • 通过input$feature.table_rows_selected引用所选功能
    • 将响应式 feature.plot 代码保留在 renderPlotly 函数中
    server <- function(input, output)
    {
        output$feature.idx <- renderUI({
            output$feature.table <-
                DT::renderDataTable(feature.rank.df,
                                    server = FALSE,
                                    selection = "single")
            DT::dataTableOutput("feature.table")
        })
    
        output$outPlot <- plotly::renderPlotly({
            if (!is.null(input$feature.table_rows_selected)) {
                feature.id <-
                    feature.rank.df$feature_id[input$feature.table_rows_selected]
                plot.title <- feature.id
                plot.df <- suppressWarnings(
                    feature.df %>%
                        dplyr::filter(feature_id == feature.id) %>%
                        dplyr::left_join(
                            coordinate.df,
                            by = c("coordinate_id" = "coordinate_id")
                        )
                )
                feature.plot <-
                    suppressWarnings(
                        plotly::plot_ly(
                            marker = list(size = 3),
                            type = 'scatter',
                            mode = "markers",
                            color = plot.df$value,
                            x = plot.df$x,
                            y = plot.df$y,
                            showlegend = F,
                            colors = colorRamp(feature.color.vec)
                        ) %>%
                            plotly::layout(
                                title = plot.title,
                                xaxis = list(
                                    zeroline = F,
                                    showticklabels = F,
                                    showgrid = F
                                ),
                                yaxis = list(
                                    zeroline = F,
                                    showticklabels = F,
                                    showgrid = F
                                )
                            ) %>%
                            plotly::colorbar(
                                limits = c(
                                    min(plot.df$value, na.rm = T),
                                    max(plot.df$value, na.rm = T)
                                ),
                                len = 0.4,
                                title = "Value"
                            )
                    )
                feature.plot
            }
    
        })
    }
    

    编辑:

    或者,您可以将 feature.plot 保留为响应式,如下所示:

    server <- function(input, output)
    {
    
        output$feature.idx <- renderUI({
            output$feature.table <- DT::renderDataTable(feature.rank.df, server = FALSE, selection = "single")
            DT::dataTableOutput("feature.table")
        })
    
        feature.plot <- reactive({
            if (!is.null(input$feature.table_rows_selected)) {
                feature.id <-
                    feature.rank.df$feature_id[input$feature.table_rows_selected]
                plot.df <- suppressWarnings(
                    feature.df %>%
                        dplyr::filter(feature_id == feature.id) %>%
                        dplyr::left_join(coordinate.df, by = c("coordinate_id" =
                                                                   "coordinate_id"))
                )
                feature.plot <-
                    suppressWarnings(
                        plotly::plot_ly(
                            marker = list(size = 3),
                            type = 'scatter',
                            mode = "markers",
                            color = plot.df$value,
                            x = plot.df$x,
                            y = plot.df$y,
                            showlegend = F,
                            colors = colorRamp(feature.color.vec)
                        ) %>%
                            plotly::layout(
                                title = plot.df$feature_id[1],
                                xaxis = list(
                                    zeroline = F,
                                    showticklabels = F,
                                    showgrid = F
                                ),
                                yaxis = list(
                                    zeroline = F,
                                    showticklabels = F,
                                    showgrid = F
                                )
                            ) %>%
                            plotly::colorbar(
                                limits = c(
                                    min(plot.df$value, na.rm = T),
                                    max(plot.df$value, na.rm = T)
                                ),
                                len = 0.4,
                                title = "Value"
                            )
                    )
            }
            return(feature.plot)
        })
    
        output$outPlot <- plotly::renderPlotly({
            req(feature.plot(), input$feature.table_rows_selected)
            feature.plot()
        })
    }
    

    【讨论】:

    • 非常感谢。知道为什么将绘图部分包裹在 reactive 语句中会失败吗?
    • 可以将其保留在响应式语句中,请参阅我的编辑。您可能必须以不同的方式传递情节标题(或者像我一样,或作为属性),并添加另一个 req 检查,但它也有效。
    猜你喜欢
    • 2020-08-01
    • 2020-08-13
    • 2018-07-04
    • 1970-01-01
    • 2015-01-19
    • 2020-12-15
    • 2015-08-28
    • 2021-08-16
    • 2018-08-11
    相关资源
    最近更新 更多