R/Shining:仅在单击操作按钮后更改绘图

R/Shining:仅在单击操作按钮后更改绘图,r,shiny,R,Shiny,我正在设置一个小的闪亮的应用程序,我不想改变情节,除非点击动作按钮。在下面的示例中,当我第一次运行应用程序时,在单击“操作”按钮之前,没有绘图。但是,如果我随后将下拉列表中的菜单选项从“直方图”更改为“散点”,则散点图将自动显示,即使输入$show_plot的值没有更改,因为没有单击“操作”按钮 是否有一种方法可以将我的菜单选择从直方图更改为散点,但在单击“操作”按钮之前不会更改绘图?我读了好几篇不同的帖子和文章,但似乎无法解决这个问题 谢谢你的意见 用户界面 服务器.R library(shi

我正在设置一个小的闪亮的应用程序,我不想改变情节,除非点击动作按钮。在下面的示例中,当我第一次运行应用程序时,在单击“操作”按钮之前,没有绘图。但是,如果我随后将下拉列表中的菜单选项从“直方图”更改为“散点”,则散点图将自动显示,即使输入$show_plot的值没有更改,因为没有单击“操作”按钮

是否有一种方法可以将我的菜单选择从直方图更改为散点,但在单击“操作”按钮之前不会更改绘图?我读了好几篇不同的帖子和文章,但似乎无法解决这个问题

谢谢你的意见

用户界面

服务器.R

library(shiny)
library(ggplot2)

set.seed(10)

function(input, output, session) {

  ### GENERATE SOME DATA ###
  source_data <- reactive({
    mydata1 = as.data.frame(rnorm(n = 100))
    mydata2 = as.data.frame(rnorm(n = 100))
    mydata = cbind(mydata1, mydata2)
    colnames(mydata) <- c("value1","value2")
    return(mydata)
  })

  # get a subset of the data for the histogram
  hist_data <- reactive({
      data_sub = as.data.frame(source_data()[sample(1:nrow(source_data()), 75), "value1"])
      colnames(data_sub) <- "value1"
      return(data_sub)
  })

  # get a subset of the data for the scatter plot
  scatter_data <- reactive({
      data_sub = as.data.frame(source_data()[sample(1:nrow(source_data()), 75),])
      return(data_sub)
  })


  ### MAKE SOME PLOTS ###
  observeEvent(input$show_plot,{
    output$plot_histogram <- renderPlot({
      isolate({
        plot_data = hist_data()
        print(head(plot_data))
        p = ggplot(plot_data, aes(x = value1, y = ..count..)) + geom_histogram()
        return(p)

      })
    })
  })

  observeEvent(input$show_plot,{
    output$plot_scatter <- renderPlot({
      isolate({
        plot_data = scatter_data()
        print(head(plot_data))
        p = ggplot(plot_data, aes(x = value1, y = value2)) + geom_point()
        return(p)

      })
    })
  })

}
库(闪亮)
图书馆(GG2)
种子(10)
功能(输入、输出、会话){
###生成一些数据###

源数据基于您想要的行为我认为根本不需要
actionButton()
。如果您想根据用户输入更改绘图,那么
selectinput()
conditionPanel()
的组合已经为您做了

另一方面,在任何reactive中都有输出绑定是不好的做法。这里是服务器代码的一个改进版本。我认为您可以看到这些更改,但如果有任何问题,请进行注释-

function(input, output, session) {

  ### GENERATE SOME DATA ###
  source_data <- data.frame(value1 = rnorm(n = 100), value2 = rnorm(n = 100))

  # get a subset of the data for the histogram
  hist_data <- reactive({
    # reactive is not needed if no user input is used for creating this data
    source_data[sample(1:nrow(source_data), 75), "value1", drop = F]
  })

  # get a subset of the data for the histogram
  scatter_data <- reactive({
    # reactive is not needed if no user input is used for creating this data
    source_data[sample(1:nrow(source_data), 75), , drop = F]
  })

  ### MAKE SOME PLOTS ###
  output$plot_histogram <- renderPlot({
    req(hist_data())
    print(head(hist_data()))
    p = ggplot(hist_data(), aes(x = value1, y = ..count..)) + geom_histogram()
    return(p)
  })

  output$plot_scatter <- renderPlot({
    req(scatter_data())
    print(head(scatter_data()))
    p = ggplot(scatter_data(), aes(x = value1, y = value2)) + geom_point()
    return(p)
  })
}
功能(输入、输出、会话){
###生成一些数据###

源数据在您想要的行为中,
actionButton()
有什么额外的用途?如果您想根据用户输入更改绘图,那么
selectinput()
conditionPanel()
的组合已经为您做了。我认为没有必要使用
actionButton()
没问题。@Shree您好,谢谢您的回复。我同意在本例中,使用“操作”按钮有点无用。这是一个非常简单的示例,说明了我真正在做什么。如果菜单中有其他输入选项(其中一些选项可能是必需的,也可能不是必需的),我不希望发生任何操作(显示散点图)直到单击绘图按钮。在这种情况下,基于其他输入的计算将自动重新计算图形。如果有多个输入,则可以在
eventReactive()中执行数据操作
,然后将输出馈送到绘图,这样绘图仅在按下
操作按钮时更新。无需使绘图消失,只会使代码复杂化。
function(input, output, session) {

  ### GENERATE SOME DATA ###
  source_data <- data.frame(value1 = rnorm(n = 100), value2 = rnorm(n = 100))

  # get a subset of the data for the histogram
  hist_data <- reactive({
    # reactive is not needed if no user input is used for creating this data
    source_data[sample(1:nrow(source_data), 75), "value1", drop = F]
  })

  # get a subset of the data for the histogram
  scatter_data <- reactive({
    # reactive is not needed if no user input is used for creating this data
    source_data[sample(1:nrow(source_data), 75), , drop = F]
  })

  ### MAKE SOME PLOTS ###
  output$plot_histogram <- renderPlot({
    req(hist_data())
    print(head(hist_data()))
    p = ggplot(hist_data(), aes(x = value1, y = ..count..)) + geom_histogram()
    return(p)
  })

  output$plot_scatter <- renderPlot({
    req(scatter_data())
    print(head(scatter_data()))
    p = ggplot(scatter_data(), aes(x = value1, y = value2)) + geom_point()
    return(p)
  })
}