创建一个表单,使用闪亮的R将观察结果添加到现有表中

时间:2014-08-03 17:12:19

标签: r csv shiny

我在装有Windows 8.1的PC上使用RStudio 0.98.953和R 3.1.0。

我正在尝试在R中使用shiny为许多用户创建一个表单,该表单将通过添加到现有的最初为空的表来存储他们的类型信息。我已经看到关于这个问题已经发布了几个问题,但答案没有写清楚。

我希望任何用户都能够使用多个 textInput 字段添加到表中,然后调用此表进行编辑,更新,甚至可以生成此信息的图表。具体来说,我希望输入转到两个位置: dataTableOutput 用于在用户屏幕上显示, csv 文件供我随时访问。相反,正在发生的是 dataTableOutput 一次只显示一个条目(最新的条目)。并且没有任何内容写入csv文件。我已经做了很多条目来测试它,它总是失败。我已经尝试使用observe并隔离创建用户data.table的代码。我尝试了一个类似于if(input$Submit > 0) { ...的if语句来使用提交按钮,但是如果没有这个代码,提交按钮似乎工作正常。

这是我的代码:

server.R

library(data.table)

# first I initialize an empty data table with all of the potential fields 
# I want for each future record. There are more fields initially set than my user 
# will start off with.

ir <- data.table(X = "", Y = "", Z = "", W = "", V = "", U = "")

shinyServer(function(input, output) {

# this is a reactive data.table that takes inputs from the UI (X, Y, and Z).

obs <- reactive({

        data.table(X = input$X, Y = input$Y, 
                         Z = input$Z)
})

# this generates a table of the information for the user to see. It should show
# their record being added to a list that is continuously being added to. 

output$table <- renderDataTable({

    rbind(ir, obs())

    })

# take the latest entry, add it to the running table, and write to a csv.

reactive({
        rbind(ir, obs())
        write.csv(ir, file = "C:/Users/Geo/Documents/ideaTracker/data/ideaRecords.csv")
})

ui.R

shinyUI(navbarPage("Tracker",

# some UI code is not shown since it only defines the layout of other pages in the app.

sidebarPanel(

    # This defines the 'X' input

    dateInput("X", label = h4("Date"), value = as.character(Sys.Date()), 
                          format = "mm/dd/yy"),

    # defines the 'Y' input

    textInput("Y", h4("Name"),
                          value = "Joe Employee"),

    # and the 'Z' input

    textInput("Z", h4("Enter Information Here"), value = ""),

    submitButton("Submit")
            ),

    mainPanel(

        # displays the information entered by the user and should show the 
        # running list with all previous entries.

        dataTableOutput("table"),

    )

我认为很多其他人可以从这个问题(并希望答案,有希望)得到充分记录中受益。

0 个答案:

没有答案