【问题标题】:Plots not rendering in Rshiny using modules and tabPanels使用模块和 tabPanels 在 Rshiny 中不渲染的绘图
【发布时间】:2018-06-27 05:36:48
【问题描述】:

我的基本问题是,在对 UI 和服务器逻辑使用模块方法时,我无法让我的图在 tabPanels 中呈现。我正在尝试使用多个 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)

再次,我希望面板 1 根据用户在面板 1 中提供的输入显示图表,然后让面板 2 根据面板 2 中提供的内容显示第二个图表。当我输入数据我单击按钮,没有任何反应。我可以在面板之间来回翻转并且输入在那里,这很好,但没有情节。我没有收到任何错误消息,并且 browser() 并没有给我带来任何有用的地方。我怀疑我的问题是将绘图对象从绘图函数传递给renderPlot;但在拥有单独的功能时,我试图遵循this article 中提供的指导方针。

非常感谢任何指导,一如既往!

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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

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

    • 您的代码在定义 UI Id 时在 moduleUI 中遗漏了 namespaces - 添加了它。
    • 改变了从模块服务器返回响应值的方式
    • 对 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)
    

    【讨论】:

    • 感谢您的回复!正如你所写的那样,你的代码仍然对我不起作用,但我得到的错误来自 ggplot 关于处理类数字和选择未定义的列。我已经编辑了您的答案,以便为我提供工作代码(例如,包括您对 data.frame() 的调用并删除了列的选择)。我还编辑了我的原始问题,将 ns() 包含在 UI 模块中,因为这是一个愚蠢的错误,而不是实际答案的一部分。
    • 另一个问题也许你可以帮忙:在我的完整应用程序中,绘图函数使用了几个输入值。我知道父应用程序无法使用输入,除非打包在反应式表达式中(例如,reactive(input$val)),但我不知道应该去哪里 - 我已经尝试将它们作为参数放入callModule() 并作为 showPlot 中的参数,两者都会引发错误。有什么想法吗?
    • @phalteman input$val 本身是被动的,我猜。它可能无法正常工作,因为您已经设置了 showPlot(只是猜测)。您是否尝试在模块服务器中添加绘图功能并传递参数?
    • 第二个问题的答案见这篇文章:How to return input values from callModule function in shiny
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多