【问题标题】:How to display default plot in www/ before actionButton is clicked R shiny如何在点击 actionButton 之前在 www/ 中显示默认绘图 R 闪亮
【发布时间】:2020-07-02 22:50:09
【问题描述】:

这是一个示例代码,用于在单击 actionButton 时生成绘图。

shinyApp(
shinyUI(fluidPage(
    inputPanel( 
        numericInput("n", "n", 10),
        actionButton("update", "Update")
    ),
    plotOutput("plot")
)),

shinyServer(function(input, output) {
    values <- reactiveValues()
    values$data <- c()

    obs <- observe({
        input$update
        isolate({ values$data <- c(values$data, runif(as.numeric(input$n), -10, 10)) })
    }, suspended=TRUE)

    obs2 <- observe({
        if (input$update > 0) obs$resume()
    })

    output$plot <- renderPlot({
        dat <- values$data
        hist(dat)
    })
}) 

)

我想在应用程序启动时显示 www/test.png 中的默认图。然后根据用户输入单击 actionButton 后更改绘图。

【问题讨论】:

  • 是否有必要显示 png 或者默认的 R-plot 也足够了?两者都有可能,但后者更容易
  • 它必须是 png 或任何图像格式,但已生成并存储在 www/ 目录中。

标签: r shiny action-button


【解决方案1】:

首先,我创建一个简单的绘图,将其导出为图像(手动,而不是在代码中)并将其命名为 Rplot.png(将其保存在您想要的位置):

plot(mtcars$mpg)

那么,在闪亮的应用中,我们要区分两种情况:

  • 当应用启动时,还没有点击任何按钮,我们使用renderImage渲染图像

  • 当我们点击按钮时,我们将renderImage替换为renderPlot并渲染一个交互式绘图

这意味着我们必须在ui部分使用uiOutput,以便我们可以根据情况选择输出为图像或绘图。

这是一个例子(我没有改编你的代码,但应该不会太难):

library(shiny)

# determine your path to image here (you should use the package "here" to do so)

ui <- fluidPage(
  selectInput("choice", "Choose", choices = names(mtcars)),
  actionButton("run", "Run"),
  uiOutput("some_ui")
)

server <- function(input, output, session) {

  ### "Static" part: no click on actionButton yet
  output$some_ui <- renderUI({
    imageOutput("image_plot")
  })

  output$image_plot <- renderImage({
    list(src = "Rplot.png",
         contentType = 'image/png')
  }, deleteFile = FALSE) # Do not forget this option



  ### Click on actionButton
  observeEvent(input$run, {
    output$some_ui <- renderUI({
      plotOutput("dynamic_plot")
    })

    output$dynamic_plot <- renderPlot({
      plot(mtcars[[input$choice]])
    })
  })

}

shinyApp(ui, server)

【讨论】:

  • 这两个答案都适用于这种情况。但不知道如何接受。
  • 新线程的任何线索。好像我在那里缺少简单的基础知识。
【解决方案2】:

关键是使用 renderUI,因此您可以显示图像或 R 绘图。这应该可以满足您的要求:

shinyApp(
  shinyUI(fluidPage(
    inputPanel( 
      numericInput("n", "n", 10),
      actionButton("update", "Update")
    ),
    uiOutput("out")
  )),

  shinyServer(function(session, input, output) {
    values <- reactiveValues()

    # check if plot has been already rendered
    check <- reactiveVal(FALSE)
    values$data <- c()

    observeEvent(input$update, {
      # set check to TRUE
      check(TRUE)
      input$update
      values$data <- c(values$data, runif(as.numeric(input$n), -10, 10))
      dat <- values$data
      output$plot <- renderPlot({
        hist(dat)
      })
    })

    # initial picture. 
    output$picture <- renderImage({
      list(src = "temp.png")
    }, deleteFile = FALSE)


    output$out <- renderUI({
      # in the  beginning, check is FALSE and the picture is shown
      if (!check()) {
        imageOutput("picture")
      } else {
        # as soon as the button has been pressed the first time,
        # the plot is shown
        plotOutput("plot")
      }


    })
  }) 

)

【讨论】:

  • 在这种情况下“temp.png”应该放在哪里?
  • 与您的 app.R 文件位于同一文件夹中。
  • ##render initial picture. heatmap_rna is the id for the uiOutput() in ui.R## output$heatmap_rna &lt;- renderImage({ list(src = "/www/heatmap.png") }, deleteFile = FALSE) ###generate plot upon action button## output$heatmap_rna &lt;- renderPlot({ if(input$action==0) return() print("heatmap_rna_renderPlot") heatmap_render() ##custom function which produces the heatmap })
  • 我已经根据我的需要调整了您以上述方式提供的代码。 renderPlot 由自定义函数heatmap_render() 生成。而不是observeEvent,我使用了input$action ==0。这有什么问题吗,因为上面的代码给出了错误Error in pngfun: invalid quartz() device size
  • 不知道如何在这里提供帮助。如果您无法修复它,请在 SO 上打开另一个问题
【解决方案3】:

我知道,这已经解决了一段时间,但我需要一个没有 uiOutput 的解决方案。另外,我发现这要简单得多。

library(shiny)

if (interactive()) {
  shinyApp(
    ui = fluidPage(
      actionButton("btn", "Click me"),
      br(),
      plotOutput('some_plot', width = '100%')
    ),
    server = function(input, output) {
      output$some_plot <- renderPlot({
        if (!input$btn) {
          # default plot
          plot(1, 1, col = 'red')
        } else{
          # updated plot
          plot(1000, 1000, col = 'green')
        }
      })
    }
  )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2022-12-10
    • 1970-01-01
    • 2020-08-09
    相关资源
    最近更新 更多