【问题标题】:ShinyDashboard cannot create dynamic plotShinyDashboard 无法创建动态绘图
【发布时间】:2023-03-29 09:56:02
【问题描述】:

我正在尝试为 mtcars 数据集的每个 X 变量相对于 y 创建一个动态图。 基本上,我的 y 变量是“vs”列,其余的是我的 X 变量 但是,当我尝试运行该应用程序时,出现以下错误。有什么想法吗?

Warning: Error in xy.coords: 'x' and 'y' lengths differ

library(shiny)
library(shinydashboard)

data("mtcars")

y <- mtcars$vs
X <- mtcars[ , !(names(mtcars) %in% "vs")]


header_app <- dashboardHeader()

sidebar_app <- dashboardSidebar()

body_app <- dashboardBody(

  fluidRow(

        box(

          selectInput("filter", "Correlation", choices = names(X)),
          plotOutput("plot1", height = "250")

            ) #box
          ) #Fluid Row


)





ui <- dashboardPage(header = header_app, sidebar = sidebar_app, body = body_app)




server <- function(input,output){



  output$plot1 <- renderPlot({plot(input$filter,y)})




}


shinyApp(ui,server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    您必须从mtcars 中提取选定的列:

    library(shiny)
    library(shinydashboard)
    
    data("mtcars")
    
    y <- mtcars$vs
    X <- mtcars[ , !(names(mtcars) %in% "vs")]
    
    
    header_app <- dashboardHeader()
    
    sidebar_app <- dashboardSidebar()
    
    body_app <- dashboardBody(
      
      fluidRow(
        
        box(
          
          selectInput("filter", "Correlation", choices = names(X)),
          plotOutput("plot1", height = "250")
          
        ) #box
      ) #Fluid Row
      
      
    )
    
    
    
    
    
    ui <- dashboardPage(header = header_app, sidebar = sidebar_app, body = body_app)
    
    
    
    
    server <- function(input,output){
      
    
      output$plot1 <- renderPlot({plot(mtcars[, input$filter],y)})
      
      
      
      
    }
    
    
    shinyApp(ui,server)
    

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 2012-03-13
      • 2020-08-08
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      相关资源
      最近更新 更多