简体   繁体   中英

Reactivity in R shiny with toy example

I am altering this example ( https://gist.github.com/wch/5436415/ ). Below is a toy model. Bascially I need the selectInput to be reactive and each time the selectInput is changed the value selected is passes to a function in global.r. Then I need to be able to use the result.

Basically: (1) When the app first appears there should be 1 plot

(2) When the user changes the slider input there is a reactive "input$n" in the max_plots function in server.r. This input$n is passed to the "NumberOfPlots" function in global.r

(3) "numberOfPlots" will return a number. Ex. If the user changes the select input to 5. 5 gets passed to "NumberOfPlots" and 5 is returned from "NumberOfPlots"

(4) now that the user made his selection I am using "maxplots()" in the shiny server function to access the number of plots

I am getting the error:

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Below is my CODE which you can run if you create an "runshiny.r" file and call the 4 lines below:

install.packages("shiny")
library(rJava)
library(shiny)
runApp("C://Users/me/multi")#change this to the path of your correct folder

Here is my server.r

shinyServer(function(input, output,session) {

#get the number of plots from reactive input and pass to global function
max_plots<- reactive({
  print("IN reactive function")
  NumberOfPlots(input$n)
})


  # Insert the right number of plot output objects into the web page
  output$plots <- renderUI({
    #plot_output_list <- lapply(1:input$n, function(i) {
    plot_output_list <- lapply(1:max_plots(), function(i) {
      plotname <- paste("plot", i, sep="")
      plotOutput(plotname, height = 280, width = 700)
    })
    # Convert the list to a tagList - this is necessary for the list of items
    # to display properly.
    do.call(tagList, plot_output_list)
  }) #end of output$plots

  # Call renderPlot for each one. Plots are only actually generated when they
  # are visible on the web page.
  for (i in 1:max_plots()) {
    # Need local so that each item gets its own number. Without it, the value
    # of i in the renderPlot() will be the same across all instances, because
    # of when the expression is evaluated.
    local({
      my_i <- i
      plotname <- paste("plot", my_i, sep="")

      output[[plotname]] <- renderPlot({

        plot(1:my_i, 1:my_i,
             xlim = c(1, max_plots()),
             ylim = c(1, max_plots()),
             main = paste("1:", my_i, ".  n is ", input$n, sep = "")
        )
      })#end of renderPlot
    })#end of local
  }#end of loop over max_plots

  })#end of server

Here is my global.r

NumberOfPlots<-function(n)
{
  print("in global") 
  print(n)
  length(seq(from=1 , to=n, by = 1))
}

Here is my ui.r

shinyUI(pageWithSidebar(
  headerPanel("Dynamic number of plots"),
  sidebarPanel(
    sliderInput("n", "Number of plots", value=1, min=1, max=7)
  ),
  mainPanel(
    # This is the dynamic UI for the plots
    uiOutput("plots")
  )
))

您需要将for循环包装到observe()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM