non reactive return function

Hi,

Please see the sample code below. When the button is clicked,

  1. Click() calls getplots()
  2. getplots() first calls loaddata()
  3. loaddata() gets some data for getplots() to make some plots
  4. getplots() returns a list of plots.

Questions:
--Currently, the withSpinner is firing when teh page loads. How do you set it up to only fire when the button is clicked?

--Currently when you click the button,no plot appears? How can that be fixed?

Currently loaddata() is reactive and it fires each time the input$Number is changed but I don't want it to fire. I only want it to return data when getplots() calls it but it has to be able to access the input$Number variable. How can you make loaddata() non-reactive but still able to return data to getplots() and access input$Number?

Thank you.

library(shinycssloaders)
library(shiny)

ui = navbarPage("header1",
tabPanel("header2",
fluidRow(
column(2, wellPanel(
dateInput("Date", "Select", value = "2005-01-01")),
actionButton("Button", label = "Button"),
selectInput("Number","Number",1:10)
),
column(10, tabsetPanel(type = "tabs",
tabPanel("Tab1",
column(10,
column(width = 3,
withSpinner( plotly::plotlyOutput("plot1"))
),
column(width = 3,plotly::plotlyOutput("plot2")),
column(width = 3,
withSpinner( plotly::plotlyOutput("plot3"))
)
)
)

                       )
                       
                       )
                     )
            )

)

server = shinyServer(function(input, output) {

Click = eventReactive(input$Button,{
req(input$Button,input$Number)
print("in button")
getplots()
})

getplots=reactive({
req(input$Number)
list= list()
data = loaddata()
p1 = plotly::plot_ly(data.frame(x =1:input$Number, y = 1:input$Number), x = ~x, y =~y, type = 'bar')
p2 = plotly::plot_ly(data.frame(x = 1:input$Number, y = 1:input$Number), x = ~x, y =~y, type = 'bar')
p3 = plotly::plot_ly(data.frame(x = 1:input$Number, y = 1:input$Number), x = ~x, y =~y, type = 'bar')
list[[1]]=p1
list[[2]]=p2
list[[3]]=p3
print(class(list))
print(class(list[[1]]))
return( results = list)
})

loaddata <- reactive({
req(input$Button)
#loaddata <- function(
isolate({ req(input$Number)
data <- sample(1:input$Number, 50, replace = TRUE)})
print(length(data))
return(data)
#)
})

output$plot1 = renderPlotly({
req(input$Button)
print("in plot 1")
Click()[[1]]
})

output$plot2 = renderPlotly({
plotly::plot_ly(data.frame(x = 1:3, y = 1:3), x = ~x, y =~y, type = 'bar')
})

output$plot3 = renderPlotly({
Click()[[3]]
})

})

shinyApp(ui = ui, server = server)

perhaps this ?

 loaddata <- reactive({
    req(input$Button)
    #loaddata <- function(
 isolate({   req(input$Number)
    data <- sample(1:input$Number, 50, replace = TRUE)})
    print(length(data))
    return(data)
    #)
  })

It worked when I ran it inside the program you shared. shrug

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

Hi,

Thanks for the response. That didn't work unfortunately.

Hi,

Does the code above work or show the spinners when loading?

Thank you.

If you want to get help, I'd recommend following the advice at https://mastering-shiny.org/action-workflow.html#reprex. Currently your code is poorly formatted and contains a lot of moving parts. I'd recommend carefully restyling it to make it easier to read, and considering what parts of the code are actually connected to the specific problem you are experiencing.