在R中下载反应表的输出
我有一个R Shining应用程序,它从用户处获取.csv导入,并在内置数据框中搜索导入的数据,然后在输出中给出%匹配。UI非常简单,只有几个不同的输入(import.csv、滑块和一些单选按钮)。我想要的是能够获取反应表输出并将其打印到.csv,用户可以下载到他们的机器上。应用程序的服务器端如下所示:在R中下载反应表的输出,r,shiny,R,Shiny,我有一个R Shining应用程序,它从用户处获取.csv导入,并在内置数据框中搜索导入的数据,然后在输出中给出%匹配。UI非常简单,只有几个不同的输入(import.csv、滑块和一些单选按钮)。我想要的是能够获取反应表输出并将其打印到.csv,用户可以下载到他们的机器上。应用程序的服务器端如下所示: server <- function(input, output){ rvals <- reactiveValues() observeEvent(input$fi
server <- function(input, output){
rvals <- reactiveValues()
observeEvent(input$file_1,{
req(input$file_1)
rvals$csv <<- read.csv(input$file_1$datapath, header = TRUE)
#some data processing here
})
output$contents <- renderTable({
if(input$select == 1){
x <- function
}else if(input$select == 2){
x <- function
}else if(input$select == 3){x <- function}
#some more data processing and formatting here
return(x)
},digits = 4)
}
server在renderable
表达式内部创建的对象在其外部不可用。相反,您可以将其分配给您设置的反应值。下面是一个工作示例(请注意,我已尝试复制您的代码,以便在您单击“上载CSV”(此处仅调用mtcars
)之前,数据将不可用)
库(闪亮)
ui=fluidPage(
侧栏面板(
actionButton(inputId=“uploadCsv”,label=“Upload CSV:”,icon=icon(“Upload”),
选择输入(inputId=“preProc”,label=“Pre processing”,choices=c(“平均值”=1,“总和”=2)),
下载按钮(“下载数据”,label=“下载表”)
),
主面板(
h4(“我的桌子:”),
表格输出(“内容”)
)
)
服务器EventReactive已经输出了一个reactive值,您不需要创建额外的reactiveVal,请参见下面的示例:
library(shiny)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("Test"),
mainPanel(
actionButton("show", "Download"),
textOutput("result")
)
)
server <- function(input, output) {
csvfile <- eventReactive(req(input$show), ignoreNULL = T, {
"Content of file"
})
output$result <- reactive(
paste("result : ",csvfile()))
}
# Run the application
shinyApp(ui = ui, server = server)
库(闪亮)
#定义用户界面
ui你试过DT软件包吗?是的,我试过DT软件包,当我使用它时,我仍然遇到完全相同的问题,它不会作为.csv下载,而是抛出一个错误。我尝试不使用DT包,因为我不喜欢输出的外观。您可以在DT包中乱用多个设置,使表看起来与您希望的完全一样。我的最佳猜测是,您的x甚至不是一个数据帧,或者您正在混乱文件名的构造。您是否遵循了中提供的示例?如果您通过reprex,我可以进一步帮助您
library(shiny)
ui = fluidPage(
sidebarPanel(
actionButton(inputId = "uploadCsv", label = "Upload CSV:", icon = icon("upload")),
selectInput(inputId = "preProc", label = "Pre-processing", choices = c("Mean"=1,"Sum"=2)),
downloadButton("downloadData", label = "Download table")
),
mainPanel(
h4("My table:"),
tableOutput("contents")
)
)
server <- function(input, output) {
rvals <- reactiveValues(
csv=NULL,
x=NULL
)
observeEvent(input$uploadCsv,{
rvals$csv <- mtcars # using example data since I don't have your .csv
# rvals$csv <- read.csv(input$file_1$datapath, header = TRUE)
#some data processing here
})
output$contents <- renderTable({
# Assuing the below are functions applied to your data
req(
input$preProc,
!is.null(rvals$csv)
)
if(input$preProc == 1){
rvals$x <- data.frame(t(colMeans(mtcars)))
}else {
rvals$x <- data.frame(t(colSums(mtcars)))
}
return(rvals$x)
},digits = 4)
output$downloadData <- downloadHandler(
filename = "myFile.csv",
content = function(file){
write.csv(rvals$x, file)
}
)
}
shinyApp(ui,server)
library(shiny)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("Test"),
mainPanel(
actionButton("show", "Download"),
textOutput("result")
)
)
server <- function(input, output) {
csvfile <- eventReactive(req(input$show), ignoreNULL = T, {
"Content of file"
})
output$result <- reactive(
paste("result : ",csvfile()))
}
# Run the application
shinyApp(ui = ui, server = server)