R 使用输入作为数据框名称部分,在Shining中下载csv
现有的R 使用输入作为数据框名称部分,在Shining中下载csv,r,shiny,R,Shiny,现有的数据.帧称为:活动的
数据.帧
称为:活动的
,活动的
,不活动的
,不活动的
。它们都包含许多行和列。我用Shiny创建了一个代码,用户可以通过单选按钮选择是否要下载active或inactive、old或new。当他们点击下载时,需要下载一个csv文件。csv应该是数据帧的内容,例如用户选择Active和new,然后名为Active\u acc\u newacc
的数据帧应该作为csv下载,另一个例子是,如果他们选择Inactive和old,则名为Inactive\u acc\u oldacc
的数据帧应该作为csv下载
在本例中,active\u acc\u newacc
包含以下示例数据:
structure(list(Alpha = c(0.192491906485068, -1.44670180633351,
-0.323180534047634, 1.62229611652493, -0.689024123596357, 2.04212222261495,
0.94377911190294, 2.0819268787991, 1.91711727878331, -0.414812239592928
), Beta = c(1.03285349943413, -1.67856959219527, 0.157549690345431,
1.48913611644558, -0.0757895625491196, 1.27178094415894, 0.641673407672177,
0.800761254937157, 1.86265922566283, -0.545356026768875), Gamma = c(1.52068837343838,
-3.61004798325456, -1.35857038834863, 3.48938862108709, -3.05109504225968,
6.5047022366346, 2.50727319977214, 5.31673927920108, 3.69096202696173,
-1.03802874828505)), row.names = c(NA, -10L), class = "data.frame")
我在ui.R中有以下内容,请求输入:
ui = fluidPage(
titlePanel("Account classification"),
sidebarLayout(
sidebarPanel(
radioButtons("account_status","Select account status", choices=c("Active","Inactive","Include both"),selected = "Active"),
br(),
radioButtons("account_age","Select account creation time", choices=c("old","new","Created any time"),selected = "new")
),
mainPanel(
downloadButton('downloadData', 'Download')
)
)
)
我创建了以下服务器。R
server = function(input, output) {
datasetInput <- reactive({
switch(input$account_status,
"Active" = active_acc,
"Inactive" = inactive_acc,
"Include both" = NULL)
switch(input$account_age,
"old" = oldacc,
"new" = newacc,
"Created any time" = all)
})
dfname <- reactive({
paste(input$account_status,input$account_age, sep='_')
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$dataset, '.csv', sep='') },
content = function(file) {
write.csv(dfname(), file)
}
)
}
shinyApp(ui, server)
server=功能(输入、输出){
datasetInput这个怎么样:
Activeacc_newacc <- structure(list(Alpha = c(0.192491906485068, -1.44670180633351,
-0.323180534047634, 1.62229611652493, -0.689024123596357, 2.04212222261495,
0.94377911190294, 2.0819268787991, 1.91711727878331, -0.414812239592928
), Beta = c(1.03285349943413, -1.67856959219527, 0.157549690345431,
1.48913611644558, -0.0757895625491196, 1.27178094415894, 0.641673407672177,
0.800761254937157, 1.86265922566283, -0.545356026768875), Gamma = c(1.52068837343838,
-3.61004798325456, -1.35857038834863, 3.48938862108709, -3.05109504225968,
6.5047022366346, 2.50727319977214, 5.31673927920108, 3.69096202696173,
-1.03802874828505)), row.names = c(NA, -10L), class = "data.frame")
ui = fluidPage(
titlePanel("Account classification"),
sidebarLayout(
sidebarPanel(
radioButtons("account_status","Select account status", choices=c("Active","Inactive","Include both"),selected = "Active"),
br(),
radioButtons("account_age","Select account creation time", choices=c("old","new","Created any time"),selected = "new")
),
mainPanel(
downloadButton('downloadData', 'Download')
)
)
)
server = function(input, output) {
datasetInput <- reactive({
switch(input$account_status,
"Active" = active_acc,
"Inactive" = inactive_acc,
"Include both" = NULL)
switch(input$account_age,
"old" = oldacc,
"new" = newacc,
"Created any time" = all)
})
dfname <- reactive({
test <- paste0(input$account_status, "acc_", input$account_age, "acc", sep='')
get(test)
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$dataset, '.csv', sep='') },
content = function(file) {
write.csv(dfname(), file)
}
)
}
shinyApp(ui, server)
Activeacc\u newacc一旦纠正了输入错误和缺少的括号,应用程序就可以运行了。“它不工作”也不是很具体。你能帮我具体说明一下,我把括号放错了什么地方吗?我编辑了你的代码。现在它可以工作了。我不太确定你的csv文件中会有什么。谢谢。单击按钮时,is现在用ui.R下载html。假设你选择了“活动”和“新建”。此处,应下载的csv是一个来自数据帧的文件,该数据帧在解析过程中称为“active_acc_newacc”错误(file,keep.source=FALSE,srcfile=src,encoding=enc):/Users/xxxxxxx/shinny/account_classification/server.R:43:0:输入意外结束41:42:^警告:sourceUTF8中的错误:错误寻源/Users/xxxxxxx/account_classification/server.R[无堆栈跟踪可用]sourceUTF8中的错误(serverR,envir=new.env(parent=globalenv()):错误来源/Users/xxxxxxxxx/account\u分类/server.R