【问题标题】:shiny ggplot with interactive x and y does not pass information to brush具有交互式 x 和 y 的闪亮 ggplot 不会将信息传递给画笔
【发布时间】:2016-01-06 21:13:18
【问题描述】:

我正在尝试创建一个界面,从下拉菜单中选择 x 和 y 变量后,用户可以观察散点图,并使用画笔选择点。这些点的数据将显示在数据表中。 这在静态图上非常有效,其中 x 和 y 变量不会改变。但是,当 x 和 y 变量的选择是输入选项的结果时,虽然绘图正常工作,但画笔似乎无法正确读取数据。

它不会让我上传图像,但是当您检查画笔输出的文本时,x 和 y 值的范围为 0-1,当您可以读取轴标签的实际值时,范围为0-7。所以我显然没有正确设置画笔。 数据表中出现一条错误消息,显示“错误:brushedPoints:无法从画笔自动推断“xvar”

ui<-fluidPage(
  fluidRow(
 column(4,selectInput("X_axis_Category",label="X axis Category",choices=as.list(colnames(scores_test)),selected="Ecological_Stress")        
),
column(4,selectInput("Y_axis_Category",label="Y axis Category",choices=as.list(colnames(scores_test)),selected="Ecological_Health")           
),
column(4,selectInput("Z_axis_Category",label="Z axis Category",choices=as.list(colnames(scores_test)),selected="Comprehensive_Score")
),

 column(12, plotOutput("plotui",brush=brushOpts("plot_brush",resetOnNew=T))),
 column(12,verbatimTextOutput("brush_info")),

wellPanel(width=9,h4("Points selected by brushing, with brushedPoints():"),dataTableOutput("plot_brushed_points"))
  )
)

server<-function(input,output){


  sample_scores=read.csv(file="D:\\GIS Projects\\TreesforTribs\\HUC12_Watershed_Scores.csv",sep=",")
dat<-sample_scores  
x_means<-reactive({
  mean(sample_scores[,input$X_axis_Category])
})
  y_means<-reactive({
    mean(sample_scores[,input$Y_axis_Category])

})
  x_var<-reactive({
      as.character(input$X_axis_Category)
    })

  y_var<-reactive({
    as.character(input$Y_axis_Category)
  })

  output$plotui= renderPlot({

     pc<-ggplot(sample_scores,aes_string(x=input$X_axis_Category,y=input$Y_axis_Category,size=input$Z_axis_Category,colour=input$Z_axis_Category))+ geom_point()+geom_vline(xintercept=x_means())+geom_hline(yintercept=y_means()) +
  guides(color=guide_legend(),size=guide_legend())

print (pc)
  })


   output$plot_brushed_points<-renderDataTable({
     res<-brushedPoints(dat,input$plot_brush)
       subset_res<-subset(res,select=c(HUC12,Ecological_Health,Ecological_Stress,Comprehensive_Score))
 print (subset_res)
   })

  output$brush_info<-renderPrint({

    cat("input$plot_brush:\n")
     str(input$plot_brush)
      })


    }

【问题讨论】:

  • 如果可能,提供可重现的代码,包括一些示例数据

标签: r ggplot2 shiny interactive


【解决方案1】:

好的,我修好了(这不就是你一屈服并寻求帮助的方式——你就会发现你的愚蠢错误吗?)

为了记录,画笔选项不起作用,因为当我写我的情节时,我使用了“打印”。

output$plotui= renderPlot({

 pc<-ggplot(sample_scores,aes_string(x=input$X_axis_Category,y=input$Y_axis_Category,size=input$Z_axis_Category,colour=input$Z_axis_Category))
+ geom_point()+geom_vline(xintercept=x_means())+geom_hline(yintercept=y_means()) +guides(color=guide_legend(),size=guide_legend())

print (pc)
})

这创建了一个看似正常的绘图,只是画笔没有正确读取 x 和 y 轴。当我删除“打印”并调用情节时,画笔按我想要的方式工作。

output$plotui= renderPlot({     
pc<-ggplot(sample_scores,aes_string(x=input$X_axis_Category,y=input$Y_axis_Category,size=input$Z_axis_Category,colour=input$Z_axis_Category))
+ geom_point()+geom_vline(xintercept=x_means())+geom_hline(yintercept=y_means()) +guides(color=guide_legend(),size=guide_legend())
#print (pc) ELIMINTATE THE PRINT OPTION
pc
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多