Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions R/ggplot_shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ ggplot_shiny <- function( dataset = NA ) {
selectInput("group", "Group (or colour)", choices = ""),
selectInput("facet_row", "Facet Row", choices = ""),
selectInput("facet_col", "Facet Column", choices = ""),
checkboxInput(inputId = "factor_sort",
label = strong("Sort factor levels by their appearance in data"),
value = FALSE),
conditionalPanel(
condition = "input.Type == 'Boxplot' || input.Type == 'Violin' ||
input.Type == 'Dot + Error'",
Expand Down Expand Up @@ -173,11 +176,14 @@ ggplot_shiny <- function( dataset = NA ) {
type = "tabs",
tabPanel("Data upload", dataTableOutput("out_table")),
tabPanel("ggplot",
mainPanel(
downloadButton("download_plot_PDF",
"Download pdf of figure"),
plotOutput("out_ggplot"))
),
mainPanel(
div(style="display:inline-block;vertical-align:top;width:150px;", downloadButton("download_plot", "Download figure")),
div(style="display:inline-block;vertical-align:top;width:150px;", selectInput(inputId = "image_format",
label = NULL, choices = c("SVG", "PDF", "PNG"), selected = "SVG")
),
plotOutput("out_ggplot")
)
),
tabPanel("Plotly", plotlyOutput("out_plotly")),
tabPanel("R-code", verbatimTextOutput("out_r_code")),
tabPanel("Info",
Expand Down Expand Up @@ -448,7 +454,7 @@ p(
is.double(x),
df_shiny()))

# Make list of variables that are not factors
# Make list of variables that are factors
nms_fact <- names(Filter(function(x) is.factor(x) ||
is.logical(x) ||
is.character(x),
Expand Down Expand Up @@ -522,9 +528,28 @@ p(
} else if (input$data_input == 4){
data <- dataset
}
return(data)

return(data)
})


#####################################
####### DATA PROCESSING #############
#####################################

data_processing <- function(data) {
data <- as.data.frame(data)
if (input$factor_sort) {
nms_fact <- names(Filter(function(x) is.factor(x) || is.logical(x) || is.character(x), data))
for (colname in nms_fact) {
fcol <- data[,colname]
data[,colname] <- factor(fcol, levels = unique(fcol))
}
}
return(data)
}


#####################################
####### CREATE GRAPH-CODE ###########
#####################################
Expand Down Expand Up @@ -647,7 +672,7 @@ p(
)
}

# Replace name of variables by values
# Replace names of variables by values
p <- str_replace_all(
p,
c("input\\$y_var" = input$y_var,
Expand Down Expand Up @@ -699,13 +724,15 @@ p(
height = height, {
# evaluate the string RCode as code
df <- df_shiny()
df <- data_processing(df)
p <- eval(parse(text = string_code()))
p
})

output$out_plotly <- renderPlotly({
# evaluate the string RCode as code
df <- df_shiny()
df <- data_processing(df)
p <- eval(parse(text = string_code()))
ggplotly(p)
})
Expand Down Expand Up @@ -748,18 +775,19 @@ p(
#### GENERATE R-CODE FOR OUTPUT #####
#####################################

output$download_plot_PDF <- downloadHandler(
filename <- function() {
paste("Figure_ggplotGUI_", Sys.time(), ".pdf", sep = "")
output$download_plot <- downloadHandler(
filename = function() {
ext <- tolower(input$image_format)
paste0("Figure_ggplotGUI_", Sys.time(), ".", ext)
},
content <- function(file) {
content = function(file) {
df <- df_shiny()
df <- data_processing(df)
p <- eval(parse(text = string_code()))
ggsave(file, p, width = width_download(),
height = height_download(), units = "cm")
},
contentType = "application/pdf" # MIME type of the image
)
}
)

# End R-session when browser closed
session$onSessionEnded(stopApp)
Expand Down