无法使用基于通过 selectInput 值选择轴的 DT 进行绘图

问题描述 投票:0回答:1

我正在制作一个应用程序,您可以上传一个.csv,该文件显示为DT并使用在selectInput中选取的轴进行绘制,您还可以对其进行过滤并获得一个新的DT(filtered_frame())。问题是,当 DT 超过一行时,它不会在图表中显示任何内容。如果我进行过滤,使其仅在 DT 中显示一行,则它可以工作。

在下面的代码中,您可以看到包含两列的数据集的示例。我已经为此苦苦挣扎了好几天,我相信解决方案非常简单,只是我没有看到它。我是初学者,我尝试寻找类似的问题但无济于事:(

Example

library(ggpubr)
library(ggplot2)
library(plotly)
library(shiny)
library(dplyr)
library(DT)
library(readr)

initial_df <- data.frame('Scratch' = c(1, 1, 3, 4, 6),
                         'Particle' = c(3, 4, 5, 6, 6))
initial_df$count <- sapply(strsplit(rownames(initial_df), ","), "[[", 1)
column_names <- colnames(initial_df)
ui <- fluidPage(
  theme = shinytheme('spacelab'),
  navbarPage(
    'Test 1',
    tags$style(HTML(".content {padding-top: 65px;}")),
    position = c('static-top'),
    fluid = TRUE,
    tabPanel('Test 2',
             fluidRow(
               column(12,
                                                fluidRow(
                            column(3, 
                                   selectInput('xAxis', 'X-Axis', choices = c('Scratch')),
                                   selectInput('yAxis', 'Y-Axis', choices = c('Scratch', 'Particle'))),
                            column(9,
                                   h4('Plot'),
                                   plotlyOutput('fig'),
                            )
                          ),
                 h4('Waferdata'),
                 DT::dataTableOutput('table'),
               ),
             ),
             ),
  )
)

server <- function(input, output, session) {
  
  XAxis <<- reactive({
    switch(input$xAxis,
           "Scratch" = "Scratch",
           )
  })
  YAxis <<- reactive({
    switch(input$yAxis,
           "Scratch" = "Scratch",
           "Particle" = "Particle",
    )
  })
  
  in_react_frame <- reactiveVal(initial_df)
  
  filtered_frame <-  reactive({
    frame <- req(in_react_frame())
    indexes <- req(input$table_rows_all)
    
    frame[indexes,]
  })
  
  summarised_frame <- reactive({
    req(filtered_frame()) %>% group_by(count) %>% summarize(count = n())
    })
  
  output$table <- renderDT(in_react_frame(),
                           filter = "top",
                           options = list(
                             pageLength = 25
                           )
  )
  output$fig <- renderPlotly({
    xaxis <<- XAxis()
    yaxis <<- YAxis()
    final_df <- filtered_frame()
    xList <- final_df[xaxis]
    yList <- final_df[yaxis]
    
    newxList <- list()
    newyList <- list()
    for (i in xList) {
      newxList[[length(newxList)+1]] <- i
    }
    for (i in yList) {
      newyList[[length(newyList)+1]] <- i
    }

    fig <- req(summarised_frame()) %>% plot_ly(x = ~newxList, y = ~newyList, type = 'scatter', mode = 'markers')
    fig <- fig %>% layout(title = "Graph",  showlegend = F,
                          xaxis = list(showgrid = TRUE, zeroline = TRUE, showticklabels = TRUE, title = xaxis, tickangle=45),
                          yaxis = list(showgrid = TRUE, zeroline = TRUE, showticklabels = TRUE, title = yaxis, tickangle=45))
    
  })
}
shinyApp(ui, server)

编辑: 解决了,请看下面我的回答。

r shiny plotly dt
1个回答
0
投票

解决了这个问题,我不得不取消列出 x 和 y 值(从列表到双精度)。我不明白为什么,但是它有效:

xValue <- unlist(newxList)
yValue <- unlist(newyList)

fig <- req(summarised_frame()) %>% plot_ly(x = ~xValue, y = ~yValue, type = 'scatter', mode = 'markers')
© www.soinside.com 2019 - 2024. All rights reserved.