Shiny 为什么条形图更新不基于反应式数据帧? 问题
尽管两者共享相同的反应数据帧,为什么条形图与我的应用程序中表格中显示的数据不匹配 概述 我目前正在df-a中安排数据帧,它使用单选按钮的输入,并根据数据安排数据。之后,df以两种形式显示:条形图和表格 问题是条形图不响应单选按钮,而表格响应。我希望的输出是,条形图按照单选按钮的升序/降序排列,与表格的排列方式相同 任何关于为什么会发生这种情况以及我如何解决这一问题的澄清都将不胜感激 密码Shiny 为什么条形图更新不基于反应式数据帧? 问题,shiny,r,ggplot2,reactive-programming,Shiny,R,Ggplot2,Reactive Programming,尽管两者共享相同的反应数据帧,为什么条形图与我的应用程序中表格中显示的数据不匹配 概述 我目前正在df-a中安排数据帧,它使用单选按钮的输入,并根据数据安排数据。之后,df以两种形式显示:条形图和表格 问题是条形图不响应单选按钮,而表格响应。我希望的输出是,条形图按照单选按钮的升序/降序排列,与表格的排列方式相同 任何关于为什么会发生这种情况以及我如何解决这一问题的澄清都将不胜感激 密码 要更改绘图中的顺序,不必更改ggplot中数据的顺序。必须在绘图函数中指定它: 我已经创建了一个if-els
要更改绘图中的顺序,不必更改ggplot中数据的顺序。必须在绘图函数中指定它: 我已经创建了一个if-else条件,以便在单击升序或降序时创建不同的绘图。看看代码。我刚刚在geom_bar函数中添加了一个重新排序标准 代码: 如果需要,可以使用labs重命名轴 输出应用程序位于此处概述 多亏了@Gregor,我意识到我的错误在于没有理解条形图顺序是由因子水平决定的 虽然@Vedha Viyash的答案确实有效,但我结合了@Gregor的建议,通过设置df$Sepal.Length安排的df$物种水平来改变df$物种 通过不在输出$my.barplot中重复plot对象的构造,这是对主体的尊重
本质上是一个复制品。如答案所示,条形图的顺序不是由行顺序决定的,而是由因子级别的顺序决定的。@Gregor,谢谢!这有助于我理解我错在哪里。我已经更新了df的结构,以包含一个物种的突变版本,该物种通过Sepal.Length对该变量进行重新排序。我感谢你的帮助!是的,这当然会起作用,但如果使用“if-else”条件,则在选择任何一个ActionButton时,也可以修改图形的其他方面。我是说应用程序将更加灵活。@VedhaViyash我只希望df灵活,而不是构建输出$my.barplot。但是,如果没有您的帮助,我甚至不会听说stats::reorder函数,所以谢谢您@维德哈维亚什干葡萄酒在这里很好。如果要修改图形的各个方面,则需要在一个位置进行更改。如果添加了另一个选项,例如更改主题,则您希望在一个位置进行更改。不重复绘图代码使代码更易于维护和调试。
# 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 #
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
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({
if(input$direction=="Ascending")
ggplot( data = df() ) +
geom_bar( aes( x = Species
, y = Sepal.Length )
, stat = "identity" )
else if(input$direction=="Descending")
ggplot( data = df() ) +
geom_bar( aes( x = reorder(Species,-Sepal.Length)
, y = Sepal.Length )
, stat = "identity" )
})
# show df()
output$my.table <- renderTable({
df()
})
}
# run App
shinyApp( ui = ui, server = server )
# 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 #