I have been searching for hours on a solution for a simulation tool that should concatenate graph parts (the last part should be shown reactive before pressing an "add" action button). The dataframe of the completed graph (all concatenated parts) should be downloadable as csv.
Basically i want to combine functionality like described here Add (multiple) values to a data frame with R shiny (adding rows to a dataframe) and here http://shiny.rstudio.com/reference/shiny/latest/updateSliderInput.html (reactive sliders) without ending up in an endless loop.
I am able to make my output .csv file longer with the action button, but not all added simulations are stored correctly nor do I succeed to update my x input value (where the next part of the graph should start).
Here's my code. Any help is much appreciated. Tom
server.R
shinyServer(function(input, output, clientData, session){
#set up model input parameters
observe({
#Define input variables
x <- input$xc
a <- input$ac
b <- input$bc
l <- input$lc
#Control value, min, max, and step
updateSliderInput(session, "xr", value = x, min = x-10, max = x+10, step = 0.1)
updateSliderInput(session, "ar", value = a, min = a-10, max = x+10, step = 0.1)
updateSliderInput(session, "br", value = b, min = b-10, max = x+10, step = 0.1)
updateSliderInput(session, "lr", value = l, min = l-10, max = l+10, step = 0.1)
})
#Calculate additional variables
observe({
xs <- input$xr
xe <-xs+input$lr
al <-input$ar
bl <-input$br
xl <-xs:xe
yl<-xl*al+bl
#create the continiously updated data from the inputs
dataset0<-reactive({
df<-as.data.frame(cbind(xl,yl))
return(df)
})
#set the data to be added aside
addData <- reactiveValues()
addData$dataset0 <- dataset0()
#when the action button is pressed, freeze the data from the model and store them
observe(if (input$addDataset>0) {
newFrame <- isolate(addData$dataset0)
#THIS IS NOT WORKING: SAVE LAST X VALUE AS NEW MODEL START
newx0 <- isolate(addData$dataset0[length(addData$dataset0[,1]),1])
#update data
isolate(addData$dataset0 <- rbind(addData$dataset0, newFrame))
#THIS IS NOT WORKING: CHANGING THE INPUT CAUSES INFINITE LOOP
#updateNumericInput(session,"xc",value=newx0)
})
#set the freezed data aside
dataset<-reactive({
df<-as.data.frame(addData$dataset0)
return(df)
})
#show some output
output$newx<-renderText(addData$newx0())
output$plot<-renderPlot({plot(dataset()$y~dataset()$x)})
output$table1 <- renderTable(head({addData$dataset0},3), include.rownames=FALSE)
output$table2 <- renderTable(tail({addData$dataset0},3), include.rownames=FALSE)
#download the dataset
output$downloadDataset <- downloadHandler(
filename = function() {paste('dataset','.csv', sep='')},
content = function(file) {write.table(dataset(), dec = ",", sep = ";", row.names = FALSE, file)}
)
})
})
ui.R
shinyUI(fluidPage(
titlePanel("Simulator input"),
fluidRow(
column(2, wellPanel(
#numeric default inputs, changing them updates the sliders
numericInput("xc", "choose x:", min=0, max=100, value=1, step=0.1),
numericInput("ac", "choose a:", min=0, max=100, value=1, step=0.1),
numericInput("bc", "choose b:", min=0, max=100, value=1, step=0.1),
numericInput("lc", "choose l:", min=0, max=100, value=50, step=0.1)
)),
column(2, wellPanel(
#sliders updated through the numeric inputs, their value are used in the graph
sliderInput("xr", "choose x:", min=0, max=100, value=1, step=0.1),
sliderInput("ar", "choose a:", min=0, max=100, value=1, step=0.1),
sliderInput("br", "choose b:", min=0, max=100, value=1, step=0.1),
sliderInput("lr", "choose l:", min=0, max=100, value=50, step=0.1)
)),
#the actionButton serves to add a graph part and lines to the data frame
actionButton("addDataset", "Add to Dataset"),
downloadButton('downloadDataset', 'Download'),
# Show a table summarizing the values entered
mainPanel(
plotOutput("plot"),
textOutput("newx"),
tableOutput("table1"),
tableOutput("table2")
)
)
))