我有一些巧妙的代码,可以在鼠标悬停时在 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)
最佳答案
您需要为它们的对应函数替换一些基本的 Shiny 函数。即plotOutput
-> plotlyOutput
和 renderPlot
-> renderPlotly
.还有,最后一个 plot(p)
不是你想返回的:你只想返回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)
关于r - 鼠标悬停在 plotly 和 Shiny ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031570/