首页 > 解决方案 > 在 Shiny 中分别根据两个输入选择两列中的值,用 plotly

问题描述

我一直在研究闪亮的过滤图,并希望合并 Plotly 图。但是,每当我尝试使用 Plotly 进行绘图时,我都会得到一个空白图表。下面是我的尝试(没有 Plotly)我如何尝试集成 Plotly。

工作(不是情节)

library(shinydashboard)
library(tidyverse)
library(plotly)
library(shiny)

#______________________________________________________________________________#
server <- function(input, output, session) { 
    df <- reactive({
        subset(iris, Petal.Width == input$Petalw)
    })
    
    # Extract list of Petal Lengths from selected data - to be used as a filter
    p.lengths <- reactive({
        unique(df()$Petal.Length)
    })
    
    # Filter based on Petal Length
    output$PetalL <- renderUI({
        selectInput("PetalLengthSelector", "PetalLength", as.list(p.lengths()))
    })
    
    # Subset this data based on the values selected by user
    df_1 <- reactive({
        foo <- subset(df(), Petal.Length == input$PetalLengthSelector)
        return(foo)
    })
    
    output$table <- DT::renderDataTable(
        DT::datatable(df_1(), options = list(searching = FALSE,pageLength = 25))
    )
    
    output$correlation_plot <- renderPlot({
        plot(df_1()$Petal.Length, df_1()$Petal.Width,
             xlab = "Length", ylab = "Width")
    })
}

#______________________________________________________________________________#
ui <- navbarPage(
    title = 'Select values in two columns based on two inputs respectively',
    
    fluidRow(
        column(width = 12,
               plotOutput('correlation_plot')
        )
    ),
    
    
    fluidRow(
        column(width = 3,
               selectInput("Petalw","PetalWidth", choices = unique(iris$Petal.Width),multiple = T),
               uiOutput("PetalL")
        ),
        column(9,
               tabPanel('Table', DT::dataTableOutput('table'))
        )
    )
)
shinyApp(ui, server)

我如何修改以尝试使用 Plotly ......

 output$correlation_plot <- renderPlotly({
        plot1 <- plot_ly(data=df_1(),
                         x = ~Petal.Length,
                         y = ~Petal.Width,
                         type = 'scatter',
                         mode = 'markers'
                         )

标签: rshinyplotlyshinydashboardshiny-reactivity

解决方案


renderPlotly()plotlyOutput().

library(shinydashboard)
library(tidyverse)
library(plotly)
library(shiny)
library(DT)

#______________________________________________________________________________#
server <- function(input, output, session) { 
  df <- reactive({
    subset(iris, Petal.Width == input$Petalw)
  })
  
  # Extract list of Petal Lengths from selected data - to be used as a filter
  p.lengths <- reactive({
    unique(df()$Petal.Length)
  })
  
  # Filter based on Petal Length
  output$PetalL <- renderUI({
    selectInput("PetalLengthSelector", "PetalLength", as.list(p.lengths()))
  })
  
  # Subset this data based on the values selected by user
  df_1 <- reactive({
    foo <- subset(df(), Petal.Length == input$PetalLengthSelector)
    return(foo)
  })
  
  output$table <- DT::renderDataTable(
    DT::datatable(df_1(), options = list(searching = FALSE,pageLength = 25))
  )
  
  output$correlation_plot <- renderPlotly({
    plot1 <- plot_ly(data=df_1(),
                     x = ~Petal.Length,
                     y = ~Petal.Width,
                     type = 'scatter',
                     mode = 'markers'
    )
})
  
}

#______________________________________________________________________________#
ui <- navbarPage(
  title = 'Select values in two columns based on two inputs respectively',
  
  fluidRow(
    column(width = 12,
           plotlyOutput('correlation_plot')
    )
  ),
  
  
  fluidRow(
    column(width = 3,
           selectInput("Petalw","PetalWidth", choices = unique(iris$Petal.Width),multiple = T),
           uiOutput("PetalL")
    ),
    column(9,
           tabPanel('Table', DT::dataTableOutput('table'))
    )
  )
)
shinyApp(ui, server)

推荐阅读