【问题标题】:Reactive selection of a dataset in a Shiny appShiny 应用程序中数据集的反应式选择
【发布时间】:2019-12-02 15:17:40
【问题描述】:

我正在构建一个 R Shiny 应用程序,其中我有不同的数据集,我会根据用户的选择绘制一个或另一个图。

当我做出此选择时,我想从一个数据集更改为第二个数据集,但它目前无法正常工作。我试过reactiveobserveEventreactiveValues,但我想不通。

这是我所做的简化代码。

library(shiny)
library(plotly)
library(magrittr)

ui <- fluidPage(
             fluidRow(
               column(4,
                      wellPanel(
                        selectInput('dataset', 'Dataset', c('Synthetic 2M', 'Synthetic 4M'), 
                                    selected = 'Synthetic 2M'))
               ),
              column(8,
                     plotlyOutput('plot_result')
                     )))

server <- function(input, output, session) {
  #data load
  data2M <-data.frame(rnorm(100,0,1),rnorm(100,0,1),floor(runif(100,1,10)))
  data4M <-data.frame(rnorm(100,0,1),rnorm(100,0,1),floor(runif(100,1,10)))
  data <- data2M

  #dataset choice
  reactive({
    if (input$dataset == 'Synthetic 2M'){
      data <- data2M
    } else if (input$dataset == 'Synthetic 4M') {
      data <- data4M
    }
    else {}
  })

  #plot
  output$plot_result <- renderPlotly({
      names(data) <- c("x","y","cluster")
      plot_ly(data, x = ~x, y = ~y, color = ~cluster, text = data$cluster,
              type = "scatter", mode = "markers", hovertemplate = paste(
                "Coordinate: %{x},%{y}<br>",
                "Cluster: %{text}<br>"))})}
shinyApp(ui, server)

希望你们中的任何人都知道如何使它工作......

【问题讨论】:

    标签: r shiny dataset


    【解决方案1】:

    响应式是一个函数。您必须存储它,然后使用() 调用它。注意函数和变量之间的变量名称。

    这是一种方法:

    library(shiny)
    library(plotly)
    library(magrittr)
    
    ui <- fluidPage(
      fluidRow(
        column(4,
               wellPanel(
                 selectInput('dataset', 'Dataset', c('Synthetic 2M', 'Synthetic 4M'),
                             selected = 'Synthetic 2M'))
        ),
        column(8,
               plotlyOutput('plot_result')
        )))
    
    server <- function(input, output, session) {
      #data load
      data2M <-data.frame(rnorm(100,0,1),rnorm(100,0,1),floor(runif(100,1,10)))
      data4M <-data.frame(rnorm(100,0,1),rnorm(100,0,1),floor(runif(100,1,10)))
      data <- data2M
    
      #dataset choice
      data_plot <- reactive({
        if (input$dataset == 'Synthetic 2M'){
          return(data2M)
        } else if (input$dataset == 'Synthetic 4M') {
          return(data4M)
        } else {
          return(data)
        }
      })
    
      #plot
      output$plot_result <- renderPlotly({
        d <- data_plot()
        names(d) <- c("x","y","cluster")
        plot_ly(d, x = ~x, y = ~y, color = ~cluster, text = data$cluster,
                type = "scatter", mode = "markers", hovertemplate = paste(
                  "Coordinate: %{x},%{y}<br>",
                  "Cluster: %{text}<br>"))})}
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 2021-09-19
      相关资源
      最近更新 更多