【发布时间】:2016-12-04 04:51:28
【问题描述】:
我一直在寻找一个模拟工具的解决方案,该解决方案应该连接图形部分(最后一部分应该在按下“添加”操作按钮之前显示为反应性)。完成图的数据框(所有连接部分)应可下载为 csv。
基本上,我想结合此处描述的功能Add (multiple) values to a data frame with R shiny(向数据框添加行)和此处http://shiny.rstudio.com/reference/shiny/latest/updateSliderInput.html(反应性滑块),而不会陷入无限循环。
我可以使用操作按钮使我的输出 .csv 文件更长,但并非所有添加的模拟都正确存储,我也没有成功更新我的 x 输入值(图表的下一部分应该从哪里开始)。
这是我的代码。任何帮助深表感谢。 汤姆
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")
)
)
))
【问题讨论】:
-
非常感谢您对 observe/observeEvent 和 req 的有用反馈。下载处理程序也可以工作。