.rdata和.csv为闪亮的数据表单选按钮输入保存错误

时间:2018-03-18 18:33:42

标签: r csv shiny

我对ShinyR应用程序比较新,想要一些帮助!我在点击actionButton后尝试将来自here)的radiobutton表的输入保存为.rdata文件和.csv文件。理想情况下,我希望矩阵/数据框中的结果与列中的每个响应,以及用户名作为行名称。

我希望它看起来像这样: enter image description here

我尝试使用下面较小的可重现版本并获得两个不同的错误,具体取决于表格的填写方式。如果我填写所有回复,我的.csv文件是这样的乱码信件,则会出现第一个错误:

saved_csv_file

我无法弄清楚为什么在.csv文件中将其保存为这样,因为它在.RData文件中看起来非常好。但是,使用我的.RData文件,rbind无法正常工作,因为每次我输入新响应时它都会覆盖以前的结果。所以我的两个问题是 1)为什么.csv文件中的数据以不可读的字母保存?
2)我如何在shinyR应用程序中正确rbind

非常感谢任何帮助!

library(shiny)
library(DT)

Agree_Likert <- c("Strongly Disagree", "Disagree", "Neither", "Agree", "Strongly Agree", "NA")

qu_page <- c("Dogs are more friendly than cats",
             "I like to gte things done",
             "I am a cat person",
             "I make my bed ever morning",
             "I am more creative in a messy environement",
             "I would describe myself as friendly",
             "I enjoy getting out of my comfort zone")

shinyApp(
  ui = fluidPage(
    h2("Questions"),
    p("Below are a number of statements, please indicate your level of agreement"),
    DT::dataTableOutput('question_matrix'),
    verbatimTextOutput('list'),
    textInput(inputId = "username", label= "Please enter your username"),
    actionButton(inputId= "submit", label= "submit")
  ),
  server = function(input, output, session) {
    m = matrix(
      as.character(1:length(Agree_Likert)), nrow = length(qu_page), ncol = length(Agree_Likert), byrow = TRUE,
      dimnames = list(qu_page, Agree_Likert)
    )
    for (i in seq_len(nrow(m))) {
      m[i, ] = sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        qu_page[i], m[i, ]
      )
    }
    m
    output$question_matrix = DT::renderDataTable(
      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-radiogroup');
  });
                    Shiny.unbindAll(table.table().node());
                    Shiny.bindAll(table.table().node());")
    )

    ## print lists in App
    #test_list<<-NULL
    # output$list = renderPrint({
       #test_list[i] <<- str(sapply(qu_page, function(i) input[[i]]))
       #test_list

   #})

    observeEvent(
      input$submit,{
        # if it exists should inclue a matrix called responses
        if (file.exists("responses.Rdata")) 
          load(file="responses.Rdata")
        if (!file.exists("responses.Rdata")){
          responses<<- matrix(ncol= length(qu_page), nrow=0)
          colnames(responses) <<- qu_page
        } 
        # unlist values from json table
        listed_responses <<- sapply(qu_page, function(i) input[[i]])

        # create one row matrix with unlisted responses
        unlisted_responses <<- t(as.matrix(unlist(listed_responses)))

        # add username as input row
        rownames(unlisted_responses) <<- input$username

        # combine it with preexiating df responses
        responses <<- rbind(responses, unlisted_responses)


        save(responses, file = "responses.Rdata")
        save(responses, file = "responses.csv")
        }
      )
    }
)

0 个答案:

没有答案