从Shiny APP下载输出(需要一些建议)

时间:2018-02-13 20:27:23

标签: shiny

我想下载此应用程序的输出,但是有一个错误,当我打开下载的数据时它是空的。我通过输出$ other_val_show进行数据设置,我想下载它。有什么建议吗?

UI部分的以下代码。

downloadHandler

“服务器”部分的以下代码。 (我想下载" gr"的输出,我想通过{{1}下载它功能。

server<-function(input,output) { 
  data <- reactive({
    lower <- input$range[1]
    upper <- input$range[2]
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

  })  
  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })
  output$tb1 <- renderUI({
    tableOutput("table")
  })
  #output$model_select<-renderUI({
  #selectInput("modelselect","Select Algo",choices = c("Reference Interval"="Model")) 
  #})
  output$var1_select<-renderUI({
    selectInput("ind_var_select","Select Independent Variable", choices =as.list(names(data())),multiple = FALSE)
  })
  output$rest_var_select<-renderUI({
    checkboxGroupInput("other_var_select","Select Dependent Variable",choices =as.list(names(data()))) #Select other Var
  })
  output$other_val_show<-renderPrint({
    input$other_var_select
    input$ind_var_select
    f<-data()
    lower <- input$range[1]
    upper <- input$range[2]
    library(caret)
    library(quantregGrowth)
    dep_vars    <- paste0(input$ind_var_select, collapse = "+")
    after_tilde <- paste0("ps(", dep_vars, ", lambda = seq(",lower,",",upper,",l=100))")
    dyn_string  <- paste0(input$other_var_select, " ~ ", after_tilde)
    Model<-quantregGrowth::gcrq(as.formula(dyn_string),tau=c(0.025,0.975), data=f)
    temp <- data.frame(Model$fitted)
    gr <- cbind(f, temp)
    print(gr)
  })

output$downloadData <- downloadHandler(    
  write.csv(gr, file, row.names = FALSE)
  )
}
shinyApp(ui=ui,server=server)

2 个答案:

答案 0 :(得分:2)

如果没有minimal reproducibile example,很难完全回答这个问题,但这就是我要尝试的内容:

  1. gr
  2. 之外创建renderPrint
  3. gr()
  4. 中使用downloadHandler
  5. 重写downloadHandler以包含contentfilename参数
  6. 这是一个与您的应用程序具有相同逻辑的最小示例,即创建一个被打印(renderPrint)和可下载(downloadHandler)的被动数据框。

    library(shiny)
    ui <- navbarPage(title = "Example",
                     tabPanel("First",
                              selectInput("fruit", "Fruit", c("apple", "orange", "pear")),
                              h4("Output from renderPrint:"),
                              textOutput("other_val_show"),
                              h4("Download Button: "),
                              downloadButton("downloadData")))
    server <- function(input, output) {
        gr <- reactive({
            data.frame(fruit = input$fruit)
        })
        output$other_val_show <- renderPrint({
            print(gr())
        })
        output$downloadData <- downloadHandler(
            filename = "example.csv", 
            content = function(file) {
                write.csv(gr(), file)
            })
    }
    shinyApp(ui, server)
    

答案 1 :(得分:0)

您在该renderPrint函数的范围内定义gr,因此无法下载Handler。您应该将gr定义为该函数之外的某个无效值。这样,当您在renderPrint函数中分配它时,程序的整个范围都可以访问它。

将来,提供您收到的任何错误消息的文本会很有帮助 - 它们通常对解决问题很有帮助。