如何为没有输入的情节制作闪亮的按钮重新调用功能?

时间:2015-04-07 16:52:57

标签: r shiny

我是Shiny的新手,我需要知道如何使用按钮来重新触发shinyServer({}),即使没有新的输入。

我有一个函数makeTrace(),它创建一个具有某些功能的随机时间序列。我只想写一个简单的Shiny App来显示时间序列以及再次调用makeTrace来重做时间序列的按钮,这样观众可以通过反复按下按钮来看到他喜欢的多个。随机化是在makeTrace函数中,所以我需要闪亮的就是调用这个函数。

我让这个工作的唯一方法是添加另一个输入并通过输入链接按钮和情节,但这不是我想要的:

server.R

source("helpers.R")

shinyServer(function(input, output) {

  # builds a reactive expression that only invalidates 
  # when the value of input$goButton becomes out of date 
  # (i.e., when the button is pressed)
  ntext <- eventReactive(input$goButton, {
    output$trace <- renderPlot({
      plot(makeTrace())
    })
    input$n
  })

  output$nText <- renderText({
    ntext()
  })

})

和ui.R

   shinyUI(pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    numericInput("n", "N:", min = 0, max = 100, value = 50),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText"),
    plotOutput("trace")
  )
))

如果你想运行它,我会在底部粘贴helpers.R。

因此,如果你运行这个,你会看到显示确实更新了一个新的时间序列,但问题是我想这样做,而不是有一些虚假的输入,我用作假参数。我真的只想按一个按钮让剧情(makeTrace())再次被调用,因为正如我所说,makeTrace()在每次调用时都会创建新的/随机数据。

感谢。

helpers.R

#MAKE STEPS WITHIN A CONDUCTANCE STEP

makeSteps <- function(lowG, highG, num.G.groups = 3, max.steps = max.steps.const, mean.step.p = 300, sd.step.p = 100, temp=0){

  #decide how many within conductance plateau events there are for this plateau and segment conductance into boxes
  mid               <- rnorm(2, mean = (lowG + highG)/2, sd = (lowG + highG)/5)
  conductance.boxes <- seq(from=min(mid), to=max(mid), length.out = num.G.groups)
  numSteps          <- sample(1:max.steps, 1)

  #choose conductance and step length for each step
  #enforce G values that are physically realistic, no higher or lower than cutoff values
  stepsG <- rnorm(numSteps, mean = (lowG + highG)/2, sd = (lowG + highG)/5)
  stepsG <- replace(stepsG, stepsG<lowG, lowG)
  stepsG <- replace(stepsG, stepsG>highG, highG)
  stepsL <- floor(abs(rnorm(n = numSteps, mean = mean.step.p, sd=sd.step.p)))

  #prepare entire molecular step
  trace  <- c(1:sum(stepsL))
  startp <- 1
  for(i in 1:length(stepsL)){
    endp               <- startp + stepsL[i] - 1
    trace[startp:endp] <- rnorm(n=stepsL[i], mean = stepsG[i], sd = stepsG[i]/12)
    startp             <- startp + stepsL[i]
  }

  trace
}

# MAKES TRACES WITH A MOLECULAR STEP
makeTrace <- function(molG=10^-4, molSD=10^-4, excursion=5, GDist=.5, numP=numPconst, multiples=FALSE, temp=0, shiny=0){

  #make 3,2,1  G0 steps that have within-plateau events if temp = 0, otherwise they are relatively flat
  g3 <- makeSteps(lowG=2.1, highG=2.5, temp=temp)
  g2 <- makeSteps(lowG=1.4, highG=min(g3), temp=temp)
  g1 <- makeSteps(lowG=.5, highG=min(g2), temp=temp)


  #length measured in points
  g1stepL <- length(g1)
  g2stepL <- length(g2)
  g3stepL <- length(g3)

  #conversion between nm and indices
  delX <- numP/excursion
  dt   <- seq(from = 0 , to = excursion, excursion/numP)

  #store conductance time series in g (G for conductance)
  g      <- c(1:numP + 1)
  offset <- 0

  #assign 3,2,1 G0 steps generated earlier individually to single time series
  g[(1 + offset) : (g3stepL + offset)] <- g3 
  offset                               <- g3stepL + offset

  g[(1 + offset) : (g2stepL + offset)] <- g2 
  offset                               <- g2stepL + offset

  g[(1 + offset) : (g1stepL + offset)] <- g1 
  offset                               <- g1stepL + offset

  #metallic junction breaks to electronics noise level
  g[(offset):numP + 1] <- rnorm(n=(numP-offset+1),mean=10^-5, sd = 10^-5)

  #subtract out negative noise so all values positive
  m <- min(g)
  g <- g - m +.000001
  g <- remove.repeat.NA(trace.smooth(g, type="moving-average", width=5))
  df <- data.frame(Displacement=dt,Conductance=g, check.names = FALSE, row.names = NULL)  
}

2 个答案:

答案 0 :(得分:2)

在ui.R中包括:

actionButton('samp','New Sample')

然后在server.R中包括:

observe({
  if(input$samp > 0) {
    plot(makeTrace())
  }
})

您可能还需要其他一些电话(可能是isolate),但希望这会让您开始。

答案 1 :(得分:1)

@ GregSnow的建议很接近,但实际上没有更新显示。这是使按钮更新通过调用辅助文件中的函数生成的显示的解决方案:

 observe({   
    if(input$samp > 0) {
      plot(makeTrace())
      output$trace <- renderPlot({
        plot(makeTrace())
      })
    }
  })
不应该使用

isolate(),因为在这种情况下我没有其他输入需要与绘图隔离。