【问题标题】:Error with renderPlot but not renderPrint when using shinyjs使用 shinyjs 时,renderPlot 出错但 renderPrint 不出错
【发布时间】:2020-06-30 11:05:32
【问题描述】:

在尝试帮助 this person 时,我遇到了 shinyjs 的问题。当我单击下面代码中的矩形时,我想显示一个绘图。

在下面的示例中,单击绿色大矩形会打印文本,应该是这样的:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  div(id = "01", 
      style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", 
      HTML("01")),
  textOutput("plot")
)

server <- function(input, output) {

  onclick(id = "01", {
    output$plot <- renderPrint({
      print("foo")
    })
  })
}

shinyApp(ui, server)

但是,如果我尝试用绘图替换打印,我将面临错误:

警告:origRenderFunc 中的错误:缺少参数“名称”,没有默认值

这是产生错误的代码:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  div(id = "01", 
      style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", 
      HTML("01")),
  plotOutput("plot")
)

server <- function(input, output) {

  onclick(id = "01", {
    output$plot <- renderPlot({
      plot(mtcars)
    })
  })
}

shinyApp(ui, server)

我认为问题来自函数onclick,因为如果我将renderPlot 放在onclick 之外,我将无法重现此错误。有人知道怎么解决吗?

【问题讨论】:

    标签: r shiny shinyjs


    【解决方案1】:

    我试图将 plot() 放在 onclick() 之外,它可以工作

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
      useShinyjs(),
      div(id = "01", 
          style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", 
          HTML("01")),
      fluidRow(plotOutput("plot"))
    )
    
    server <- function(input, output) {
    
      p <- output$plot <- renderPlot(plot(mtcars))
    
      onclick("01", p)
    

    【讨论】:

    • 不工作有两个原因:1)在我点击矩形之前显示了绘图,2)点击矩形使应用程序崩溃并出现同样的错误
    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2022-07-22
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多