我正在努力找到一种方法来连接我命名的输入变量" plot1"到我所做的实际反应变量。我知道如何用ggvis(bind_shiny函数)做到这一点,但是我第一次使用ggplot并且不知道如何做到这一点。我知道反应变量vis中包含的代码在没有反应组件的情况下作为独立工作。任何帮助深表感谢!示例代码如下。
#Check packages to use in library
{
library('shiny') #allows for the shiny app to be used
library('ggplot2')
library('dplyr')
library('stringr') #string opperator
library('dplyr')
library('RSQLite')
}
#Data
horizontal_position <- c(-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)
ID_no <- 1
data_val <- sample(0:100, 25)
ID_1 <-data.frame(ID_no, horizontal_position, vertical_position, data_val)
ID_no <- 2
data_val <- sample(0:100, 25)
ID_2 <-data.frame(ID_no, horizontal_position, vertical_position, data_val)
all_data <-rbind(ID_1, ID_2)
IDchoices <- as.character(unique(all_data$ID_no))
# UI
ui <- fluidPage(
fluidRow(
column(2,
wellPanel(
selectInput(inputId = "ID", label="Select ID:", choices = IDchoices, selected = "1", multiple = FALSE, selectize = TRUE)
)
)
),
column(9,
wellPanel(plotOutput("plot1")
))
)
#SERVER
server <- function(input, output, session)
{
filteredData <- reactive({
m <- all_data %>% filter(
ID_no %in% input$ID
)
m
})
vis <- reactive({
p1 = filteredData() %>%
ggplot(aes(horizontal_position, vertical_position)) +
geom_tile(aes(fill = data_val)) +
geom_text(aes(label = data_val),colour="white")
})
}
#Run the Shiny App to Display Webpage
shinyApp(ui=ui, server=server)