我有一个闪亮的应用程序,我想要显示一个3D曲面图为不同的项目取决于用户选择。我的数据存储在数据帧中。目前,我将此数据更改为列表,因为我见过的所有示例都使用列表(特别是here和here示例),但如果有一种方法可以将数据框与曲面图一起使用,那么对于我的数据的组织方式会更好。我遇到的问题是如何将变量filteredList设置为响应式的,以便从filteredData中筛选出的数据创建一个列表,该列表只保留具有用户选择的项目编号的数据条目。我已经硬编码了一个案例,它只挑选出第一项,并绘制出我想要的显示方式,但不会对用户想要的输入做出反应。我的代码中的下一行(注释掉)是我试图使该图具有响应性。任何可以提供的帮助都是非常感谢的!
#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)发布于 2017-07-18 04:48:24
我正在开发一款做同样事情的应用程序。看看吧,here。我的代码在GitHub上:https://github.com/jeffreyxparker/3D-Shiny-Tool
https://stackoverflow.com/questions/44269752
复制相似问题