2017-03-06 115 views
0

我有一个关于R的Shiny问题。我有一个函数返回一个列表与2个对象作为输出,都作为矩阵。第一个是始终创建的,并且始终可供下载。第二个,结合显示为复选框的条件。R闪亮:下载多个.csv文件

全球

physical_check <- function(file_path, x,y,z, classification) 
input: String, int, int, int, boolean 
output: list(matrix1 = result, matrix2 = rating) 

UI:

ui <- fluidPage( 
    # Application title 
    titlePanel("Review"), 

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
    sidebarPanel(
     fileInput('file1', 'Choose .txt File', 
       accept=c('text/csv','text/comma-separated,text/plain')), 
     hr(), 

     checkboxInput("classification", "Use Text-Classification", value = FALSE), 
     hr(), 

     downloadButton("download_physic", "Physical Review"), 
     br(), 
     conditionalPanel(condition = "input.classification == true", 
         downloadButton("download_classify", "Text Classification")) 

    ), 
    mainPanel(
     h3("Review"), 
     tableOutput("rating"), 
     tableOutput("result_shiny") 
    ) 
    ) 

) 

服务器:

server <- function(input, output) { 
    inFile <- reactive({ 
    input$file1 
    }) 
    result <- reactive({NULL}) 

    classify <- reactive({ 
    input$classification 
    }) 

    observe({ 
    if (!is.null(inFile())) { 
     result <- reactive({ 
     withProgress({ 
      setProgress(message = "Processing review...") 
      physical_check(as.String(inFile()$datapath),15,8,3,classify()) 
     }) 
     }) 
    } 

    output$result_shiny <- renderTable({ 
    result()$result 
    }) 
    output$rating <- renderTable({ 
    result()$rating 
    }) 

    output$download_physic <- downloadHandler(
    filename = function() { 
     sub(pattern = "\\.txt",replacement = "_result.csv",inFile()$name) 
    }, 
    content = function(file) { 
     write.table(
     result()$result, 
     file, 
     sep = ";", 
     col.names = NA, 
     qmethod = "double" 
    ) 
    } 
) 

    output$download_classify <- downloadHandler(
    filename = function() { 
     paste("rating", ".csv") 
    }, 
    content = function(file) { 
     write.table(
     result()$rating, 
     file, 
     sep = ";", 
     col.names = NA, 
     qmethod = double 
    ) 
    } 
) 
    }) 
} 
# Run the application 
shinyApp(ui = ui, server = server) 

所以你可以在我提到的文本分类的条件下载按钮的代码中看到的,如果该复选框被触发。 根据这个TRUE/FALSE值,函数physical_check被调用true或false,并返回一个矩阵和NULL或2 matrizes,只有在第二个选项中显示下载按钮。 我有点困惑,因为tableOutput我在主面板中收到正确的表格,同时下载第一个下载部分的physical review也能正常工作。 但第二个谷歌浏览器失败,下载错误:

download_classify 
Failed - Server problem 

虽然它的构造相同的方式。

回答

0

你忘了在qmethod的论点中加入引号""。你的分类下载处理程序应该是这样的:

output$download_classify <- downloadHandler(
     filename = function() { 
     paste0("rating", ".csv") 
     }, 
     content = function(file) { 
     write.table(
      result()$rating, 
      file, 
      sep = ";", 
      col.names = NA, 
      qmethod = "double" 
     ) 
     } 
    ) 
+0

OH no,请原谅我的无能!是的,解决了我的问题.....非常感谢! –