【发布时间】:2018-03-18 21:16:58
【问题描述】:
一旦单击了 actionButton,我正在尝试保存从 [此处][2] 改编的复选框表 (see here) 中的输入。理想情况下,我想要一个数据框列中的选定框列表,并将用户名作为行名。
我尝试使用以下语法,将响应存储在列表中,然后将它们附加到现有的 csv.file。
library(shiny)
library(DT)
answer_options<- c("reading", "swimming",
"cooking", "hiking","binge- watching series",
"other")
question2<- "What hobbies do you have?"
shinyApp(
ui = fluidPage(
h2("Questions"),
p("Below are a number of statements, please indicate your level of agreement"),
DT::dataTableOutput('checkbox_matrix'),
verbatimTextOutput('checkbox_list'),
textInput(inputId = "username", label= "Please enter your username"),
actionButton(inputId= "submit", label= "submit")
),
server = function(input, output, session) {
checkbox_m = matrix(
as.character(answer_options), nrow = length(answer_options), ncol = length(question2), byrow = TRUE,
dimnames = list(answer_options, question2)
)
for (i in seq_len(nrow(checkbox_m))) {
checkbox_m[i, ] = sprintf(
'<input type="checkbox" name="%s" value="%s"/>',
answer_options[i], checkbox_m[i, ]
)
}
checkbox_m
output$checkbox_matrix= DT::renderDataTable(
checkbox_m, escape = FALSE, selection = 'none', server = FALSE,
options = list(dom = 't', paging = FALSE, ordering = FALSE),
callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-checkbox');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
observeEvent(input$submit,{
# unlist values from json table
listed_responses <- sapply(answer_options, function(i) input[[i]])
write.table(listed_responses,
file = "responses.csv",
append= TRUE, sep= ',',
col.names = TRUE)
})
}
)
我得到的只是警告:
write.table(listed_responses, file = "responses.csv", append = TRUE, :appending column names to file
除了警告之外,.csv 文件中没有保存任何内容,我不确定我到底缺少什么。
如何正确保存数据表中的选中框列表?
【问题讨论】:
标签: javascript r shiny