【发布时间】: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)
【问题讨论】: