akaDrHouse
akaDrHouse

Reputation: 2240

Why is RShiny executing code differently than R

I first noticed graphs graphing differently (different subsets of points on a scatter plot) between Shiny and R. I finally narrowed down my troubleshooting to the data wrangling. When I run the application file (repro.R) below in the Rstudio IDE, I get the following which is incorrect.

enter image description here

When I run the code inline, and replace the input$... with hard values that represent what the application is passing, I get different results and they are correct.

enter image description here

I've posted this on the RStudio community page as well, but no answers yet. Insight is much appreciated!

I've place testdata.RData and the R-Shiny script repro.R on https://github.com/jshousephd/repro.test.

Inline code here:

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

Session

    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

Upvotes: 0

Views: 43

Answers (1)

GyD
GyD

Reputation: 4072

Beware that the values of selectInput are stored as characters, so you have to convert them to numeric like: padj <- as.numeric(input$adj.pvalue)

Then replace input$adj.pvalue with padj, and you get the desired result:

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

Upvotes: 1

Related Questions