1

Im trying to set the x and y axes of the plotly chart below by using shiny selectInput() but I get an empty plot instead.

## app.R ##
library(shiny)
library(shinydashboard)
library(plotly)
library(dplyr)


df<-structure(list(year = c("2016", "2016", "2016", "2016", "2016", 
                            "2016", "2016", "2016", "2017", "2017", "2017", "2017", "2017", 
                            "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018", 
                            "2018", "2018", "2018", "2018", "2018", "2019", "2019", "2019", 
                            "2019", "2019", "2019", "2019", "2020", "2020", "2020", "2020", 
                            "2020", "2020", "2020", "2020"), accident.description = c("left and center lane blocked", 
                                                                                      "one lane blocked", "right and center lane blocked", "right hand shoulder blocked", 
                                                                                      "right lane blocked", "shoulder blocked", "three lanes blocked", 
                                                                                      "two lanes blocked", "left and center lane blocked", "one lane blocked", 
                                                                                      "right and center lane blocked", "right hand shoulder blocked", 
                                                                                      "right lane blocked", "road closed", "shoulder blocked", "three lanes blocked", 
                                                                                      "two lanes blocked", "left and center lane blocked", "one lane blocked", 
                                                                                      "right and center lane blocked", "right hand shoulder blocked", 
                                                                                      "right lane blocked", "road closed", "shoulder blocked", "three lanes blocked", 
                                                                                      "two lanes blocked", "one lane blocked", "right and center lane blocked", 
                                                                                      "right hand shoulder blocked", "right lane blocked", "road closed", 
                                                                                      "shoulder blocked", "two lanes blocked", "left and center lane blocked", 
                                                                                      "one lane blocked", "right and center lane blocked", "right hand shoulder blocked", 
                                                                                      "right lane blocked", "road closed", "shoulder blocked", "two lanes blocked"
                            ), severity = c(8, 68, 5, 206, 142, 6, 2, 25, 32, 77, 30, 309, 
                                            285, 8, 2, 6, 89, 19, 58, 13, 224, 245, 8, 8, 2, 119, 77, 12, 
                                            34, 178, 28, 2, 61, 15, 42, 6, 34, 182, 32, 3, 122), traffic.sp = c(116, 
                                                                                                                670, 82, 2550, 1394, 82, 32, 235, 283, 651, 341, 3996, 3053, 
                                                                                                                0, 23, 71, 772, 239, 592, 174, 2967, 2748, 0, 128, 24, 1181, 
                                                                                                                990, 133, 505, 1972, 0, 46, 654, 113, 379, 74, 519, 1658, 0, 
                                                                                                                29, 1116), distance.m = c(0.02, 0.06, 0.01, 0.44, 0.26, 0.01, 
                                                                                                                                          0.01, 0.03, 0.05, 5.84, 0.05, 0.67, 9.36, 1.299, 0.01, 0, 1.12, 
                                                                                                                                          4.22, 2.875, 0, 3.37, 18.379, 1.306, 0, 0, 2.017, 12.281, 0, 
                                                                                                                                          0, 21.872, 3.168, 0, 12.392, 0, 2.349, 0, 0, 21.76, 3.149, 0, 
                                                                                                                                          6.625), traffic._1 = c(250, 1980, 168, 7618, 4193, 214, 85, 735, 
                                                                                                                                                                 873, 2238, 976, 11436, 9022, 162, 76, 166, 2518, 582, 1868, 428, 
                                                                                                                                                                 8380, 7939, 162, 313, 83, 3496, 2696, 337, 1420, 5910, 546, 80, 
                                                                                                                                                                 1776, 412, 1231, 174, 1426, 5313, 659, 80, 3180)), class = c("grouped_df", 
                                                                                                                                                                                                                              "tbl_df", "tbl", "data.frame"), row.names = c(NA, -41L), groups = structure(list(
                                                                                                                                                                                                                                year = c("2016", "2017", "2018", "2019", "2020"), .rows = structure(list(
                                                                                                                                                                                                                                  1:8, 9:17, 18:26, 27:33, 34:41), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                 "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                 ), row.names = c(NA, -5L), .drop = TRUE))
ui <- dashboardPage(title="ScopeDashboard",
                    dashboardHeader(title = span("Scope Dashboard")),
                    
                    dashboardSidebar(
                      selectInput(
                        inputId = "varsx", 
                        label = "Choose X variable:",
                        choices = c("severity","traffic.sp","distance.m","traffic._1"),
                        multiple = F,
                        selected = "severity"
                      ),
                           selectInput(
                             inputId = "varsy", 
                             label = "Choose Y variable:",
                             choices = c("severity","traffic.sp","distance.m","traffic._1"),
                             multiple = F,
                             selected = "traffic.sp"
                           )
                    ),
                    
                    dashboardBody(
                      plotlyOutput("corrs")
                      
                    )
)

server <- function(input, output) {
  output$corrs<-renderPlotly({
    
    fig <- plot_ly(
      data = as.data.frame(df),
      x = .data[[input$varsx]],
      y = .data[[input$varsy]],
      color = ~accident.description,
      mode = "scatter",
      marker = list(
        size = 10,
        line = list(color = 'rgba(152, 0, 0, .8)',
                    width = 2)
      ),
      text = paste(
        "Accident Description:",~accident.description,
        "<br>Year :",
        df$year,
        "<br> Count  :",
        .data[[input$varsx]],
        "<br> Count :",
        .data[[input$varsy]]
      ),
      hoverinfo = "text"
    ) %>% layout(height=470,showlegend=T,
                 title = paste(" per year"
                 ),font=t,
                 xaxis = list(
                   
                   dtick = 10,
                   tick0=0,
                   rangemode = "nonnegative"
                   
                 ),
                 yaxis = list(
                   dtick = 10,
                   tick0 = 0,
                   rangemode = "nonnegative"
                   
                 ),
                 legend = list(title = list(text = "<b>Accident Description</b>"))
    )
    
    fig
  })
}

shinyApp(ui, server)   
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
firmo23
  • 7,490
  • 2
  • 38
  • 114
  • 1
    Hi @firmo23. The `.data` pronoun will not work with `plotly`. plotly was invented before rlang. See https://stackoverflow.com/questions/55505599/combine-formula-and-tidy-eval-plotly. Easiest way to fix that would probably be to replace `.data` by `df` or convert to formula e.g. `x = as.formula(paste("~", input$varsx))`. Not sure whether this fixes your issue with the axes .. – stefan Jan 31 '22 at 18:44
  • u re right thanks – firmo23 Jan 31 '22 at 19:56
  • In R plotly the tilde `~` is used to pass a variable of the `data.frame` provided to the data parameter of `plot_ly` - there is no need to repeat `df`. Please see my post below. – ismirsehregal Feb 02 '22 at 10:50

1 Answers1

1

In R plotly the tilde ~ is used to pass a variable of the data.frame provided to the data parameter of plot_ly (in R the tilde marks a variable as a formula) we can use e.g. x = ~ get(input$varsx) to access the variable programmatically:

library(shiny)
library(shinydashboard)
library(plotly)

DF <- structure(list(year = c("2016", "2016", "2016", "2016", "2016", "2016",
"2016", "2016", "2017", "2017", "2017", "2017", "2017", "2017", "2017",
"2017", "2017", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2019", "2019", "2019", "2019", "2019", "2019", "2019",
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020"),
accident.description = c("left and center lane blocked", "one lane blocked",
"right and center lane blocked", "right hand shoulder blocked", "right lane
blocked", "shoulder blocked", "three lanes blocked", "two lanes blocked",
"left and center lane blocked", "one lane blocked", "right and center lane
blocked", "right hand shoulder blocked", "right lane blocked", "road closed",
"shoulder blocked", "three lanes blocked", "two lanes blocked", "left and
center lane blocked", "one lane blocked", "right and center lane blocked",
"right hand shoulder blocked", "right lane blocked", "road closed", "shoulder
blocked", "three lanes blocked", "two lanes blocked", "one lane blocked",
"right and center lane blocked", "right hand shoulder blocked", "right lane
blocked", "road closed", "shoulder blocked", "two lanes blocked", "left and
center lane blocked", "one lane blocked", "right and center lane blocked",
"right hand shoulder blocked", "right lane blocked", "road closed", "shoulder
blocked", "two lanes blocked" ), severity = c(8, 68, 5, 206, 142, 6, 2, 25,
32, 77, 30, 309, 285, 8, 2, 6, 89, 19, 58, 13, 224, 245, 8, 8, 2, 119, 77, 12,
34, 178, 28, 2, 61, 15, 42, 6, 34, 182, 32, 3, 122), traffic.sp = c(116, 670,
82, 2550, 1394, 82, 32, 235, 283, 651, 341, 3996, 3053, 0, 23, 71, 772, 239,
592, 174, 2967, 2748, 0, 128, 24, 1181, 990, 133, 505, 1972, 0, 46, 654, 113,
379, 74, 519, 1658, 0, 29, 1116), distance.m = c(0.02, 0.06, 0.01, 0.44, 0.26,
0.01, 0.01, 0.03, 0.05, 5.84, 0.05, 0.67, 9.36, 1.299, 0.01, 0, 1.12, 4.22,
2.875, 0, 3.37, 18.379, 1.306, 0, 0, 2.017, 12.281, 0, 0, 21.872, 3.168, 0,
12.392, 0, 2.349, 0, 0, 21.76, 3.149, 0, 6.625), traffic._1 = c(250, 1980,
168, 7618, 4193, 214, 85, 735, 873, 2238, 976, 11436, 9022, 162, 76, 166,
2518, 582, 1868, 428, 8380, 7939, 162, 313, 83, 3496, 2696, 337, 1420, 5910,
546, 80, 1776, 412, 1231, 174, 1426, 5313, 659, 80, 3180)), class =
c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -41L),
groups = structure(list( year = c("2016", "2017", "2018", "2019", "2020"),
.rows = structure(list( 1:8, 9:17, 18:26, 27:33, 34:41), ptype = integer(0),
class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl",
"data.frame" ), row.names = c(NA, -5L), .drop = TRUE))


ui <- dashboardPage(title="ScopeDashboard",
                    dashboardHeader(title = span("Scope Dashboard")),
                    
                    dashboardSidebar(
                      selectInput(
                        inputId = "varsx", 
                        label = "Choose X variable:",
                        choices = c("severity","traffic.sp","distance.m","traffic._1"),
                        multiple = F,
                        selected = "severity"
                      ),
                      selectInput(
                        inputId = "varsy", 
                        label = "Choose Y variable:",
                        choices = c("severity","traffic.sp","distance.m","traffic._1"),
                        multiple = F,
                        selected = "traffic.sp"
                      )
                    ),
                    
                    dashboardBody(
                      plotlyOutput("corrs")
                      
                    )
)

server <- function(input, output) {
  output$corrs <- renderPlotly({
    
    fig <- plot_ly(
      data = as.data.frame(DF),
      x = ~ get(input$varsx),
      y = ~ get(input$varsy),
      color = ~ accident.description,
      type = "scatter",
      marker = list(
        size = 10,
        line = list(color = 'rgba(152, 0, 0, .8)',
                    width = 2)
      ),
      text = ~ paste(
        "Accident Description:", ~ accident.description,
        "<br>Year :",
         year,
        "<br> Count  :",
        get(input$varsx),
        "<br> Count :",
        get(input$varsy)
      ),
      hoverinfo = "text",
      height = 470
    ) %>% layout(showlegend = TRUE,
                 title = paste("Something per year"),
                 xaxis = list(
                   title = input$varsx
                 ),
                 yaxis = list(
                   title =  input$varsy
                 ),
                 legend = list(title = list(text = "<b>Accident Description</b>"))
    )
    
    fig
  })
}

shinyApp(ui, server)

result

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78