简体   繁体   English

R Shiny:散点图未显示

[英]R Shiny: scatter plot not showing

I am trying to render a plot_ly scatter plot based on a dynamic slidebar.我正在尝试基于动态滑动条渲染 plot_ly 散点图。 However, it seems like I am missing something.但是,我似乎错过了一些东西。

When I run the app, the error I see is the following:当我运行该应用程序时,我看到的错误如下:

Error: invalid first argument错误:第一个参数无效

I would really appreciate your help in understanding what is going wrong.我真的很感谢你帮助理解出了什么问题。

Please find below the code (hope it's reproducible enough now).请在代码下方找到(希望它现在具有足够的可重现性)。

#load libraries to be used. Pacman ensures library reproducibility 
if (!require("pacman")) install.package("pacman")
pacman::p_load(ggplot2,dplyr,tidyr, rpart, shiny, shinydashboard, plotly, DT)


#load Titanic dataset 
dataset<-read.csv(".../titanic.csv")


ui <- fluidPage(
  dashboardHeader(
    title = "My trial",
    titleWidth = 300
  ),

  dashboardSidebar(
    sidebarMenu(
      sidebarMenu(
        menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
        menuItem("Data Table", tabName = "datatable", icon = icon("th"))
      )

    )
  ),

  dashboardBody(
    tabItems(
      # Content of first tab
      tabItem(tabName = "visualization",
              h2("Visualization using Plot_ly"),
              fluidRow(
                box(plotlyOutput("Trialplot")),
                box(
                  title = "Age range",
                  status="primary",
                  solidHeader=TRUE,
                  collapsible=TRUE,
                  sliderInput("slider", "Please select the age range to be seen:", 0, max(dataset$Age), c(0,max(dataset$Age)/2), ticks=FALSE)
                )
              ), 
              box(plotlyOutput("Trialplot"))

      ),

      # Content of second tab
      tabItem(tabName = "datatable",
              h2("Data table")
      )
    )
  )
)


server = shinyServer(function(input, output,session) { 



  output$Trialplot<-renderPlotly({
    dataset_filtered<-dataset[dataset$Age ==input$slider,]
    plot_ly(data = dataset_filtered, x = ~Fare, y = ~get(input$slider) , 
                                  color=~Survived, 
                                  colors=pal, 
                                  type='scatter',
                                  marker=list(size=10),
                                  hoverinfo='text',
                                  text=~paste("Name:",Name,", Age:",Age,", Fare",Fare)
                                  )%>%
                                  layout(title="Age vs.Fare")})

}
)

shinyApp(ui,server)

Thanks in advance.提前致谢。

You should not call plotlyOutput("Trialplot") twice in you shiny app, also, sliderInput has two values which is min and max of your slider, so you can not use such dataset$Age == input$slider :你不应该在你闪亮的应用程序中调用plotlyOutput("Trialplot")两次,而且, sliderInput有两个值,分别是你的滑块的最小值和最大值,所以你不能使用这样的dataset$Age == input$slider

I tried to reproduce your code, now it works for me, next time try to post your data with output of dput(data) function.我试图重现您的代码,现在它对我dput(data) ,下次尝试使用dput(data)函数的输出发布您的数据。

library(shiny)
pacman::p_load(ggplot2,dplyr,tidyr, rpart, shiny, shinydashboard, plotly, DT)


#load Titanic dataset 
dataset<-read.csv("titanic.csv")


ui <- fluidPage(
  dashboardHeader(
    title = "My trial",
    titleWidth = 300
  ),

  dashboardSidebar(
    sidebarMenu(
      sidebarMenu(
        menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
        menuItem("Data Table", tabName = "datatable", icon = icon("th"))
      )

    )
  ),

  dashboardBody(
    tabItems(
      # Content of first tab
      tabItem(tabName = "visualization",
              h2("Visualization using Plot_ly"),
              fluidRow(
                # box(plotlyOutput("Trialplot")),
                box(
                  title = "Age range",
                  status="primary",
                  solidHeader=TRUE,
                  collapsible=TRUE,
                  sliderInput("slider", "Please select the age range to be seen:", 0, max(dataset$Age,na.rm = T), c(0,max(dataset$Age,na.rm = T)/2), ticks=FALSE)
                )
              ), 
              box(plotlyOutput("Trialplot"))

      ),

      # Content of second tab
      tabItem(tabName = "datatable",
              h2("Data table")
      )
    )
  )
)


server = shinyServer(function(input, output,session) { 
  observe({
    print(input$slider)
  })
  output$Trialplot<-renderPlotly({
    dataset_filtered<-dataset[dataset$Age >= input$slider[1] & dataset$Age <= input$slider[2],]
    plot_ly(data = dataset_filtered, x = ~Fare, y = ~Age , 
            color=~Survived, 
            colors=NULL, 
            type='scatter',
            marker=list(size=10),
            hoverinfo='text',
            text=~paste("Name:",Name,", Age:",Age,", Fare",Fare)
    )%>%
      layout(title="Age vs.Fare")
    })

}
)

shinyApp(ui,server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM