I would like to extend this application when data frame exists at the beginning. To be honest, my question is bigger than this where you can find the problem in following link: How to add a new row to uploaded datatable in shiny
Via this question, I am gonna chase the big picture with minors.
I have a currently data frame, 2 columns and 3 rows. First column indicates the current date, other one is to be calculated. new row should be appeared like (Current Date - Like in Excel eg. 11.02.2015-, [Input$1 + "perivious value of column2's row"])
However, I have problem about showing the system date. Additionaly, I cannot produce a new line which gives a warning in newLine!
second version: data can be uploaded. with error: Error in read.table(file = file, header = header, sep = sep, quote = quote, :
'file' must be a character string or connection
Warning: Unhandled error in observer: object of type 'closure' is not subsettable
observeEvent(input$update)
library(shiny)
library(gtools)
runApp(
list(
ui = fluidPage(
pageWithSidebar(
headerPanel("Adding entries to table"),
sidebarPanel(
wellPanel(fileInput('file1', 'Choose Planning File:', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'), multiple = FALSE),
selectInput(inputId = "location",label = "Choose Location",
choices = c('All','Lobau'='LOB', 'Graz'='GRA', 'St. Valentin'='VAL'), selected = "GRA"),
selectInput(inputId = "product",label = "Choose Product",
choices = c('All','Gasoline'='OK', 'Diesel'='DK'), selected = "DK")),
numericInput("spotQuantity", "Enter the Spot Quantity",value=0),
actionButton("action","Confirm Spot Sales"),
numericInput("num2", "Column 2", value = 0),
actionButton("update", "Update Table")),
mainPanel(tableOutput("table1")))
),
server = function(input, output, session) {
values <- reactive({ #
#file.choose()
dm <- as.data.frame(read.csv(input$file1$datapath, sep=";",check.names = FALSE))
})
addData <- observeEvent(input$update, {
values$dm <- isolate({
newLine <- data.frame('Month'=1,'Day ID'=2,'Day'="28-11-2012",'starting inventory'=2,'planned (in kTO)'=2,'lifted (in kTO)'="2",'replenishment (in kTO)'="2", 'Product'="OK",'Location'="GRA", check.names=F)
rbind.data.frame(values$dm,newLine)
})
})
output$table1 <- renderTable({
values()
})
}
)
)
Answer
There are multiple issues with your code. You start reactiveValues
but you never assign anything to it so no data could hope to be reactive. Also, you likely want to use observeEvent
so that each time you hit the Update
button you get a response. You can also isolate
blocks of code. Furthermore, you should use a data.frame
for your new data as the 'type' of the data matters (i.e. numeric
, character
, etc.). The following works well for me.
library(shiny)
runApp(
list(
ui = fluidPage(
pageWithSidebar(
headerPanel("Adding entries to table"),
sidebarPanel(
numericInput("num2", "Column 2", value = 0),
actionButton("update", "Update Table")),
mainPanel(tableOutput("table1")))
),
server = function(input, output, session) {
values <- reactiveValues(
dm = data.frame(Date = as.Date(c("2015-05-10", "2015-10-07", "2015-03-26","2015-07-18")),
Col2 = c(160, 150, 121, 93))
)
addData <- observeEvent(input$update, {
values$dm <- isolate({
newLine <- data.frame(Date = format(Sys.time(), "%Y-%m-%d"),
Col2 = tail(values$dm$Col2, n=1) - 4)
rbind(values$dm,newLine)
})
})
output$table1 <- renderTable({
values$dm
})
}
)
)
No comments:
Post a Comment