r-闪亮的反应性图3D表面图



我有一个闪亮的应用程序,我想根据用户选择的内容显示其他项目的3D表面图。我的数据存储在数据框架中。当前,我将此数据更改为列表,因为我在Plotly使用列表中看到的所有示例(尤其是此处和此处的示例(,但是如果有一种方法可以将数据框架与Plotly Surface绘图一起使用,那就是更好地组织了我的数据。我遇到的麻烦是如何使变量过滤列表具有反应性,以便从从FilteredData中过滤的数据创建列表,该数据仅将数据输入与用户选择的项目编号保持。我已经对一个只挑选项目1的案例进行了硬编码,并绘制了我希望显示的方式,但对用户所需的输入没有反应。我的代码中的下一行(注释(是我尝试使情节反应性。非常感谢可以提供的任何帮助!

#Check packages to use in library
  {
    library('shiny') #allows for the shiny app to be used
    library('ggplot2')
    library('plotly')
  }
  #Data
  horizontal_position <- c(-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2)
  vertical_position <- c(-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
  data_val <- sample(0:100, 25)
  item_no <- as.character("Item 1")
  item_1 <-data.frame(item_no, horizontal_position, vertical_position, data_val)
  horizontal_position <- c(-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2)
  vertical_position <- c(-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
  data_val <- sample(215:270, 25)
  item_no <- as.character("Item 2")
  item_2 <-data.frame(item_no, horizontal_position, vertical_position, data_val)
  item_data <-rbind(item_1, item_2) #dataframe containing both items
  itemchoices <- as.character(unique(item_data$item_no)) 
  # UI
  ui <- fluidPage(
    column(2,
    selectInput(inputId = "Item", label="Select Item:", choices = itemchoices, selected = "Item 1", multiple = FALSE, selectize = TRUE)
    ),
      column(4,
             plotlyOutput("plot3")
      )
    )
  #SERVER
  server <- function(input, output, session)
  {
    filteredData <- reactive({
      m <- item_data %>% filter(
        item_no %in% input$Item
      )
      m
    })
    filteredList <- list(x = unique(item_data[1:25,'horizontal_position']), y = unique(item_data[1:25,'vertical_position']), z = matrix(item_data[1:25,'data_val'], nrow = length(unique(item_data[1:25,'horizontal_position']))))
    # filteredList <- reactive({list(x = unique(filteredData()$horizontal_position), y = unique(filteredData()$vertical_position), z = matrix(filteredData()$data_val, nrow = length(unique(filteredData()$horizontal_position))))})

    output$plot3 <- renderPlotly({
      plot_ly(x=filteredList$x, y = filteredList$y, z= filteredList$z) %>% add_surface()
    })
  }
  #Run the Shiny App to Display Webpage
  shinyApp(ui=ui, server=server)

我正在处理一个执行同样事情的应用程序。在这里查看。我的代码在github上:https://github.com/jeffreyxparker/3d-shiny-tool

最新更新