使 conditionalPanel 依赖于使用 fileInput 上传的文件 [英] Make conditionalPanel depend on files uploaded with fileInput

查看:54
本文介绍了使 conditionalPanel 依赖于使用 fileInput 上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试制作一个闪亮的应用程序,其中有一个按钮,只有在文件已上传时才会显示;为此,我使用了 conditionalPanel.

So I'm trying to make a shiny app where I have a button which only shows up if files have been uploaded; for this im using conditionalPanel.

ui.R:

require(shiny)
shinyUI(pageWithSidebar(
  headerPanel("My App"),

  sidebarPanel(
    fileInput("files", "Choose file"),
    conditionalPanel(
      condition = "input.files",
      actionButton("submitFiles", "Submit files for processing"))),

  mainPanel(h3("Nothing to see here"))
))

我认为在我的 server.R 中没有什么需要关心的,因为上面的例子没有任何事情.在上述条件下,按钮永远不会出现,即条件永远不会为真.

I don't think there's anything to care about in my server.R, since the above example doesn't do anything. With the above condition, the button never shows up, i.e. the condition is never true.

我为我的病情尝试过的一些事情是 input.files.length >0, input.files.size() >0,这两者都会导致按钮在我上传文件之前出现.我猜这是因为 input$files 在选择文件之前是一个空的 data.frame,因此长度/大小不为零,对吗?

Some things I've tried for my condition are input.files.length > 0, input.files.size() > 0, both of which result in the button being present before I upload a file. I'm guessing this is because input$files is an empty data.frame before choosing files, and so has a non-zero length/size, is that right?

在至少一个文件上传完成之前,我可以使用什么条件来隐藏按钮?

What condition can I use to hide the button until at least one file is done uploading?

我认为另一种选择是将 conditionalPanel 替换为 uiOutput,并在内部调用 renderUI({actionButton(...)})server.R 中的一个观察/隔离块正在观察 input.files (if (nrow(input$files) < 1) return());这是唯一的方法吗?如果我能以任何一种方式做到这一点,什么会让我选择一个或另一个(除了 conditionalPanel 导致更少的代码)?

I think another option would be to replace conditionalPanel with uiOutput, and call renderUI({actionButton(...)}) inside of an observe/isolate block in server.R which is watching input.files (if (nrow(input$files) < 1) return()); is that the only way? If I can do this either way, what would make me pick one or the other (beyond conditionalPanel resulting in less code)?

推荐答案

您必须制作一个响应式输出,返回上传状态,并将此输出的选项 suspendWhenHidden 设置为 FALSE.

You have to make a reactive output returning the status of the uploading and set the option suspendWhenHidden of this output to FALSE.

更准确地说,在 server.R 中,您肯定有一个响应式函数,比如 getData() 可以从上传的文件中创建数据帧.然后这样做:

More precisely, in server.R you surely have a reactive function, say getData() to make a dataframe from the uploaded file. Then do this:

  getData <- reactive({
    if(is.null(input$files)) return(NULL)
    ......
  })
  output$fileUploaded <- reactive({
    return(!is.null(getData()))
  })
  outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)

并且在 ui.R 中,您可以通过以下方式使用 conditionalPanel():

And in ui.R you can use conditionalPanel() by doing:

conditionalPanel("output.fileUploaded",
   ......

这篇关于使 conditionalPanel 依赖于使用 fileInput 上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆