1

I want to download the output of this App which I made but there is an error and when I open the downloaded data it is empty.I make a data set by output$other_val_show and I want to download it. Any advice?

The following code in for the UI section.

library(shiny)
library(quantreg)
library(quantregGrowth)
library(plotly)
library(rsconnect)
library(ggplot2)
library(lattice)

    ui = tagList(
      tags$head(tags$style(HTML("body{ background: aliceblue; }"))),
      navbarPage(title="",
                 tabPanel("Data Import",
                          sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                                                      tags$hr(),
                                                      h5(helpText("Select the read.table parameters below")),
                                                      checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                                      checkboxInput(inputId = "stringAsFactors", "StringAsFactors", FALSE),
                                                      radioButtons (inputId = 'sep', label = 'Separator', 
                                                                    choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
                          ),
                          mainPanel(uiOutput("tb1"))
                          )),
                 tabPanel("Interval",
                          sidebarLayout(sidebarPanel(
                            uiOutput("model_select"),
                            uiOutput("var1_select"),
                            uiOutput("rest_var_select"),
                            #uiOutput("testText1"), br(),
                            #textInput("Smooting Parameter min value", "Smooting Parameter max value", value = "")                        
                            sliderInput("range", "Smooth Parameter range:",min = 0, max = 1000, value = c(0,100)),
                            downloadButton('downloadData', 'Download')
                          ),
                          mainPanel(helpText("Selected variables and Fitted values"),
                                    verbatimTextOutput("other_val_show")))),
                 tabPanel("Model Summary", verbatimTextOutput("summary")), 
                 tabPanel("Scatterplot", plotOutput("scatterplot"))#, # Plot
                 #tabPanel("Distribution", # Plots of distributions
                 #fluidRow(
                 #column(6, plotOutput("distribution1")),
                 #column(6, plotOutput("distribution2")))
                 #)             
                 ,inverse = TRUE,position="static-top",theme ="bootstrap.css"))

The following code in for the Server section. (I want to download the output which is "gr" and I want to download it by downloadHandler function.

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)
stat
  • 65
  • 7

2 Answers2

2

It's hard to fully answer this without a minimal reproducibile example, but here's what I would try:

  1. Create gr outside of renderPrint
  2. Use gr() in downloadHandler
  3. Rewrite downloadHandler to include content and filename arguments

Here's a minimal example with the same logic as your app, i.e. create a reactive dataframe which is both printed (renderPrint) and downloadable (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)
Hallie Swan
  • 2,714
  • 1
  • 15
  • 23
0

You define gr inside the scope of that renderPrint function so it isn't available to downloadHandler. You should define gr as a reactive value somewhere outside that function. That way, when you assign it in the renderPrint function, it will be accessible to the entire scope of your program.

In the future, it would be helpful to provide the text of any error messages you get - they are often quite helpful to solving problems.

divibisan
  • 11,659
  • 11
  • 40
  • 58