Navbar Pages data load and execution scheduling

Hi, first thanks for watching my problem.

I have two question.
1.When I use Navbar Pages(sorry about that I used to describe it as navilist ) layout and load data from my databse in each tabpanel, it seems the app load all data at the beginnig. Can I load data of a penel after I change to this tabpanel?
2.


please look at this picture.
When I use this schedule, I found the F error. I think that E triggle F's flush first and don't accept D's result correctly. How can I fix this? I should represent the result of C so I can't conbine C & D.

Hi,

Issues with Shiny code can stem from both the reactive Shiny code itself or the regular R code used in the functions. In order for us to help you with your question, please provide us a minimal reprocudible example (Reprex) where you provide a minimal (dummy) dataset and code that can recreate the issue. One we have that, we can go from there. For help on creating a Reprex, see this guide:

Also, could you elaborate on point 2 where you show the diagram. What action or flow is it supposed to represent? Again, providing a reprex will give the best chance of us being able to help you.

Good luck!
PJ

Thank you pieterjanvc. I think I should make my problem clear.
You can see my code as follow.

1.The code is OK but I want to improve it.

library("shiny")

ui <- navbarPage("Reprex",
  tabPanel("Page1",textOutput("res1")),
  tabPanel("Page2",textOutput("res2"))
)

server = function(input,output){
  #I use rdata for example. In my real code, it's a sql code which get data from my database.
  #The question is that my database is too large and need sql twice.
  #I don't want to run these two sql at the beginnig.
  #Can I run the second code when I change to Page2?
 #Then users who don't want to view Page2 don't need to wait too long for unnecessary data load.
  data("iris")
  data("mtcars")
  
  output$res1 <- renderText(names(iris))
  output$res2 <- renderText(names(mtcars))
}


shinyApp(ui, server)

2.I found the problem is the isolate function. And it's also a improvement problem.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("select_class","choose class",
                  choices = c("class1","class2"),
                  selected = "class1"),
      tableOutput("datavar")
    ),
    mainPanel(
      uiOutput("select_name"),
      actionButton("select_name_active","See final result"),
      tableOutput("finaloutput"))
  )
)

server <- function(input, output) {
  #data example:
  data = data.frame(name = c("A","A","B","B","C","C",
                             "D","D","E","E","F","F"),
                    score = rnorm(12),
                    class = rep(c("class1","class2"),each = 6))

  #a reactive expression to choose my data.
  selectdata = reactive({data[data$class == input$select_class,]})
  
  
  #Present a index for user. 
  #I wish they can choose any "x" type,But there are too large data to explore. 
  #So I set a index for their inference. Like var, I will choose the "x" of max var by default.
  #And this index will enter the data fliter part directly and present a result.
  datavar <- reactive({
    res = aggregate(selectdata()$score,
                    by = list(selectdata()$name),
                    var)
    colnames(res) = c("name","scoreVar")
    res
    
  })
  output$datavar <- renderTable({
    datavar()
  })
  
  
  output$select_name = renderUI({
    selectInput("select_name","choose name",
                choices = unique(datavar()$name),
                selected = datavar()$name[which.max(datavar()$scoreVar)]
    )
  })
  
  #In my real code,select_name have many input.I didn't want that changing anyone will triggle a calculation.It may cost time.So I isolate it.
  
  #But selectdata() triggle the first flush so the result is empty at the first.
  #And it semms that select_name can't triggle a flush.
  #The question is that I want to show a pre-example for user instead of a empty result which must active by actionbutton.
  output$finaloutput <- renderTable({
    
    input$select_name_active
    isolate(selectdata()[which(selectdata()$name == input$select_name),])
    
  })
}

shinyApp(ui, server)

Hi,

Thanks for the reprex, I understand your issue now!

I recently helped a user with a very similar question in this post:

Take a look at my code there and see if this will give you your answer. If not just let me know why is didn't work and we'll explore the issue further.

Hope this helps,
PJ

Thank you pieterjanvc.

For Problem1, it works well when I copy your code. Brilliant !
But I didn't konw how do these codes work.

req(is.null(plotData()))
print("Tab 2 code is run")
plotData(runif(100))

Why does the reactive value "plotData" directly get a value? Is the complete code "plotData(value = runif(100))"?

Hi,

A reactive value in Shiny is updated differently than a normal R variable. It's actually some sort of function and new values are assigned by putting the new value inside the old reactive value.

#Both variables have the same initial value (NULL)
rVal = NULL
shinyVal= reactiveVal()

#A normal R variable is updated like this
rVal = 1
print(rVal) #Will print 1 to console

#A Shiny variable can only be accessed / updated within a reactive environment 
 #Example: when a button is clicked
observeEvent(input$button, {
   shinyVal(1) #will update the variable to the value 1
   print(shinyVal()) #Will print 1 to console, notice how you access the variable as a function
})

Got it. I usually use reactive() to creat a reactive value and don't learn these. Now I understand.

Thank you again, pieterjanvc.

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