Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

reusing ouputs$xxx across tabs in navbarPage

$
0
0

Im fairly new to Shiny - especially how to make thecode compact and avoid duplication. To simplify my problem (code below): I want to make use of the navbarPage, because i like the overview it gives.

In each tab I want to show a plot in the mainPanel, which depends on the inputs from the sidePanel, which in turn depends on the Tab. But since it is now allowed to call the same output in multiple places, this leads to duplication as my code below shows.

So my question is what the way get around this is, while still using tabs. Thanks a lot for the help and happy new years!

#test
library(shiny)

plot_fun_helper <- function(x,y){
    plot(x,y, type = 'l')
}

#function that returns the x- and y vektors for plotting, not important.
xy_dens_gen <- function(tab, input){
  if(tab == "Normal"){
      #if normal, plot around mean, pm 3 std
      x <- seq(from = input$normal_mean - 3*input$normal_std, 
               to = input$normal_mean + 3*input$normal_std,
               length.out = 1e3)
      y <- dnorm(x, mean = input$normal_mean, sd = input$normal_std)
    }
  else if(tab == "LogNormal"){
    #if LogNormal, plot from 0 to 3*CV
    x <- seq(from = 0, 
             to = 3*input$lognormal_CV,
             length.out = 1e3)
    y <- dlnorm(x, meanlog = 0, sdlog = 1)
  }
  else if(tab =="Exponential"){
    x <- seq(from = 0,
             to = 3/input$exp_rate,
             length.out = 1e3)
    y <- dexp(x, rate = input$exp_rate)
  }
  else stop("No method found")

  return(list(x = x,
              y = y))
}


# Define UI for application that draws the pdf
ui <- 
  navbarPage(
    title = 'reprex', id = "cur_tab", selected = 'Normal',
    # Normal Tab ----
    navbarMenu("Normal/LogNormal",
               tabPanel("Normal",
                        sidebarLayout(
                          sidebarPanel(
                            sliderInput(inputId = "normal_mean", "Choose mean", value = 1, min = -2 , max = 2),
                            sliderInput(inputId = "normal_std" , "Choose std" , value = 1, min = 0 , max = 2)
                          ),
                          mainPanel(
                            plotOutput("plot_normal")
                          )
                        )
               ),
               tabPanel("LogNormal",
                        sidebarLayout(
                          sidebarPanel(
                            sliderInput(inputId = "lognormal_CV", "Choose CV", value = 1, min = 0 , max = 20)

                          ),
                          mainPanel(
                            plotOutput("plot_LogNormal")
                          )
                        )
               )
    ),

    # exponential Tab 
    tabPanel('Exponential',       
             sidebarLayout(
               sidebarPanel(
                 sliderInput(inputId = "exp_rate", "Choose rate", value = 1, min = 0, max = 10)
               ),
               mainPanel(
                  plotOutput("plot_exp")

               )
             )
    )

  )


# Define server logic required to draw pdf
server <- function(input, output) {

    rvals <- reactiveValues()

    observe({
      rvals$x <- xy_dens_gen(tab = input$cur_tab, input = input)$x
      rvals$y <- xy_dens_gen(tab = input$cur_tab, input = input)$y
    })



    #Render print inside renderPLot
    output$plot_normal <- renderPlot({
      plot_fun_helper(x = rvals$x, y = rvals$y)
    })

    output$plot_LogNormal <- renderPlot({
      plot_fun_helper(x = rvals$x, y = rvals$y)
    })

    output$plot_exp <- renderPlot({
      plot_fun_helper(x = rvals$x, y = rvals$y)
    })


}

# Run the application 
shinyApp(ui = ui, server = server)


Viewing all articles
Browse latest Browse all 205399

Trending Articles