如何将 RShiny reactiveFileReader 与 reactiveUI 和不存在的文件一起使用?

Posted

技术标签:

【中文标题】如何将 RShiny reactiveFileReader 与 reactiveUI 和不存在的文件一起使用?【英文标题】:How to use RShiny reactiveFileReader with reactiveUI and non-existent files? 【发布时间】:2018-12-07 20:05:06 【问题描述】:

我将如何构建一个响应式 UI,以响应具有不同数据输入的 reactiveFileReader?

我有兴趣将 reactiveFileReader 集成到一个应用程序中,该应用程序在数据中绘制组图并按组显示选定的点。

挑战:

    并非我可以通过前缀和后缀识别的每个文件都存在。 每个文件有不同数量的组。

当我

时崩溃/失败
    尝试打开一个不存在的文件。 更新文件(因此它确实检测到有更改)

可能的解决方案:

    在读取数据后减慢/延迟接下来的步骤,以便它可以重新加载数据。 通过reactive()req() 修复 isolate() 依赖 UI,因此它仅在第一次加载文件时更改组数。

我包含了模拟数据(及其生成)、一个 UI、损坏的服务器和一个没有响应式文件阅读器的工作服务器。

更新

唯一剩下的就是让renderUI“组”在文件被重新读取时不重置。通常这是一件好事,但在这里我不想要那样。

library(tidyr); library(dplyr); library(ggplot2); library(readr); library(stringr)
library(shiny)
#library(DT)

模拟数据

a1 <- structure(list(Group = c("alpha_1", "alpha_1", "alpha_2", "alpha_2", "alpha_3", "alpha_3"), Sample = c("ps_1", "ps_2", "ps_1", "ps_2", "ps_1", "ps_2"), x = c(1, 1.1, 4, 4.1, 6.8, 7), y = c(2.1, 2, 7.3, 7, 10, 9.7)), .Names = c("Group", "Sample", "x", "y"), row.names = c(NA,-6L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = structure(list(Group = structure(list(), class = c("collector_character", "collector")),Sample = structure(list(), class = c("collector_character","collector")), x = structure(list(), class = c("collector_double", "collector")), y = structure(list(), class = c("collector_double", "collector"))), .Names = c("Group", "Sample", "x", "y")), default = structure(list(), class = c("collector_guess", "collector"))), .Names = c("cols", "default"), class = "col_spec"))
a2 <- structure(list(Group = c("alpha_6", "alpha_6", "alpha_7", "alpha_7", "alpha_9", "alpha_9", "alpha_10", "alpha_10"), Sample = c("ps_1", "ps_2", "ps_1", "ps_2", "ps_1", "ps_2", "ps_1", "ps_2"), x = c(3,3.2, 5, 5.1, 1, 1.1, 5, 5.1), y = c(8.1, 7, 3, 4, 14, 15, 4,3)), .Names = c("Group", "Sample", "x", "y"), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = structure(list(Group = structure(list(), class = c("collector_character", "collector")), Sample = structure(list(), class = c("collector_character","collector")), x = structure(list(), class = c("collector_double", "collector")), y = structure(list(), class = c("collector_double", "collector"))), .Names = c("Group", "Sample", "x", "y")), default = structure(list(), class = c("collector_guess", "collector"))), .Names = c("cols", "default"), class = "col_spec"))
b2 <- structure(list(Group = c("beta_3", "beta_3", "beta_4", "beta_4", "beta_6", "beta_6"), Sample = c("ps_1", "ps_2", "ps_1", "ps_2", "ps_1", "ps_2"), x = c(3, 3.2, 5, 5.1, 1, 1.1), y = c(8.1, 7, 3, 4, 14, 15)),.Names = c("Group", "Sample", "x", "y"), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = structure(list(Group = structure(list(), class = c("collector_character", "collector")), Sample = structure(list(), class = c("collector_character","collector")), x = structure(list(), class = c("collector_double", "collector")), y = structure(list(), class = c("collector_double", "collector"))), .Names = c("Group", "Sample", "x", "y")), default = structure(list(), class = c("collector_guess", "collector"))), .Names = c("cols", "default"), class = "col_spec"))
b3 <- structure(list(Group = c("beta_3", "beta_3", "beta_4", "beta_4", "beta_6", "beta_6"), Sample = c("ps_1", "ps_2", "ps_1", "ps_2", "ps_1", "ps_2"), x = c(3, 3.2, 5, 5.1, 1, 1.1), y = c(8.1, 7, 3, 4, 14, 15)), .Names = c("Group", "Sample", "x", "y"), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = structure(list(Group = structure(list(), class = c("collector_character", "collector")), Sample = structure(list(), class = c("collector_character","collector")), x = structure(list(), class = c("collector_double", "collector")), y = structure(list(), class = c("collector_double", "collector"))), .Names = c("Group", "Sample", "x", "y")), default = structure(list(), class = c("collector_guess", "collector"))), .Names = c("cols", "default"), class = "col_spec"))

# Data export to simulate the problem
lz_write <- function(input) 
  write_csv(input, paste0(substitute(input), ".csv"))

lz_write(a1); lz_write(a2); lz_write(b2); lz_write(b3) # Messed up function for lapply...
# rm(list = ls()) # Clean the environment

用户界面

ui <- fluidPage(
  titlePanel("Minimal Example"),
  fluidRow(
    column(width = 2, class = "well", 
           # File selection
           html(paste("Which file?")),
           # Prefix:
           selectInput(inputId = "p",
                       label = "Prefix:",
                       choices = c("a", "b", "c"),  
                       selected = "a"), 

           # Suffix:
           numericInput(inputId = "s",
                        label = "Suffix:",
                        min = 1,
                        max = 3,
                        value = 1,
                        step = 1)), 
    column(width = 10,
           plotOutput(outputId = "scatterplot",
                      dblclick = "plot_dblclick",  # Might not be necessary, but it's not more work to include but more work to exclude
                      brush = brushOpts(id =  "plot_brush", resetOnNew = TRUE)))
    ), 
  fluidRow(
    column(width = 3,
           br(),
           uiOutput(outputId = "group_n")), 
    column(width = 9, 
           fixedRow( 
             column(width = 3,
                    HTML(paste0("Arg 1"))),
             column(width = 3,
                    HTML(paste0("Arg 2"))),
             column(width = 3,
                    uiOutput(outputId = "num_2"))
             )
    )
  ),
  fluidRow(
    br(), br(), br(), #Lets add some gaps or spacing
    DT::dataTableOutput(outputId = "Table")) # Summary table
)  # Not sure if actually necessary for this example

服务器损坏 现在唯一的问题是用户界面会在重新读取文件时重置...

server_broken <- function(input, output, session)  # Broken version

  #Larger subset: A Reactive Expression # May be used later...
  args <- reactive(
    list(input$p, input$s)  #which file do we wish to input. This was our tag
  )
  # Reactive File-reader Subset
  path <- reactive(
    paste0(input$p, input$s, ".csv")
  ) # Reactive Filename, kinda like our args... 



  filereader <- function(input)  # The function we pass into a reactive filereader. 
    suppressWarnings(read_csv(input, col_types = cols(
      Group = col_character(),
      Sample = col_character(),
      x = col_double(),
      y = col_double())
    ))
  

  ##BROKEN REACTIVE FILE READER HERE##
  data_1 <- reactiveValues() # The function we use for livestream data
  observe(
    if(file.exists(path()) == TRUE) 
      fileReaderData <-  reactiveFileReader(500, session, path(), filereader) 
      else  
      message("This file does not exist") 
    ## OR DO I DO SOMETHING ELSE HERE??##
    
    data_1$df <- reactive( 
     ## STOPS APP CRASHING, BUT NO LONGER REFRESHES CONSTANTLY ##
      req(fileReaderData()) 
      fileReaderData()
    )   
  ) # Honestly don't understand still

  data <- reactive(data_1$df()) # Pulling things out just so the rest of our code can stay the same. 

  ## END OF BROKEN FILE READER##
  ## Reactive UI HERE##
  data_m <- reactive(
    req(data()) 
    args()
    tmp <- isolate(select(data(), Group))
    tmp %>% distinct()
  ) # number of groups

  output$num_2 <- renderUI(
    req(data())
    numericInput(inputId = "n",
                 label = "Group:",
                 min = 1,
                 max = length(data_m()$Group), 
                 value = 1 
    )
  ) #This is our 'reactive' numeric input for groups. This caps the max of our function based on the number of groups there are per file

  n <- reactive(input$n) #which marker number we are dealing with. 
  ## End of reactive UI##
  data_n <- reactive(
    req(data()); req(data_m())
    dt <- filter(data(), Group == data_m()[[1]][input$n])
  ) 


  # Create scatterplot object the plotOutput function is expecting ----
  ranges <- reactiveValues(x = NULL, y = NULL)


  output$scatterplot <- renderPlot(
    validate(need(data(), "The specified file does not exist. Please try another"))
    p <- as.numeric(input$p)
    plot <- ggplot(data_n(), aes(x, y)) +  
      labs(title = paste0("Group ", data_n()$Group[1])) + 
      labs(x = "X vals", y = "Y vals") + 
      geom_point() + theme_bw() # I already have customized aesthetics. Removed for minimalism  
    plot + coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = TRUE) # So we see all points more readily. messes up the zoom but oh well
  )

  # When a double-click happens, check if there's a brush on the plot.
  # If so, zoom to the brush bounds; if not, reset the zoom.
  observeEvent(input$plot_dblclick, 
    brush <- input$plot_brush
    if (!is.null(brush)) 
      ranges$x <- c(brush$xmin, brush$xmax)
      ranges$y <- c(brush$ymin, brush$ymax)
     else 
      ranges$x <- NULL
      ranges$y <- NULL
    
  )  


  #Creating text ----
  output$group_n <- renderText(
    req(data())
    paste0("There are ", length(data_m()$Group), " groups in this file.",
           tags$br("This is Group: ", data_m()$Group[n()])
    )
  ) 

  #Building a table for you to visibly see points. You may need to update the DT to the github version ----
  output$Table <- DT::renderDataTable(
    req(data())
    brushedPoints(data_n(), brush = input$plot_brush) %>%
      select(Sample) 
  )


功能服务器

它已被删除,因为损坏的至少不会崩溃,并且问题很明显。查看之前对原件的修改。

咨询的来源

Interactive file input and reactive reading in shinyapp https://github.com/rstudio/shiny/issues/167 在isolateobservers 上寻找灵感 https://groups.google.com/forum/#!topic/shiny-discuss/QgdUfWGsuVU

会话信息

R 版本 3.4.2 (2017-09-28) 平台:x86_64-w64-mingw32/x64(64 位) 运行于:Windows 7 x64(内部版本 7601)Service Pack 1

更新

Observe() 中放置一个响应式可以阻止应用程序崩溃,并且它确实会更新文件(忘记删除一些内容)。剩下的就是将依赖的 UI 保存在某个地方......

【问题讨论】:

【参考方案1】:

简而言之,问题是由于没有正确理解observers 的逻辑,在反应后缺少(),并且没有调用req 来阻止某些部分重新执行(参见HERE)。

具体的逐行更新可以通过查找下面的##CHANGE: 来找到...最重要的变化(不分先后)是:

    isolate() 用于renderUIrenderUI 中使用req() 来减慢它的速度,并且在组数有更新之前不运行,但调用args() 使其依赖于文件选择 预计算renderUI 之外的组数

更新的服务器

server_fixed <- function(input, output, session)  

  #Larger subset: A Reactive Expression # May be used later...
  args <- reactive(
    list(input$p, input$s)  #which file do we wish to input. This was our tag
  )
  # Reactive File-reader Subset
  path <- reactive(
    paste0(input$p, input$s, ".csv")
  ) # Reactive Filename, kinda like our args... 



  filereader <- function(input)  # The function we pass into a reactive filereader. 
    suppressWarnings(read_csv(input, col_types = cols(
      Group = col_character(),
      Sample = col_character(),
      x = col_double(),
      y = col_double())
    ))
  

  data_1 <- reactiveValues() # The function we use for livestream data
  observe(
    if(file.exists(path()) == TRUE) 
      fileReaderData <-  reactiveFileReader(500, session, path(), filereader) 
      else  
      message("This file does not exist")
    
    data_1$df <- reactive(
      # if(exists(fileReaderData())) 
      #   fileReaderData()
      #  # Crashed from the beginning
      req(fileReaderData()) 
      fileReaderData()
    )   
  ) 

  data <- reactive(data_1$df()) ##CHANGE: FORGOT THE ()##

  # Group setting...
  data_m <- reactive(
    req(data()) 
    args()
    tmp <- isolate(select(data(), Group))
    tmp %>% distinct()
  ) #number of markers, keeping only the marker name

  data_m_length <- reactive( ##CHANGE: TOOK OUT OF output$num_2## 
  ##CHANGE: ADDED AN ISOLATE to fix the # of groups per file ##

    isolate(length(data_m()$Group))
  )

  output$num_2 <- renderUI(
    req(data_m_length()) ## CHANGE: ONLY EXECUTE ONCE WE HAVE OUR isolated data_m_length##
    args() ## CHANGE: DEPENDENT UPON changing files##
    isolate(
    numericInput(inputId = "n",
                 label = "Group:",
                 min = 1,
                 max = data_m_length(), 
                 value = 1 # THIS SHOULD BE CACHED! 
    )) ##CHANGE: ADDED IT IN ISOLATE when testing. NOT SURE IF STILL NEEDED##
  ) #This is our 'reactive' numeric input for groups. This caps the max of our function based on the number of groups there are per file

  n <- reactive(input$n) #which marker number we are dealing with. 

  data_n <- reactive(
    req(data()); req(data_m())
    dt <- filter(data(), Group == data_m()[[1]][n()])
  ) 


  # Create scatterplot object the plotOutput function is expecting ----
  ranges <- reactiveValues(x = NULL, y = NULL)


  output$scatterplot <- renderPlot(
    validate(need(data(), "The specified file does not exist. Please try another"))
    p <- as.numeric(input$p)
    plot <- ggplot(data_n(), aes(x, y)) +  
      labs(title = paste0("Group ", data_n()$Group[1])) + 
      labs(x = "X vals", y = "Y vals") + 
      geom_point() + theme_bw() # I already have customized aesthetics. Removed for minimalism  
    plot + coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = TRUE) # So we see all points more readily. messes up the zoom but oh well
  )

  # When a double-click happens, check if there's a brush on the plot.
  # If so, zoom to the brush bounds; if not, reset the zoom.
  observeEvent(input$plot_dblclick, 
    brush <- input$plot_brush
    if (!is.null(brush)) 
      ranges$x <- c(brush$xmin, brush$xmax)
      ranges$y <- c(brush$ymin, brush$ymax)
     else 
      ranges$x <- NULL
      ranges$y <- NULL
    
  )  


  #Creating text ----
  output$group_n <- renderText(
    req(data())
    paste0("There are ", length(data_m()$Group), " groups in this file.",
           tags$br("This is Group: ", data_m()$Group[n()])
    )
  ) 

  #Building a table for you to visibly see points. You may need to update the DT to the github version ----
  output$Table <- DT::renderDataTable(
    req(data())
    brushedPoints(data_n(), brush = input$plot_brush) %>%
      select(Sample) 
  )


剩下的就是适当地使用suppressErrorvalidate

【讨论】:

以上是关于如何将 RShiny reactiveFileReader 与 reactiveUI 和不存在的文件一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

R Shiny:如何将数据表添加到动态创建的选项卡

R Shiny:如何创建“添加字段”按钮

wellPanel Rshiny 在滚动时被输入选项切断

R Shiny:如何使幻灯片居中

R Shiny:如何构建动态 UI(文本输入)

如何在 R Shiny 中创建具有复杂标题的数据表?