Sorry for the bad title description. Tried posting the same on reddit and stackoverflow but haven't gotten any help. Try running the code firs without the ## and then with them and you'll see my problem. Been asking more senior coders to have a look at it, but no solutions have been found. My conclusion is that it's a bug. Because the code works when you first create the file (no ##) then upload it but it doesn't work when you only upload it.
If I run this code, we get the Error: object 'AB' not found. But if we remove the ## from the code (recreating the file before uploading it) this problem doesn't occur. Also tableOutput finds the file (and prints it just fine) and if we call the column name instead of tab_input1() there's no issues with plotting the file. I've also made sure that the file is saved to my computer and I've tried writing it as a csv and tsv file.
library(shiny)
library(tidyverse)
library(plotly)
ui <- fluidPage(tabsetPanel(
tabPanel("Experiment 1",
verticalLayout(fluidRow(
column(4, selectInput(inputId = "y_axis1", label = "Y-axis",
choices = c("a or", "b")))
),
plotlyOutput("experiment1_plot"),
tableOutput("table_example")
)
)
)
)
server <- function(input, output, session) {
##AB <- c(1, 3, 5)
##B <- c(2, 4, 6)
##C <- c(10, 20, 30)
##my_df <- data.frame(AB, B, C)
##saveRDS(my_df, "data/test.rds")
testing_df <- readRDS("data/test.rds")
tab_input1 <- reactive({
switch(input$y_axis1,
"a or" = AB,
"b" = B)
})
output$table_example <- renderTable(testing_df$AB)
output$experiment1_plot <- renderPlotly({
ggplotly(
ggplot(testing_df) +
geom_point(aes(x = tab_input1(), y = C)) +
####geom_point(aes(x = testing_df[,tab_input1()], y = C)) +
expand_limits(x=0, y=0)
)
})
}
shinyApp(ui, server)
Tried adapting the suggestion I got at stackoverflow (the row with ####), but I think this has the exact same problem and it gives "Warning: Error in [.data.frame: undefined columns selected"
What I'm having a hard time wrapping my head around why doesn't the same error occur if I write the file inside the server. (by removing the ## signs, if you want to try it yourself)
I'm thinking the problem is caused within the switch somewhere when combined with reading the file.
Session info:
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plotly_4.9.0 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_2.1.3
[9] ggplot2_3.2.0 tidyverse_1.2.1 shiny_1.3.2
loaded via a namespace (and not attached):
[1] tidyselect_0.2.5 haven_2.1.1 lattice_0.20-38 colorspace_1.4-1 generics_0.0.2 vctrs_0.2.0 htmltools_0.3.6
[8] viridisLite_0.3.0 yaml_2.2.0 rlang_0.4.0 later_0.8.0 pillar_1.4.2 glue_1.3.1 withr_2.1.2
[15] modelr_0.1.5 readxl_1.3.1 munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.4 htmlwidgets_1.3
[22] labeling_0.3 crosstalk_1.0.0 httpuv_1.5.1 broom_0.5.2 Rcpp_1.0.2 xtable_1.8-4 promises_1.0.1
[29] scales_1.0.0 backports_1.1.4 jsonlite_1.6 mime_0.7 hms_0.5.0 digest_0.6.20 stringi_1.4.3
[36] grid_3.6.1 cli_1.1.0 tools_3.6.1 magrittr_1.5 lazyeval_0.2.2 crayon_1.3.4 pkgconfig_2.0.2
[43] zeallot_0.1.0 data.table_1.12.2 xml2_1.2.1 lubridate_1.7.4 assertthat_0.2.1 httr_1.4.1 rstudioapi_0.10
[50] R6_2.4.0 nlme_3.1-140 compiler_3.6.1
Edit: formatting