#r #shiny #plotly #shinydashboard #shiny-reactivity
Вопрос:
Я работал над графиком фильтрации в shiny и хочу включить графические графики. Однако всякий раз, когда я пытаюсь построить график с помощью 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)
Как я изменил, чтобы попытаться использовать сюжетно…
output$correlation_plot <- renderPlotly({
plot1 <- plot_ly(data=df_1(),
x = ~Petal.Length,
y = ~Petal.Width,
type = 'scatter',
mode = 'markers'
)
Ответ №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)