Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouseover in plotly and shiny

I have some plotly code that perfectly calls the row names of a dataframe on mouseover both within RStudio and on RPubs . . . but not when embedded in Shiny. The basic code is:

require(shiny)
require(plotly)
Trial <- read.table("http://history.emory.edu/RAVINA/Aozora/Data/Trial.txt", row.names = 1)
plot_ly(Trial, x=V1, y=V2, text=rownames(Trial), mode = "markers") 

The Shiny version, however, is completely dead. What am I missing?

require(shiny)
require(plotly)
Trial <- read.table("http://history.emory.edu/RAVINA/Aozora/Data/Trial.txt", row.names = 1)

ui <- fluidPage(
  titlePanel("Word Frequency Analysis for Meiji-era Authors"),
      mainPanel(
      plotOutput("plot"),
      dataTableOutput("Print")
    )
  )


server <- function(input, output){


  output$plot<-renderPlot({
    p <- plot_ly(Trial, x=V1, y=V2, text=rownames(Trial), mode = "text")
    plot(p)
  })
  output$Print<-renderDataTable({Trial})



}

shinyApp(ui = ui, server = server)
like image 912
Mark R Avatar asked Dec 01 '15 22:12

Mark R


1 Answers

You need to swap out some base shiny functions for their plotly counterparts. Namely plotOutput -> plotlyOutput and renderPlot -> renderPlotly. Also, that last plot(p) isn't what you want to return: you just want to return p (the plot object).

require(shiny)
require(plotly)
Trial <- read.table("http://history.emory.edu/RAVINA/Aozora/Data/Trial.txt", row.names = 1)

ui <- fluidPage(
  titlePanel("Word Frequency Analysis for Meiji-era Authors"),
  mainPanel(
    plotlyOutput("plot"),
    dataTableOutput("Print")
  )
)


server <- function(input, output){            
  output$plot<-renderPlotly({
    p <- plot_ly(Trial, x=V1, y=V2, text=rownames(Trial), mode = "text")
    #plot(p)
    p
  })
  output$Print<-renderDataTable({Trial})     
}

shinyApp(ui = ui, server = server)
like image 52
David Marx Avatar answered Oct 15 '22 03:10

David Marx