简体   繁体   中英

R Shiny: scatter plot not showing

I am trying to render a plot_ly scatter plot based on a dynamic slidebar. 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 :

I tried to reproduce your code, now it works for me, next time try to post your data with output of dput(data) function.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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