闪亮:上传数据后,updateSelectInput为空

时间:2020-01-25 17:40:41

标签: r shiny reactive shiny-reactivity

我有一个Shiny应用程序,用户将.txt文件上传到其中,然后用户可以用来创建绘图。该图取决于用户根据上传数据的列名选择输入。这些输入是下拉列表,其中包含上传数据后的列名称。

该应用程序于2020年1月23日开始工作。但是在今天(2020年1月25日),代码没有任何更新,下拉输入不再显示已上传数据框列的名称。在运行应用程序时,没有错误提示,因为下拉列表中没有列名。除了这个问题,该应用程序可以完美运行(尽管显然无法绘制)。

我已经从应用程序中剥离了以下代码,以给出一个上传数据框和使用下拉列表的示例。该代码与应用程序中显示的代码完全相同,并且在此示例中有效。为了解决相同的问题(下拉列表中缺少列名),您需要从GitHub克隆应用程序,因为我还没有找到一种方法来重现丢失的列名。

非常感谢您的帮助!

# example data to upload to the app
data <- data.frame(this = 1:10, is = 1:10, some = 1:10, great = 1:10, example = 1:10, data = 1:10)
write.table(data, "data_frame.txt", 
            row.names = FALSE, col.names = TRUE, quote = FALSE, sep = "\t")
library(shiny)
library(data.table)


ui <- fluidPage(

  sidebarPanel(
    helpText("Select paramaters for data upload then upload data. Functions based on R read.table()."),

    checkboxInput(
      inputId = 'header',
      label = 'Header',
      value = TRUE
    ),

    checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),

    radioButtons(
      inputId = 'sep',
      label = 'Separator',
      choices = c(
        Comma = ',',
        Semicolon = ';',
        Tab = '\t',
        Space = ''
      ),
      selected = '\t'
    ),

    radioButtons(
      inputId = "quote",
      label = "Quote",
      choices = c(
        None = "",
        "Double Quote" = '"',
        "Single Quote" = "'"
      ),
      selected = '"'
    ),

    fileInput(
      inputId = "file1",
      label = "Track 1",
      multiple = TRUE,
      accept = c(
        "text/csv",
        "text/comma-separated-values",
        "text/plain",
        ".csv"
      )
    ),

    selectInput("label_column",
                "Label:",
                choices="", 
                selected = "")
))


server <- shinyServer(function(input, output, session) {

# Read data from .csv/.txt files
track2_data1 <- eventReactive(input$file1, {
  rbindlist(
    lapply(
      input$file1$datapath,
      FUN = read.csv,
      header = input$header,
      sep = input$sep,
      quote = input$quote
    ),
    use.names = TRUE,
    fill = TRUE
  )
})

# drop down menu 
observeEvent(input$file1, {
  updateSelectInput(session,
                    "label_column",
                    choices = c("",colnames(track2_data1())))})
})

shinyApp(ui = ui, server = server)

sessInfo()

运行shiny::runApp()之前:

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

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.6/Resources/lib/libRlapack.dylib

locale:
  [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

loaded via a namespace (and not attached):
  [1] compiler_3.6.2 tools_3.6.2    packrat_0.5.0 

运行shiny::runApp()后:

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

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.6/Resources/lib/libRlapack.dylib

locale:
  [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

other attached packages:
  [1] markdown_1.1         rmarkdown_2.1        fs_1.3.1             stringr_1.4.0       
[5] shinycssloaders_0.3  dplyr_0.8.3          circlize_0.4.8       data.table_1.12.8   
[9] plotly_4.9.1         ggplot2_3.2.1        shinythemes_1.1.2    shinyLP_1.1.2       
[13] shinydashboard_0.7.1 shiny_1.4.0         

loaded via a namespace (and not attached):
  [1] shape_1.4.4         tidyselect_0.2.5    xfun_0.12           purrr_0.3.3         colorspace_1.4-1   
[6] vctrs_0.2.2         htmltools_0.4.0     viridisLite_0.3.0   yaml_2.2.0          rlang_0.4.3        
[11] later_1.0.0         pillar_1.4.3        glue_1.3.1          withr_2.1.2         lifecycle_0.1.0    
[16] munsell_0.5.0       gtable_0.3.0        htmlwidgets_1.5.1   GlobalOptions_0.1.1 evaluate_0.14      
[21] knitr_1.27          fastmap_1.0.1       crosstalk_1.0.0     httpuv_1.5.2        Rcpp_1.0.3         
[26] xtable_1.8-4        promises_1.1.0      scales_1.1.0        jsonlite_1.6        mime_0.8           
[31] packrat_0.5.0       digest_0.6.23       stringi_1.4.5       grid_3.6.2          tools_3.6.2        
[36] magrittr_1.5        lazyeval_0.2.2      tibble_2.1.3        crayon_1.3.4        tidyr_1.0.2        
[41] pkgconfig_2.0.3     rsconnect_0.8.16    assertthat_0.2.1    httr_1.4.1          rstudioapi_0.10    
[46] R6_2.4.1            compiler_3.6.2   

1 个答案:

答案 0 :(得分:0)

我设法通过从withSpinner()中删除ui.R来解决此问题。它位于第261-263行:

mainPanel(
          withSpinner(uiOutput("pdf"))
        ))))),

解决方法:

mainPanel((uiOutput("pdf")))
        )))),

我不太确定为什么这可以解决问题……

相关问题