Home > Enterprise >  Using R Shiny with plotly to make a scatterplot
Using R Shiny with plotly to make a scatterplot

Time:03-11

I am brand new to Rshiny and trying to figure out why the code I have is not giving me a plotly graph. I have a file called 'final' (tbl_df) that contains all the columns that I using. I am trying to have a scatterplot where both x and y axis's have the same list (in a dropdown) and trying to see how these values change by week.

When I Run the app, I get a blank graph, but I can click on my three dropdowns (choose a week, x-axis, & y-axis). But when I click on different values of my dropdowns, nothing happens but the x and y axis labels change.

Here's what it looks like: figure 1

I am wondering if I have to use reactive with my x and y values as well as 'Week' for this to work? I am concerned if scaling could be a reason why I'm having trouble as well.

Here's what I have..

library(shiny)
library(plotly)

ui <- fluidPage(

   titlePanel("Weekly updates"),

   sidebarLayout(
      sidebarPanel(
           selectInput(inputID = "Week", label= "Choose a week:", choices =unique(final$Week), selected ="44"),

           selectInput(inputId = "y", label = "y-axis", 
                       choices =c("Height","Weight","Color", "Shape"), 
                       selected = "Height"),
     
           selectInput(inputId = "x", label = "x-axis", 
                       choices =c("Height", "Weight", "Color", "Shape")
                       selected = "Weight")
),

      mainPanel(
         plotlyOutput(outputId = "scatter")
      )
   )
)

server <- function(input, output) {

   output$scatter <-renderPlotly({

         plot_ly(data=final) %>%
       add_trace(x=~input$x, y=~input$y,
                 type= 'scatter', mode = 'markers',
                 text = final$Sample,
                 hoverinfo = 'text'
                 showlegend = F)
   })
}

shinyApp(ui = ui, server = server)

CodePudding user response:

input$x and input$y return characters, so this resolves to something like add_trace(x=~"Height", y=~"Weight",... instead of add_trace(x=~Height, y=~Weight,....

You can however turn this into a formula instead

       plot_ly(data=final) %>%
       add_trace(x = as.formula(paste0("~",input$x)),
                 y = as.formula(paste0("~",input$y)),
                 type= 'scatter', mode = 'markers',
                 text = final$Sample,
                 hoverinfo = 'text'
                 showlegend = F)

CodePudding user response:

I figured it out. This was the only part of my code that needed to be changed... Thank you for your help!

server <- function(input, output) {

output$scatter <- renderPlotly ({
   plot_ly(data=filter(final, Week == input$Week)) %>%
      add_trace(
        x= as.formula(paste0("~`", input$x, "`")),
        y= as.formula(paste0("~`", input$y, "`")),
        type = 'scatter'
        mode = 'markers'
        text = filter(final, Week == input$Week)$Sample,
        hoverinfo = 'text'
        showlegend = F)
   })
}
  • Related