Tho*_*ugh 1 r ggplot2 shiny shiny-reactivity
我正试图弄清楚如何在我的R Shiny项目中保存反应性ggplots.我已经按照本指南以及R Shiny网站上的指南进行了操作.但是,我想我可能会遇到一个问题,因为我正在使用反应情节.
这是我到目前为止的代码.
ui <- fluidPage(
dashboardBody(
fluidRow(uiOutput('topbox')),
fluidRow(
tabBox(
id = 'tabset1',
width = '100%',
tabPanel('Grades Graph', downloadButton('Download'), plotOutput('individualGraph')),
)
)
)
)
server <- function(input, output, session) {
grades <- reactive({
req(input$file)
inFile <- input$file
if (endsWith(inFile$name, '.xlsx')){
gradesTbl <- read_excel(inFile$datapath)
gradesTbl <- gradesTbl %>%
arrange(Period, Student, Date, Type) %>%
mutate(Date = as.Date(Date))
return(gradesTbl)
} else if (endsWith(inFile$name, 'csv')){
gradesTbl <- read_csv(inFile$datapath)
gradesTbl <- gradesTbl %>%
arrange(Period, Student, Date, Type) %>%
mutate(Date = mdy(Date))
return(gradesTbl)
}
})
output$Download <- downloadHandler(
filename = function(){
paste('test', '.png', sep = '')
},
content = function(file){
ggsave(file, plot = output$individualGraph, device = 'png')
}
)
indivdf <- function(){
data.frame(grades()) %>%
filter((Student == input$studentVar) & (Period == input$periodVar) & (Type %in% input$typeVar) & (Unit %in% input$unitVar))
}
output$individualGraph <- renderPlot({
req(input$periodVar)
indivdf() %>%
ggplot(aes(x = Date, y = Grade,
color = Type, shape = Unit)) +
geom_point(size = 4) +
ggtitle(paste(input$studentVar, "'s Individual Grades", sep = '')) +
plotTheme() +
scale_shape_manual(values = 1:10) +
facet_wrap(Unit~.) +
scale_color_manual(values = c('#E51A1D', '#377DB9', '#4EAE4A'))
})
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
完整的代码在这里,但我认为这是显示我正在尝试做的一切.我只是无法弄清楚如何保存这些表和反应图.我觉得这与使用'plot = output $ indididualGraph'有关,但我真的不知道.
小智 6
您需要使用代码生成绘图并将其从renderPlota 移动到reactive.然后你可以reactive从内部调用相同的内容renderPlot来在UI中显示图形,并从中downloadHandler下载图表.
请参阅下面的示例.对reactive(individualGraph())和输出return(output$individualGraph)使用相同的变量名称可能不是最好的编码实践,但我发现它更方便.
server <- function(input, output, session) {
individualGraph <- reactive({
req(input$periodVar)
indivdf() %>%
ggplot(aes(x = Date, y = Grade,
color = Type, shape = Unit)) +
geom_point(size = 4) +
ggtitle(paste(input$studentVar, "'s Individual Grades", sep = '')) +
plotTheme() +
scale_shape_manual(values = 1:10) +
facet_wrap(Unit~.) +
scale_color_manual(values = c('#E51A1D', '#377DB9', '#4EAE4A'))
})
output$individualGraph <- renderPlot({
req(individualGraph())
individualGraph()
})
output$Download <- downloadHandler(
filename = function(){
paste('test', '.png', sep = '')
},
content = function(file){
req(individualGraph())
ggsave(file, plot = individualGraph(), device = 'png')
}
)
grades <- reactive({
req(input$file)
inFile <- input$file
if (endsWith(inFile$name, '.xlsx')){
gradesTbl <- read_excel(inFile$datapath)
gradesTbl <- gradesTbl %>%
arrange(Period, Student, Date, Type) %>%
mutate(Date = as.Date(Date))
return(gradesTbl)
} else if (endsWith(inFile$name, 'csv')){
gradesTbl <- read_csv(inFile$datapath)
gradesTbl <- gradesTbl %>%
arrange(Period, Student, Date, Type) %>%
mutate(Date = mdy(Date))
return(gradesTbl)
}
})
indivdf <- function(){
data.frame(grades()) %>%
filter((Student == input$studentVar) & (Period == input$periodVar) & (Type %in% input$typeVar) & (Unit %in% input$unitVar))
}
}
Run Code Online (Sandbox Code Playgroud)