为什么不基于反应数据框更新条形图? [英] Why isn't bar plot updating based on reactive data frame?

查看:44
本文介绍了为什么不基于反应数据框更新条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

尽管两者共享同一个反应数据框架,但条形图为何与我的

代码

 #加载必要的程序包图书馆(闪亮的)图书馆(tidyverse)#创建用户界面ui<-fluidPage(title =反应式编程",plotOutput(outputId ="my.barplot"),radioButtons(inputId ="direction",label =安排数据:",choices = c("Ascending","Descending"),selected =降序",inline = TRUE),tableOutput(outputId ="my.table"))#创建服务器服务器<-功能(输入,输出,会话){#根据"Sepal.Length"列以升/降顺序排列虹膜df<-反应性({开关(EXPR =输入$方向,升序" =虹膜%>%排列(Sepal.Length),下降" =虹膜%>%排列(desc(Sepal.Length)))})#绘图df()output $ my.barplot<-renderPlot({ggplot(data = df())+geom_bar(aes(x =物种,y = Sepal.Length),stat ="identity")})#显示df()output $ my.table<-renderTable({df()})}#运行应用ShinyApp(ui = ui,服务器=服务器)#脚本结尾# 

会话信息

  R版本3.4.4(2018-03-15)平台:x86_64-apple-darwin15.6.0(64位)运行于:macOS High Sierra 10.13.2矩阵产品:默认BLAS:/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylibLAPACK:/Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib语言环境:[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8附带的基本软件包:[1]统计图形grDevices utils数据集方法库其他附件包:[1] forcats_0.3.0 stringr_1.3.0 dplyr_0.7.4 purrr_0.2.4 readr_1.1.1[6] tidyr_0.8.0 tibble_1.4.2 ggplot2_2.2.1 tidyverse_1.2.1 Shiny_1.0.5通过名称空间(未附加)加载:[1] Rcpp_0.12.16 cellranger_1.1.0支柱_1.2.1编译器_3.4.4[5] plyr_1.8.4绑定器_0.1.1工具_3.4.4摘要_0.6.15[9] lubridate_1.7.3 jsonlite_1.5 nlme_3.1-131.1 gtable_0.2.0[13]点阵_0.20-35 pkgconfig_2.0.1 rlang_0.2.0 psych_1.7.8[17] cli_1.0.0 rstudioapi_0.7 yaml_2.1.18 parallel_3.4.4[21] Haven_1.1.1 bindrcpp_0.2 xml2_1.2.0 httr_1.3.1[25] hms_0.4.2 grid_3.4.4胶水_1.2.0 R6_2.2.2[29] readxl_1.0.0 foreign_0.8-69 modelr_0.1.1 reshape2_1.4.3[33] magrittr_1.5标度_0.5.0 htmltools_0.3.6 rvest_0.3.2[37]断言_0.2.0 mnormt_1.5-5 colorspace_1.3-2 mime_0.5[41] xtable_1.8-2 httpuv_1.3.6.2 stringi_1.1.7 lazyeval_0.2.1[45]孟塞尔_0.4.3扫帚_0.4.3蜡笔_1.3.4 

解决方案

概述

由于@Gregor,我意识到我的错误是不了解条形图的顺序是由因子水平决定的.

@Vedha Viyash的答案确实有效时,我结合了@Gregor的建议以使用

 #加载必要的程序包图书馆(闪亮的)图书馆(tidyverse)#创建用户界面ui<-fluidPage(title =反应式编程",plotOutput(outputId ="my.barplot"),radioButtons(inputId ="direction",label =安排数据:",choices = c("Ascending","Descending"),selected =降序",inline = TRUE),tableOutput(outputId ="my.table"))#创建服务器服务器<-功能(输入,输出,会话){#根据"Sepal.Length"列以升/降顺序排列虹膜df<-反应性({开关(EXPR =输入$方向,升序" =虹膜%>%排列(Sepal.Length)%&%;%mutate(种=重新排序(x =种,X = Sepal.Length)),"Descending" =虹膜%>%排列(desc(Sepal.Length))%>%mutate(种=重新排序(x =种,X = desc(Sepal.Length)))))})#绘图df()output $ my.barplot<-renderPlot({ggplot(data = df())+geom_bar(aes(x =物种,y = Sepal.Length),stat ="identity")})#显示df()output $ my.table<-renderTable({df()})}#运行应用ShinyApp(ui = ui,服务器=服务器)#脚本结尾# 

Question

Despite both sharing the same reactive data frame, why does the bar plot not match the data shown in the table in my app?

Overview

I'm currently arranging the iris data frame in df - a reactive expression that uses the input of a radio button and arranges the data based on base::switch(). Afterwards, df is displayed in two forms: a bar plot and a table.

The trouble is that the bar plot doesn't respond to the radio button, while the table does. My desired output would be for the bar plot to be arranged in ascending/descending order - based on the radio button - in the same way the table is arranged.

Any clarification on why this is happening and how I can resolve this would be much appreciated!

Code

# load necessary package
library( shiny )
library( tidyverse )

# create UI
ui <- fluidPage(
  title = "Reactive Programming"
  , plotOutput( outputId = "my.barplot" )
  , radioButtons( inputId = "direction"
                  , label = "Arrange the data:"
                  , choices = c("Ascending", "Descending")
                  , selected = "Descending"
                  , inline = TRUE )
  , tableOutput( outputId = "my.table" )
)

# create server
server <- function( input, output, session){

  # arrange iris in ascending/descending order based on the "Sepal.Length" column
  df <- reactive({
    switch( EXPR = input$direction
            , "Ascending" = iris %>% arrange( Sepal.Length ) 
            , "Descending" = iris %>% arrange( desc( Sepal.Length ) ) )
  })

  # plot df()
  output$my.barplot <- renderPlot({
    ggplot( data = df() ) +
      geom_bar( aes( x = Species
                     , y = Sepal.Length )
                , stat = "identity" )
  })

  # show df()
  output$my.table <- renderTable({
    df()
  })
}

# run App
shinyApp( ui = ui, server = server )

# end of script #

Session Info

R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.3.0   stringr_1.3.0   dplyr_0.7.4     purrr_0.2.4     readr_1.1.1    
 [6] tidyr_0.8.0     tibble_1.4.2    ggplot2_2.2.1   tidyverse_1.2.1 shiny_1.0.5    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     cellranger_1.1.0 pillar_1.2.1     compiler_3.4.4  
 [5] plyr_1.8.4       bindr_0.1.1      tools_3.4.4      digest_0.6.15   
 [9] lubridate_1.7.3  jsonlite_1.5     nlme_3.1-131.1   gtable_0.2.0    
[13] lattice_0.20-35  pkgconfig_2.0.1  rlang_0.2.0      psych_1.7.8     
[17] cli_1.0.0        rstudioapi_0.7   yaml_2.1.18      parallel_3.4.4  
[21] haven_1.1.1      bindrcpp_0.2     xml2_1.2.0       httr_1.3.1      
[25] hms_0.4.2        grid_3.4.4       glue_1.2.0       R6_2.2.2        
[29] readxl_1.0.0     foreign_0.8-69   modelr_0.1.1     reshape2_1.4.3  
[33] magrittr_1.5     scales_0.5.0     htmltools_0.3.6  rvest_0.3.2     
[37] assertthat_0.2.0 mnormt_1.5-5     colorspace_1.3-2 mime_0.5        
[41] xtable_1.8-2     httpuv_1.3.6.2   stringi_1.1.7    lazyeval_0.2.1  
[45] munsell_0.4.3    broom_0.4.3      crayon_1.3.4 

解决方案

Overview

Thanks to @Gregor, I realized my mistake was not understanding that bar plot order is determined by factor level.

While @Vedha Viyash's answer does work, I combined @Gregor's advice to use dplyr::mutate() to alter df()$Species by using stats::reorder() to set the levels in df()$Species to be arranged by df()$Sepal.Length.

This honors the D.R.Y principal by not repeating the construction of the plot object inside of output$my.barplot.

# load necessary package
library( shiny )
library( tidyverse )

# create UI
ui <- fluidPage(
  title = "Reactive Programming"
  , plotOutput( outputId = "my.barplot" )
  , radioButtons( inputId = "direction"
                  , label = "Arrange the data:"
                  , choices = c("Ascending", "Descending")
                  , selected = "Descending"
                  , inline = TRUE )
  , tableOutput( outputId = "my.table" )
)

# create server
server <- function( input, output, session){

  # arrange iris in ascending/descending order based on the "Sepal.Length" column
  df <- reactive({
    switch( EXPR = input$direction
            , "Ascending" = iris %>% arrange( Sepal.Length ) %>% mutate( Species = reorder( x = Species, X = Sepal.Length ) )
            , "Descending" = iris %>% arrange( desc( Sepal.Length ) ) %>% mutate( Species = reorder( x = Species, X = desc( Sepal.Length ) ) ) )
  })

  # plot df()
  output$my.barplot <- renderPlot({
    ggplot( data = df() ) +
      geom_bar( aes( x = Species
                     , y = Sepal.Length )
                , stat = "identity" )
  })

  # show df()
  output$my.table <- renderTable({
    df()
  })
}

# run App
shinyApp( ui = ui, server = server )

# end of script #

这篇关于为什么不基于反应数据框更新条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆