我正在尝试执行以下操作 一种。生成散点图
b。当用户单击某个点时,将使用近点功能显示与该点有关的详细信息
c。当用户选择从图表中删除该单击的数据点时,我将使用一个标志从原始数据集中删除该数据点。不知何故,此步骤似乎不起作用。
这是代码。任何指针将不胜感激
ui<- fluidPage(
plotOutput("Scatterplot", click = "plot_click"),
actionButton("updateData", "Update data by removing the clicked datapoint"),
#actionButton("refreshline", "Rline"),
dataTableOutput("info"),
verbatimTextOutput("data")
)
server<- function(input, output)
{
# data for plot
x1 <- c(3, 10, 15, 3, 4, 7, 1, 12, 8, 18, 20, 4, 4, 5, 10) #x
x2 <- c(4, 10, 12, 17, 15, 20, 14, 3, 4, 15, 12, 5, 5, 6, 2)
scatter_plot_show<- rep(1, 15)
df<- as.data.frame(cbind(x1,x2,scatter_plot_show))
plot_data <- reactive({
plot_data <- df %>%
dplyr:: filter (scatter_plot_show == 1 )
})
output$Scatterplot <- renderPlot({
ggplot(plot_data(),aes(x= x1, y=x2)) +
geom_point()
})
# use near points to get x1 clicked by user
nearpoints_data <- reactive({
nearpoints_df<- as.data.frame(nearPoints(plot_data(), input$plot_click, xvar= "x1", yvar= "x2"))
nearpoints_df1 <- plot_data() %>%
dplyr::filter(x1 %in% nearpoints_df$x1)
})
#show X1,X2 of data point that was clicked by the user
output$info <- renderDataTable({
#input$plot_click
DT::datatable(nearpoints_data())
})
# when user clicks, update data, remove the datapoint and render the scatter plot
observeEvent(
input$updateData,
{ plot_data<- plot_data() %>% mutate( scatter_plot_show=replace(scatter_plot_show, x1 %in% nearpoints_data()$x1,0))
}
)
}
shinyApp(ui=ui, server=server)
答案 0 :(得分:0)
使用Shiny Gallery中提供的一些示例找出答案。我列出解决方案以获取其他利益
options(shiny.reactlog= TRUE)
ui<- fluidPage(
plotOutput("Scatterplot", click = "plot_click"),
actionButton("Exclude_points", "Exclude points"),
actionButton("Reset", "Reset"),
#actionButton("refreshline", "Rline"),
tableOutput("info"),
tableOutput("df"),
verbatimTextOutput("data")
)
server<- function(session,input, output)
{
x1 <- c(3, 10, 15, 3, 4, 7, 1, 12, 8, 18, 20, 4, 4, 5, 10) #x
x2 <- c(4, 10, 12, 17, 15, 20, 14, 3, 4, 15, 12, 5, 5, 6, 2)
df<- as.data.frame(cbind(x1,x2)) # df defined within the app
rv<- reactiveValues(keeprows = rep(TRUE, nrow(df))) # initial keeprows to select all rows
output$Scatterplot <- renderPlot({
keep<- df [rv$keeprows,, drop=FALSE]
ggplot(keep,aes(x= x1, y=x2)) +
geom_point()
})
observeEvent(input$plot_click, {
res <- nearPoints(df, input$plot_click, allRows = TRUE)
rv$keeprows <- xor(rv$keeprows, res$selected_) # keeprows are removed from df upon plot_clicks
})
output$info <- renderTable({
rv$keeprows
})
}
shinyApp(ui=ui, server=server)