4Manuals

  • PDF Cloud HOME

在navbarPage中的各个选项卡之间重用ouputs $ xxx Download

    RSQLite:如何增加列数和参数数? 使用plotstyle =“ ggplot”时,为什么在qqcomp函数中没有显示任何点? 如何测量时间序列功耗模式的变化性? 具有不存在点的四分之一圈ggplot-s 使用奇怪的结构构建列表(?) 从R中的数据帧读取值 使用broom :: augment函数和面板数据 如何以月-年格式获取日期? 向R中的函数添加可选参数 ggsurvplot:如何使检查标记仅出现在生存线上方

对于Shiny来说还不是很陌生-尤其是如何使代码紧凑并避免重复。 为了简化我的问题(下面的代码):我想使用navbarPage,因为我喜欢它给出的概述。

在每个选项卡中,我想在mainPanel中显示一个图,这取决于sidePanel的输入,而sidePanel的输入又取决于Tab。但是,由于现在允许在多个位置调用相同的输出,因此导致重复,如下面的代码所示。

所以我的问题是,在仍使用制表符的同时,如何解决此问题。 非常感谢您的帮助和新年快乐!

#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)

1 个答案:

答案 0 :(得分:1)

据我所知,不。不同的选项卡将需要不同的输出ID。您可能会用一个闪亮的模块来掩盖这个事实,但是在某个级别的某个地方,您将分配给3个不同的输出。



Similar searches
    从application.yml获取logback.groovy中的属性 RSQLite:如何增加列数和参数数? 在本地本地运行Azure WebJob 触发从另一个表中删除行 首次加载页面时未显示Chart.js