繁体   English   中英

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

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

我在生成一个窗口时遇到问题,可以在该窗口中从要显示的交互式绘图列表中选择一个。 目前,结果是一个带有选择侧边栏的窗口,但主窗口中有一个空图。 我从网上找到的部分拼凑起来的代码如下:

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)

我已经加载了以下包:

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

其中第一个用于创建变量html ,这是一个大list()交互式绘图,是powdR包函数的输出,并且根据各自的文档, ggplotly绘图。 这就是我使用plotlyOutput而不是plotOutput 情节本身有两个面板和一个图例,每个面板都是交互式的(我猜发布完整的数据和脚本来获取情节太多了)。

不幸的是,尽管尝试了几个小时,我还没有得到任何有用的结果。 我希望这个错误对于比我更高级的 Shiny 用户来说是显而易见的(即不是绝对的初学者)。

注意: str_replace(...)此处仅生成一个字符串等于列表名称 (html) 的字符向量,其中“AH1Y”是一个选项(此处是要预选的)。

编辑:对于对象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", "")

这给出了一个大列表对象。 但是,这个对象不能作为html[[x]]访问,而是作为html[x] (原因我不知道)。 在 R studio 中仅运行html[x]会将第x个图形绘制到查看器面板。 [[x]]调用只产生一条错误消息:

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

编辑 #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

- 问题可能是情节的某些部分与情节不兼容吗?

编辑 #3:我缩小了问题的范围 我想显示的图是使用plotly::subplot()或类似函数生成的。 当我只绘制一个绘图面板时,我可以将它正确传递给html列表中的第一个脚本。 但是,例如,如果我采用到目前为止与脚本兼容的subplot()并应用subplot()以显示该图两次堆叠在一起,则将不再可能通过shinyApp()正确显示它。

这是使用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)

解决方案:不知何故,要绘制的数据在途中丢失了; 我必须从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