为什么RShiny执行代码的方式与R不同

时间:2019-05-03 12:43:22

标签: r shiny tidyverse

我首先注意到在Shiny和R之间图形不同(散点图上点的不同子集)的图形。最后我将故障排除的范围缩小到数据整理。当我在Rstudio IDE中运行下面的应用程序文件(repro.R)时,得到以下错误信息。

enter image description here

当我内联运行代码并将input$...替换为代表应用程序正在传递的硬值时,我得到的结果是不同的,而且它们是正确的。

enter image description here

我也已在RStudio社区页面上发布了此内容,但尚未找到答案。洞察力倍受赞赏!

我已将testdata.RData和R-Shiny脚本repro.R放在https://github.com/jshousephd/repro.test上。

此处为内联代码:

library(tidyverse)
testdata %>%
dplyr::filter(trt == trt_list[2]) %>%
dplyr::select(x = log2FoldChange, qx = padj, mylabel) -> x
testdata %>%
dplyr::filter(trt == trt_list[1]) %>%
dplyr::select(y = log2FoldChange, qy = padj, mylabel) -> y
plotdata <- dplyr::left_join(x, y, by = c("mylabel"))
plotdata %>% dplyr::mutate(x = ifelse(is.na(x), 0, x),
y = ifelse(is.na(y), 0, y),
qx = ifelse(is.na(qx), 1, qx),
qy = ifelse(is.na(qy), 1, qy)) %>%
dplyr::mutate(significance = ifelse(plotdata$qx <= .1 & plotdata$qy > .1, "X-Significant",
ifelse(plotdata$qx > .1 & plotdata$qy <= .1, "Y-Significant",
ifelse(plotdata$qx <= .1 & plotdata$qy <= .1, "Both-Significant", "Neither")))) %>%
dplyr::filter(significance != "Neither") -> plotdata
final <- as.data.frame(table(plotdata$significance))
final

会话

    sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] shinythemes_1.1.2 DT_0.5 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.0.1 purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_2.1.1
[10] ggplot2_3.1.1 tidyverse_1.2.1 shiny_1.2.0

loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 cellranger_1.1.0 pillar_1.3.1 compiler_3.6.0 later_0.8.0 plyr_1.8.4 tools_3.6.0 digest_0.6.18 lubridate_1.7.4
[10] jsonlite_1.6 nlme_3.1-139 gtable_0.3.0 lattice_0.20-38 pkgconfig_2.0.2 rlang_0.3.1 cli_1.1.0 rstudioapi_0.10 crosstalk_1.0.0
[19] yaml_2.2.0 haven_2.1.0 withr_2.1.2 xml2_1.2.0 httr_1.4.0 htmlwidgets_1.3 hms_0.4.2 generics_0.0.2 grid_3.6.0
[28] tidyselect_0.2.5 glue_1.3.0 R6_2.3.0 readxl_1.3.1 modelr_0.1.4 magrittr_1.5 backports_1.1.4 scales_1.0.0 promises_1.0.1
[37] htmltools_0.3.6 rvest_0.3.3 assertthat_0.2.1 mime_0.6 xtable_1.8-3 colorspace_1.4-1 httpuv_1.4.5.1 stringi_1.2.4 lazyeval_0.2.2
[46] munsell_0.5.0 broom_0.5.2 crayon_1.3.4

1 个答案:

答案 0 :(得分:1)

请注意,selectInput的值存储为字符,因此您必须将它们转换为数字,例如:padj <- as.numeric(input$adj.pvalue)

然后将input$adj.pvalue替换为padj,您将获得所需的结果:

dplyr::mutate(significance = ifelse(plotdata$qx <= padj & plotdata$qy > padj, "X-Significant",
                                          ifelse(plotdata$qx > padj & plotdata$qy <= padj, "Y-Significant",
                                                 ifelse(plotdata$qx <= padj & plotdata$qy <= padj, "Both-Significant", "Neither")))) %>%
      dplyr::filter(significance != "Neither") -> plotdata