繁体   English   中英

R Shiny:散点图未显示

[英]R Shiny: scatter plot not showing

我正在尝试基于动态滑动条渲染 plot_ly 散点图。 但是,我似乎错过了一些东西。

当我运行该应用程序时,我看到的错误如下:

错误:第一个参数无效

我真的很感谢你帮助理解出了什么问题。

请在代码下方找到(希望它现在具有足够的可重现性)。

#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)

提前致谢。

你不应该在你闪亮的应用程序中调用plotlyOutput("Trialplot")两次,而且, sliderInput有两个值,分别是你的滑块的最小值和最大值,所以你不能使用这样的dataset$Age == input$slider

我试图重现您的代码,现在它对我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