鼠标悬停在阴谋和闪亮

Mouseover in plotly and shiny

我有一些巧妙的代码,可以在 RStudio 和 RPubs 上鼠标悬停时完美地调用数据框的行名称。 . .但嵌入 Shiny 时不会。 基本代码为:

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") 

然而,Shiny 版本已完全失效。我错过了什么?

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)

您需要将一些闪亮的基本函数换成它们的阴谋对应物。即 plotOutput -> plotlyOutputrenderPlot -> renderPlotly。另外,最后一个 plot(p) 不是你想要的 return:你只想 return p (情节对象)。

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)