【问题标题】:Select values in two columns based on two inputs respectively in Shiny, with plotly在 Shiny 中分别根据两个输入选择两列中的值,用 plotly
【发布时间】:2021-09-13 01:48:03
【问题描述】:

我一直在研究闪亮的过滤图,并希望合并 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'
                         )

【问题讨论】:

    标签: r shiny plotly shinydashboard shiny-reactivity


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      相关资源
      最近更新 更多