我在装有 Windows 8.1 的 PC 上使用 RStudio 0.98.953 和 R 3.1.0。
我试图在 R 中使用 Shiny 为许多用户创建一个表单,该表单将通过添加到现有的、最初为空的表来存储他们输入的信息。我已经看到一些关于该主题的问题,但答案没有写清楚。
我希望任何用户都能够使用多个textInput字段添加到表中,然后调用此表进行编辑、更新,甚至制作此信息的图表。具体来说,我希望输入到两个地方:一个用于显示在用户屏幕上的dataTableOutput和一个可供我随时访问的csv文件。相反,发生的情况是dataTableOutput 一次只显示一个条目(最新的条目)。并且没有任何内容写入 csv 文件。我已经做了很多条目来测试它,但它总是失败。我尝试在创建用户的 data.table 的代码周围使用观察和隔离。我试过类似的 if 语句if(input$Submit > 0) { ... 使用提交按钮,但提交按钮在没有此代码的情况下似乎可以正常工作。
这是我的代码:
服务器
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")
})
Run Code Online (Sandbox Code Playgroud)
用户界面
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"),
)
Run Code Online (Sandbox Code Playgroud)
我认为许多其他人可以从这个问题(并希望回答)中受益。