不使用模块和tabPanel在Rshiny中渲染的绘图

时间:2018-01-18 07:58:56

标签: r shiny

我的基本问题是,在使用UI和服务器逻辑的模块方法时,我无法在tabPanel中渲染我的绘图。我正在尝试使用几个tabPanel(在navbarPage和navbarMenus下)构建一个应用程序,其中用户在选项卡之间输入具有类似结构的一些数据,然后应用程序生成,然后在该选项卡上绘制特定于用户输入的数据。

这是我的应用程序的一个大大减少的版本,但这似乎产生了同样的问题:

# UI module
modUI <- function(id, label="inputvalues") {
  ns <- NS(id)
  tagList(
    numericInput(ns("mean"), "Mean",value = NULL),
    numericInput(ns("sd"),"Std. Dev.",value = NULL),
    actionButton(ns("draw"),"Draw plot")
  )
}

# Server Logic module
mod <- function(input, output, session){
  x <- reactiveValues(data=NULL)
  observeEvent(input$draw, {
    x$data <- data.frame(rnorm(100,input$mean,input$sd))
  })
  reactive({x})
}

# Plotting function
showPlot <- function(data){
  reactive({
  p <- ggplot(data, aes(x=data[,1])) +
    geom_histogram()
  p
  })
}

# UI call
ui <- navbarPage("Fancy Title",
          tabPanel("Panel1",
              sidebarPanel(
                modUI("input1")
              ),
              mainPanel(plotOutput("plot1"))
              ),
          tabPanel("Panel2",
              sidebarPanel(
                modUI("input2")
              ),
              mainPanel(plotOutput("plot2"))
          )
)

# Server call
server <- function(input, output) {
  y <- callModule(mod, "input1")
  output$plot1 <- renderPlot({ showPlot(y()) })

  z <- callModule(mod, "input2")
  output$plot2 <- renderPlot({ showPlot(z()) })
}

shinyApp(ui, server)

同样,我希望Panel 1能够根据用户在面板1中提供的输入显示图形,然后让Panel 2根据面板2中提供的内容显示第二个图形。当我输入时数据我点击按钮没有任何反应。我可以在面板之间来回切换,输入就在那里,这很好,但没有情节。我没有收到任何错误消息,而浏览器()并没有带给我任何有用的信息。我怀疑我的问题是将绘图对象从绘图函数传递给renderPlot;但是在使用单独的功能时,我正在尝试遵循this article中提供的指南。

任何指导都一如既往地受到赞赏!

版本(R,RStudio,Rshiny)都是最新版本。

1 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些更改:

  • 在定义UI ID时,您的代码在moduleUI中错过了namespaces - 添加了它。
  • 更改了从moduleserver
  • 返回反应值的方式
  • showplot上的一点调整(关于如何处理数据)

所以,你的问题不在于绘图,而在于绘制输入数据。

更新的代码:

library(shiny)

# UI module
modUI <- function(id, label="inputvalues") {
  ns <- NS(id)
  tagList(
    numericInput(ns("mean"), "Mean",value = NULL),
    numericInput(ns("sd"),"Std. Dev.",value = NULL),
    actionButton(ns("draw"),"Draw plot")
  )
}

# Server Logic module
mod <- function(input, output, session){
  x <- reactiveValues(data=NULL)
  observeEvent(input$draw, {
        x$data <- rnorm(100,input$mean,input$sd)
  })
  return(reactive({x$data}))
  }

# Plotting function
showPlot <- function(data){

    data <- data.frame(data = data)
    p <- ggplot(data, aes(x=data)) +
      geom_histogram()
    p

}

# UI call
ui <- navbarPage("Fancy Title",
                 tabPanel("Panel1",
                          sidebarPanel(
                            modUI("input1")
                          ),
                          mainPanel(plotOutput("plot1"))
                 ),
                 tabPanel("Panel2",
                          sidebarPanel(
                            modUI("input2")
                          ),
                          mainPanel(plotOutput("plot2"))
                 )
)

# Server call
server <- function(input, output, session) {
  y <- callModule(mod, "input1")



  output$plot1 <- renderPlot({ showPlot(data.frame(y())) })
  #output$plot1 <- renderPlot(plot(1:100))

  z <- callModule(mod, "input2")
  output$plot2 <- renderPlot({ showPlot(data.frame(z())) })
}

shinyApp(ui, server)