首页 > 解决方案 > 开关不会从上传的文件中找到列,除非上传的文件是同时创建的

问题描述

如果我运行这段代码,我们会得到错误:找不到对象'AB'。但是如果我们从代码中删除##(在上传文件之前重新创建文件),这个问题就不会发生。tableOutput 也找到了文件,如果我们调用列名而不是 tab_input1() 则文件没有问题。我还确保该文件已保存到我的计算机中,并且我尝试将其编写为 csv 和 tsv 文件。

通过移除开关,而使输入选择与列中完全相同,可以解决问题。然而,这将是一个丑陋的修复。

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(), B)) + 
              expand_limits(x=0, y=0)
    )
  })

}

shinyApp(ui, server)

我的会话信息()

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   

标签: r

解决方案


rstudio 社区的 hugo-pa 进行了修复,这归功于他。我对他的答案做了一些细微的修改,以更好地适应这篇文章,因为我在得到@teofil 的一些帮助并调整了他的答案后,在 rstudio 社区做了一个。需要更改才能解决我的问题。我通过将@teofil 的答案改编为绘图部分所做的第一个更改:

output$experiment1_plot <- renderPlotly({
    our_df <- table_input()
    ggplotly(
      ggplot(our_df) +
        geom_point(aes(x = our_df[,tab_input1()], y = C)) + 
        expand_limits(x=0, y=0)
    )
  })

第二个变化,来自 hugo-pa 的精彩解释:

首先,我想解释一下为什么取消注释 ## 行时不会发生错误:如您所知,这些行创建数据框,然后将其保存到文件中;但是,在创建数据框之前,对象 AB、B 和 C 都在服务器函数的范围内创建。因此,您的 tab_input1 反应式表达式中的 switch() 调用能够定位对象 AB 和 B。因此,解决错误的不是在加载之前将数据帧保存到文件的行为,而是分配AB 和 B 变量。

必须更新 switch 语句才能完全修复它,如下所示:

switch(input$y_axis1,
       "a or" = "AB",
       "b" = "B")

同样,您必须这样做的原因是,当您从 test.rds 文件加载对象时,您只加载保存在其中的数据框(即 my_df;这是因为 saveRDS() 仅保存一个R 对象到文件),而不是用于创建它的变量。因此,为了获得您想要的数据,您必须从数据框中提取它。


推荐阅读