Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:单击按钮上载文件_R_Upload_Action_Shiny - Fatal编程技术网

R:单击按钮上载文件

R:单击按钮上载文件,r,upload,action,shiny,R,Upload,Action,Shiny,我知道网络上已经有很多资料可以回答我的问题,但它们似乎都不适合我。我想那是因为我不太理解Shiny的反应式编程 因此,我希望创建一个界面,允许用户使用fileInput选择一个文件,并仅在单击上载按钮时上载它。我在不同的论坛上尝试了一些解决方案,但都没有成功。以下是我的最新尝试: #ui.R library(shiny) shinyUI(pageWithSidebar( headerPanel(""), sidebarPanel( fileIn

我知道网络上已经有很多资料可以回答我的问题,但它们似乎都不适合我。我想那是因为我不太理解Shiny的反应式编程

因此,我希望创建一个界面,允许用户使用fileInput选择一个文件,并仅在单击上载按钮时上载它。我在不同的论坛上尝试了一些解决方案,但都没有成功。以下是我的最新尝试:

#ui.R

library(shiny)

shinyUI(pageWithSidebar(


    headerPanel(""),

    sidebarPanel(

            fileInput("in_file", "Input file:",
                    accept=c("txt/csv", "text/comma-separated-values,text/plain", ".csv")),
            checkboxInput(inputId="is_header", label="Does the input file have column names?", value=TRUE),
            actionButton("upload_data", "Upload Data"),
    ),
    mainPanel(
        tabsetPanel(
                    tabPanel("Original Data", tableOutput("orig_data"))
        )
    )
))


#server.R

library(shiny)

shinyServer(function(input, output, session) {

    ra_dec_data <- reactive({
            if(input$upload_data==0)
                    return(NULL)

            return(isolate({
                    head(read_data(input$in_file$datapath, input$in_file$is_header), 50)
            }))
    })

    output$orig_data <- renderTable({
        ra_dec_data()
    })
})
我面临的问题是,文件一旦被选中就会被上传,而上传按钮没有响应

我的猜测是,我所做的没有意义,所以请接受我的道歉,因为我做得太糟糕了。任何帮助都将不胜感激。谢谢

fileInput直接上传文件,因此我建议您创建自己的fileInput

以下是我将采取的行动:

服务器.R

library(shiny)

shinyServer(function(input, output, session) {

  observe({

    if (input$browse == 0) return()

    updateTextInput(session, "path",  value = file.choose())
  })

  contentInput <- reactive({ 

    if(input$upload == 0) return()

    isolate({
      writeLines(paste(readLines(input$path), collapse = "\n"))
    })
  })

  output$content <- renderPrint({
    contentInput()
  })

})
在Server.R中,我们首先在每次单击操作按钮浏览时更新文本输入的值

contentInput是一个反应式函数,当函数体中包含的输入值发生更改时,它将被重新执行,此处输入$upload,而不是当输入$path发生更改时,因为我们将其隔离。如果我们没有隔离包含input$path的部分,那么每次浏览新文件时都会重新执行contentInput,然后这里的upload按钮将无效

然后我们在output$content中返回contentInput的结果

希望这有帮助

编辑: 我意识到,如果您取消文件选择,它会产生错误和闪亮的应用程序崩溃,那么您应该使用Henrik Bengtsson提供的此功能:


请务必注意,file.choose仅在本地运行闪亮应用程序时才起作用。我不知道你所说的“直接上传文件”是什么意思。OP希望使用fileInput选择一个文件,并仅在单击“上传”按钮时上传它。fileInput不会等待用户操作来上传数据。您的read_data功能在哪里?您查看了吗?试着运行shiny::runExample09_upload如果这样做有效,请查看并尝试根据您的需要调整它。
library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Example"),

  sidebarPanel(
    textInput("path", "File:"),
    actionButton("browse", "Browse"),
    tags$br(),
    actionButton("upload", "Upload Data")
  ),

  mainPanel(
    verbatimTextOutput('content')
  )

))
file.choose2 <- function(...) {
  pathname <- NULL;
  tryCatch({
    pathname <- file.choose();
  }, error = function(ex) {
  })
  pathname;
}