为了通过最小示例来说明我的问题,让我们考虑下面的代码(这是一个完全可重现的示例).

library(shiny)

ui <- fluidPage(
  fileInput("upload", NULL, accept = c(".csv", ".tsv")),
  numericInput("n", "Rows", value = 5, min = 1, step = 1),
  tableOutput("head"),
  fileInput("upload2", NULL, accept = c(".csv", ".tsv")),
  numericInput("n2", "Rows", value = 5, min = 1, step = 1),
  tableOutput("head2")
)

server <- function(input, output, session) {
  data <- reactive({
    req(input$upload)
    
    ext <- tools::file_ext(input$upload$name)
    switch(ext,
           csv = vroom::vroom(input$upload$datapath, delim = ","),
           tsv = vroom::vroom(input$upload$datapath, delim = "\t"),
           validate("Invalid file; Please upload a .csv or .tsv file")
    )
  })
  
  output$head <- renderTable({
    head(data(), input$n)
  })
  
  data2 <- reactive({
    
    req(input$upload2)
    
    ext <- tools::file_ext(input$upload2$name)
    switch(ext,
           csv = vroom::vroom(input$upload2$datapath, delim = ","),
           tsv = vroom::vroom(input$upload2$datapath, delim = "\t"),
           validate("Invalid file; Please upload a .csv or .tsv file")
    )
  })
  
  output$head2 <- renderTable({
    tail(data2(), input$n2)
  })
  
}

shinyApp(ui, server)

该应用程序要求用户上传一个数据集,然后询问行数并显示head()个上传数据.如果用户上传另一个数据集,同样会显示它的tail.

现在我的要求是,每当shiny 的应用程序用户上传data2来显示底部行时,顶部/头部行的data应该与其head信息一起从环境中删除;反之亦然.

我try 了很多方法,比如使用observeEventeventReactive等,但我做不到.请提个建议.

推荐答案

也许这就是你想要的:

library(shiny)

ui <- fluidPage(
  fileInput("upload", NULL, accept = c(".csv", ".tsv")),
  numericInput("n", "Rows", value = 5, min = 1, step = 1),
  tableOutput("head"),
  fileInput("upload2", NULL, accept = c(".csv", ".tsv")),
  numericInput("n2", "Rows", value = 5, min = 1, step = 1),
  tableOutput("head2")
)

server <- function(input, output, session) {
  data <- reactiveVal(NULL)
  data2 <- reactiveVal(NULL)
  
  observeEvent(input$upload, {
    ext <- tools::file_ext(input$upload$name)
    new_data <- switch(ext,
                       csv = vroom::vroom(input$upload$datapath, delim = ","),
                       tsv = vroom::vroom(input$upload$datapath, delim = "\t"),
                       validate("Invalid file; Please upload a .csv or .tsv file")
    )
    data(new_data)
    data2(NULL)  
  })
  
  observeEvent(input$upload2, {
    ext <- tools::file_ext(input$upload2$name)
    new_data <- switch(ext,
                       csv = vroom::vroom(input$upload2$datapath, delim = ","),
                       tsv = vroom::vroom(input$upload2$datapath, delim = "\t"),
                       validate("Invalid file; Please upload a .csv or .tsv file")
    )
    data2(new_data)
    data(NULL)
  })
  
  output$head <- renderTable({
    req(data())
    head(data(), input$n)
  })
  
  output$head2 <- renderTable({
    req(data2())
    tail(data2(), input$n2)
  })
}

shinyApp(ui, server)

R相关问答推荐

按块将载体转换为矩阵-reshape

在ggplot的注释表格中突出显示最大值

无法在我的情节中表现出显着的差异

工作流程_set带有Dplyrr风格的 Select 器,用于 Select 结果和预测因子R

MCMC和零事件二元逻辑回归

大规模重新标记haven标签数据

使用tidyverse方法绑定行并从一组管道列表执行左连接

基于不同组的列的相关性

如何在RMarkdown LaTex PDF输出中包含英语和阿拉伯语?

如何在编辑列时更新可编辑数据表,并使用该表在Shiny中执行连续计算

如何改变x轴比例的列在面

在ggplot2中更改小提琴情节的顺序

合并后返回列表的数据帧列表

您是否可以使用facet_rap设置一个较低的限制来对ggmap上的比例中断进行zoom ?

从非重叠(非滚动)周期中的最新数据向后开窗并在周期内计数

仅在R中的数据集开始和结束时删除所有 Select 列的具有NA的行

安全地测试文件是否通过R打开

如何计算每12行的平均数?

是否从列中删除★符号?

如何在一种 colored颜色 中设置数值变量的 colored颜色 和高于阈值的 colored颜色 点?