timelyportfolio / shiny-websockets

experiments with shiny at the core and with react
3 stars 0 forks source link

Not an issue - Need some guidance #1

Open debsush opened 8 years ago

debsush commented 8 years ago

Hi Kent,

I was trying to create crosshairs on a ggplot2 and this is the best I could come up with. Do you think we can achieve this using native R functions or do we have to resort of Javascript. Given your expertise in this field, was wondering if you could provide your views.

library(shiny)
library(ggplot2)
library(Cairo)   # For nicer ggplot2 output when deployed on Linux

# We'll use a subset of the mtcars data set, with fewer columns
# so that it prints nicely
mtcars2 <- mtcars[, c("mpg", "cyl", "disp", "hp", "wt", "am", "gear")]

ui <- fluidPage(
  fluidRow(
    column(width = 4,
           plotOutput("plot1", height = 300,
                      # Equivalent to: click = clickOpts(id = "plot_click")
                      hover = hoverOpts(id = "plot_hover", delayType = "throttle"),
           )
    )
  ),
  fluidRow(
    column(width = 6,
           h4("Hover Points"),
           verbatimTextOutput("hover_info")
    )
  )
)

server <- function(input, output) {
  output$plot1 <- renderPlot({
    ggplot(mtcars2, aes(wt, mpg)) + 
    geom_point()  + 
    geom_vline(xintercept = xdata(),colour="black", linetype = "longdash") +
    geom_hline(yintercept = ydata(),colour="black", linetype = "longdash") +
    scale_x_continuous(limit = c(0, 5)) +
    scale_y_continuous(limit = c(0, 35))  
  })

  xdata<-reactive({
    ifelse(!is.null(input$plot_hover$x), return(input$plot_hover$x), return(0))
  })

  ydata<-reactive({
    ifelse(!is.null(input$plot_hover$y), return(input$plot_hover$y), return(0))
  })

  output$hover_info <- renderPrint({
    input$plot_hover$x
  }) 
}

shinyApp(ui, server)
timelyportfolio commented 8 years ago

This could be handled as you suggest but would be much better with JavaScript. I do not see any advantage to drawing the crosshairs from R. Likely you will quickly run into performance degradation. With either route, you will probably need something like d3 to draw the crosshairs. Since the plot is static, following the line with the crosshairs will be very difficult but not impossible.

Kent

On Tuesday, July 26, 2016, debsush notifications@github.com wrote:

Hi Kent,

I was trying to create crosshairs on a ggplot2 and this is the best I could come up with. Do you think we can achieve this using native R functions or do we have to resort of Javascript. Given your expertise in this field, was wondering if you could provide your views.

library(shiny) library(ggplot2) library(Cairo) # For nicer ggplot2 output when deployed on Linux

We'll use a subset of the mtcars data set, with fewer columns

so that it prints nicely

mtcars2 <- mtcars[, c("mpg", "cyl", "disp", "hp", "wt", "am", "gear")]

ui <- fluidPage( fluidRow( column(width = 4, plotOutput("plot1", height = 300,

Equivalent to: click = clickOpts(id = "plot_click")

                  hover = hoverOpts(id = "plot_hover", delayType = "throttle"),
       )
)

), fluidRow( column(width = 6, h4("Hover Points"), verbatimTextOutput("hover_info") ) ) )

server <- function(input, output) { output$plot1 <- renderPlot({ ggplot(mtcars2, aes(wt, mpg)) + geom_point() + geom_vline(xintercept = xdata(),colour="black", linetype = "longdash") + geom_hline(yintercept = ydata(),colour="black", linetype = "longdash") + scale_x_continuous(limit = c(0, 5)) + scale_y_continuous(limit = c(0, 35)) })

xdata<-reactive({ ifelse(!is.null(input$plot_hover$x), return(input$plot_hover$x), return(0)) })

ydata<-reactive({ ifelse(!is.null(input$plot_hover$y), return(input$plot_hover$y), return(0)) })

output$hover_info <- renderPrint({ input$plot_hover$x }) }

shinyApp(ui, server)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/timelyportfolio/shiny-websockets/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/AAzJFqU8gBk-zzgviq5_mL-47RSOkgk8ks5qZjl6gaJpZM4JVWhB .

debsush commented 8 years ago

Thanks for the feedback. Do you have any tutorial or some demo where we can combine D3 in JS with ggplot2. I am not particularly interested in plotly as it does not always suit my requirement but a generic use case mixing JS and ggplot2 which could help me introduce D3 components into my plot would be greatly appreciated.

On Wed, Jul 27, 2016 at 1:30 AM, timelyportfolio notifications@github.com wrote:

This could be handled as you suggest but would be much better with JavaScript. I do not see any advantage to drawing the crosshairs from R. Likely you will quickly run into performance degradation. With either route, you will probably need something like d3 to draw the crosshairs. Since the plot is static, following the line with the crosshairs will be very difficult but not impossible.

Kent

On Tuesday, July 26, 2016, debsush notifications@github.com wrote:

Hi Kent,

I was trying to create crosshairs on a ggplot2 and this is the best I could come up with. Do you think we can achieve this using native R functions or do we have to resort of Javascript. Given your expertise in this field, was wondering if you could provide your views.

library(shiny) library(ggplot2) library(Cairo) # For nicer ggplot2 output when deployed on Linux

We'll use a subset of the mtcars data set, with fewer columns

so that it prints nicely

mtcars2 <- mtcars[, c("mpg", "cyl", "disp", "hp", "wt", "am", "gear")]

ui <- fluidPage( fluidRow( column(width = 4, plotOutput("plot1", height = 300,

Equivalent to: click = clickOpts(id = "plot_click")

hover = hoverOpts(id = "plot_hover", delayType = "throttle"), ) ) ), fluidRow( column(width = 6, h4("Hover Points"), verbatimTextOutput("hover_info") ) ) )

server <- function(input, output) { output$plot1 <- renderPlot({ ggplot(mtcars2, aes(wt, mpg)) + geom_point() + geom_vline(xintercept = xdata(),colour="black", linetype = "longdash") + geom_hline(yintercept = ydata(),colour="black", linetype = "longdash") + scale_x_continuous(limit = c(0, 5)) + scale_y_continuous(limit = c(0, 35)) })

xdata<-reactive({ ifelse(!is.null(input$plot_hover$x), return(input$plot_hover$x), return(0)) })

ydata<-reactive({ ifelse(!is.null(input$plot_hover$y), return(input$plot_hover$y), return(0)) })

output$hover_info <- renderPrint({ input$plot_hover$x }) }

shinyApp(ui, server)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/timelyportfolio/shiny-websockets/issues/1, or mute the thread < https://github.com/notifications/unsubscribe-auth/AAzJFqU8gBk-zzgviq5_mL-47RSOkgk8ks5qZjl6gaJpZM4JVWhB

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/timelyportfolio/shiny-websockets/issues/1#issuecomment-235437595, or mute the thread https://github.com/notifications/unsubscribe-auth/AOYQSXcRCLSTkiojGqC-_3w5ONbtSF3rks5qZpivgaJpZM4JVWhB .