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

  • 本文关键字:APP 下载 输出 shiny
  • 更新时间 :
  • 英文 :


我想下载我制作的这个应用程序的输出,但有一个错误,当我打开下载的数据时它是空的。我通过 output$other_val_show 制作了一个数据集,我想下载它。有什么建议吗?

以下代码用于 UI 部分。

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"))
">

服务器"部分的以下代码(我想下载"gr"的输出,我想通过downloadHandler函数下载它。

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)

如果没有最小的可重现示例,很难完全回答这个问题,但这是我会尝试的:

  1. renderPrint之外创建gr
  2. downloadHandler中使用gr()
  3. 重写downloadHandler以包含contentfilename参数

下面是一个与您的应用程序具有相同逻辑的最小示例,即创建一个既可以打印(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)

您可以在该renderPrint函数的范围内定义gr,因此它不可用于downloadHandler。您应该将 gr 定义为该函数之外的某个反应值。这样,当您在 renderPrint 函数中分配它时,它的整个程序范围都可以访问它。

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

最新更新