【问题标题】:R shiny brushed points table blank NA rowsR闪亮拉丝点表空白NA行
【发布时间】:2019-06-08 23:50:09
【问题描述】:

我正在开发一个shiny 应用程序,我允许用户选择绘图标准,然后还允许他们刷图并在下表中查看他们的选择。我的数据中有一些 NA 值。我注意到这些NAs 最终在我的拉丝点表中作为NA 的整行出现。我可以使用something like this 手动删除这些。但是,我想知道我是否在刷子上做错了什么导致了这种情况。

带有工作示例的代码如下。我还包括了一个画笔选择的图像,展示了我的意思。

library(shiny)
library(tidyverse)

# replace some random values in mtcars with NA
set.seed(1)
mtnew <-
  as.data.frame(lapply(mtcars, function(m)
    m[sample(
      c(TRUE, NA),
      prob = c(0.8, 0.2),
      size = length(m),
      replace = TRUE
    )]))


# set up UI that allows user to pick x and y variables, see a plot, 
#  brush the plot, and see a table based on the brush
ui <- fluidPage(

  titlePanel("Shiny Test"),

  sidebarLayout(

    sidebarPanel(
      selectInput("xvar", 
                  "pick x", 
                  choices = names(mtnew)),
      selectInput("yvar", 
                  "pick y", 
                  choices = names(mtnew))),

    mainPanel(
      plotOutput("myplot", 
                 brush = brushOpts(id = "plot_brush")),
      tableOutput("mytable")
    )
  )
)


server <- function(input, output) {
  output$myplot <- renderPlot({
    ggplot(data = mtnew) + 
      geom_point(aes(x = !!rlang::sym(input$xvar), 
                     y = !!rlang::sym(input$yvar)))
  })

  output$mytable <- renderTable({
    brush_out <- brushedPoints(mtnew, input$plot_brush)
  })
}

# Complete app with UI and server components
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我想您必须确定要表示的数据。

    您可能只想定义没有 NA 的记录,在这种情况下,我建议使用 complete.cases 函数。然而,此解决方案将大大减少您的数据集(以下我已应用于您的代码)。

    另一种选择是保留所有记录,但不保留 NA。在这种情况下,您应该考虑使用imputation methods 来设置适当的替换值。看看this post,它提供了一个例子。

    library(shiny)
    library(tidyverse)
    
    # replace some random values in mtcars with NA
    set.seed(1)
    mtnew <-
      as.data.frame(lapply(mtcars, function(m)
        m[sample(
          c(TRUE, NA),
          prob = c(0.8, 0.2),
          size = length(m),
          replace = TRUE
        )]))
    
    mtnew_complete <- mtnew[complete.cases(mtnew),] 
    
    # set up UI that allows user to pick x and y variables, see a plot, 
    #  brush the plot, and see a table based on the brush
    ui <- fluidPage(
    
      titlePanel("Shiny Test"),
    
      sidebarLayout(
    
        sidebarPanel(
          selectInput("xvar", 
                      "pick x", 
                      choices = names(mtnew)),
          selectInput("yvar", 
                      "pick y", 
                      choices = names(mtnew))),
    
        mainPanel(
          plotOutput("myplot", 
                     brush = brushOpts(id = "plot_brush")),
          tableOutput("mytable")
        )
      )
    )
    
    
    server <- function(input, output) {
      output$myplot <- renderPlot({
        #ggplot(data = mtnew) + 
        ggplot(data = mtnew_complete) + 
          geom_point(aes(x = !!rlang::sym(input$xvar), 
                         y = !!rlang::sym(input$yvar)))
      })
    
      output$mytable <- renderTable({
        #brush_out <- brushedPoints(mtnew, input$plot_brush)
        brush_out <- brushedPoints(mtnew_complete, input$plot_brush)
      })
    }
    
    # Complete app with UI and server components
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 2021-09-26
      • 2020-12-26
      • 1970-01-01
      • 2016-08-15
      • 2016-06-09
      • 2013-12-21
      • 2021-03-21
      相关资源
      最近更新 更多