[英]shiny reactive plotly 3D surface plot
我有一個閃亮的應用程序,我想根據用戶的選擇顯示不同項目的3D表面圖。 我的數據存儲在一個數據框中。 當前,我將此數據更改為列表,因為我在繪圖中看到的所有示例都使用列表(尤其是此處和此處的示例),但是如果有一種方法可以將數據框與繪圖表面圖一起使用,那將是更好地組織我的數據。 我遇到的麻煩是如何使變量filteredList具有反應性,以便從已過濾的數據中篩選出的數據創建一個列表,該列表僅保留具有用戶選擇的項目編號的數據條目。 我已經硬編碼了一個案例,該案例僅選擇第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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.