简体   繁体   English

通过闪亮绘制交互式 ggplotly 图(当前生成一个空图)

[英]Plot an interactive ggplotly plot via shiny (currently produces an empty plot)

I have problems generating a window where one can select one out of a list of interactive plots to be displaied.我在生成一个窗口时遇到问题,可以在该窗口中从要显示的交互式绘图列表中选择一个。 Currently, the result is a window with the selection sidebar but an empty plot in the main window.目前,结果是一个带有选择侧边栏的窗口,但主窗口中有一个空图。 The code I put together from parts I've found on the internet is as follows:我从网上找到的部分拼凑起来的代码如下:

ui <- pageWithSidebar(
  headerPanel(h1(textOutput("XRD-Ergebnisse"))),
  sidebarPanel(
    uiOutput("filter_degree"), width = 2
  ),
  mainPanel = mainPanel(
    h3(textOutput("Diffraction Pattern")),
    plotlyOutput("plot")
  )
)
#server.r
server <- function(input, output, session){
  output$filter_degree <- renderUI({
    radioButtons("rd", "Select Option", choices = str_replace(files, ".xy", ""),
                 selected = "AH1Y")
  })
  output$plot <- renderPlotly({
    plot <- html[which(str_replace(files, ".xy", "") == input$rd)]
    })
}
shinyApp(ui = ui, server = server)

and I've loaded the following packages:我已经加载了以下包:

c("powdR", "ggplot2", "stringr", "tools", "readxl", "dplyr", "shiny", "plotly")

of which the first ones were used to create the variable html , which is a large list() of interactive plots that are output of functions of the powdR package and, according to the respective documentation, ggplotly plots.其中第一个用于创建变量html ,这是一个大list()交互式绘图,是powdR包函数的输出,并且根据各自的文档, ggplotly绘图。 This is why I used plotlyOutput instead of plotOutput .这就是我使用plotlyOutput而不是plotOutput The plots themselves have two panels and a legend each and are interactive (posting the complete data and script to get the plots would be too much, I guess).情节本身有两个面板和一个图例,每个面板都是交互式的(我猜发布完整的数据和脚本来获取情节太多了)。

Unfortunately, despite trying for hours I haven't got any useful results.不幸的是,尽管尝试了几个小时,我还没有得到任何有用的结果。 I hope the error is obvious to more advanced Shiny users than I am (ie to not absolute beginners).我希望这个错误对于比我更高级的 Shiny 用户来说是显而易见的(即不是绝对的初学者)。

Notes: str_replace(...) here produces only a character vector with strings equal to the names of the list (html), of which "AH1Y" is one option (here the one to be preselected).注意: str_replace(...)此处仅生成一个字符串等于列表名称 (html) 的字符向量,其中“AH1Y”是一个选项(此处是要预选的)。

Edit: For the definition of the object html编辑:对于对象html的定义

html <- list()
for(i in 1:NROW(results)){
  html[[i]] <- plot(results[[i]], wavelength = "Cu", interactive = TRUE, mode = "both", main = str_replace(files[[i]], ".xy", ""))
}
names(html) <- str_replace(files, ".xy", "")

Which gives a large list object.这给出了一个大列表对象。 This object, however, is not accessible as html[[x]] but as html[x] (for reasons I don't know).但是,这个对象不能作为html[[x]]访问,而是作为html[x] (原因我不知道)。 Running just html[x] in R studio plots the x th graph to the viewer panel.在 R studio 中仅运行html[x]会将第x个图形绘制到查看器面板。 The [[x]] call only produces an error message: [[x]]调用只产生一条错误消息:

Warning: Error in [[: subscript out of bounds
  [No stack trace available]

Edit #2: When I try to use the non-interactive version and manually ggplotly() it, I get:编辑 #2:当我尝试使用非交互式版本并手动ggplotly()时,我得到:

Warning messages:
1: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDrawGrob() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
2: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDrawGrob() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues

-could the issue be some parts of the plot that are not compatible with plotly? - 问题可能是情节的某些部分与情节不兼容吗?

Edit #3: I narrowed down the problem The plots I want to display were generated using plotly::subplot() or a similar function.编辑 #3:我缩小了问题的范围 我想显示的图是使用plotly::subplot()或类似函数生成的。 When I only plot one of the plot panels I can pass it to the first script within the html list correctly.当我只绘制一个绘图面板时,我可以将它正确传递给html列表中的第一个脚本。 However, if I eg take this plot that was compatible with the script so far and apply subplot() in order to show the plot two times stacked on each other, it will no longer be possible to display it correctly in via shinyApp() .但是,例如,如果我采用到目前为止与脚本兼容的subplot()并应用subplot()以显示该图两次堆叠在一起,则将不再可能通过shinyApp()正确显示它。

Here's an example of using ggplotly to make two plots that get saved in a list and then called by a selector.这是使用ggplotly制作两个图的示例,这些图保存在列表中,然后由选择器调用。

library(shiny)
library(plotly)

ui <- fluidPage(
  sidebarPanel(
    selectInput("plots", "Plots", c("plot1", "plot2"))
  ),
  mainPanel(
    plotlyOutput("plot")
  )
  
)

server <- function(input, output, session) {
  p1 <- ggplot(iris, aes_string(x="Sepal.Length", y="Petal.Length")) +
    geom_point()  
  p1 <- ggplotly(p1)
  p2 <- ggplot(iris, aes_string(x="Sepal.Width", y="Petal.Width")) +
    geom_point() 
  p2 <- ggplotly(p2)
  plots <- list("plot1" = p1, "plot2" = p2)
  
  output$plot <- renderPlotly({
    out <- plots[[input$plots]]
  })
  
}

shinyApp(ui, server)

Solution: Somehow the data to plot gets lost on the way;解决方案:不知何故,要绘制的数据在途中丢失了; I have to plot from within the server <-... function like this:我必须从server <-...内绘制server <-...函数,如下所示:

server <- function(input, output, session){
  output$filter_degree <- renderUI({
    radioButtons("rd", "Select Sample", choices = str_replace(files, ".xy", ""),
                 selected = "AH1Y")
  })
  output$plot <- renderPlotly({
    plot(results[[which(str_replace(files, ".xy", "") == input$rd)]], wavelength = "Cu", interactive = TRUE, mode = "both",
         main = str_replace(files[[i]], ".xy", ""))
  })
}
shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM