Hi,
Please see the sample code below. When the button is clicked,
- Click() calls getplots()
- getplots() first calls loaddata()
- loaddata() gets some data for getplots() to make some plots
- 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)