inst/theme_generator_app/modules/custom_setting.R

custom_setting_ui <- function(id = "custom_setting") {
  ns <- NS(id)

  SAMPLE_CODE <- paste(
    "# enter code here to generate sample graph",
    "# uploaded dataset will be assigned to variable \"data\"",
    "# for example, upload dataset iris here",
    "library(ggplot2)",
    "ggplot(data, aes(Sepal.Length, Sepal.Width)) +",
    "  geom_point()",
    sep = "\n"
  )

  sidebarLayout(
    sidebarPanel = sidebarPanel(
      fileInput(
        ns("data"),
        label = "Upload data",
        width = "100%"
      ),
      shinyAce::aceEditor(
        ns("code"),
        value = SAMPLE_CODE,
        fontSize = 6,
        tabSize = 2,
        readOnly = FALSE
      ),
      hr(),
      actionButton(
        ns("generate"),
        label = "Generate Sample Graph",
        width = "100%"
      )
    ),
    mainPanel = mainPanel(
      plotOutput(ns("plot"), height = HEIGHT) %>%
        .withSpinner()
    )
  )

}

custom_setting_server <- function(id = "custom_setting") {
  moduleServer(id, function(input, output, session) {

      observeEvent(input$generate, {
        req(input$data)
        output$plot <- renderCachedPlot({
          file <- input$data
          ext <- tools::file_ext(file$datapath)
          if (ext == "csv") {
            data <- read.csv(file$datapath)
          }
          custom_plot$plot <- eval(parse(text = isolate(input$code)))
          .get_plot(function() "custom")
        }, cacheKeyExpr = .cache_key())
      })

    }
  )
}
fanggong/themeGenerator documentation built on Dec. 20, 2021, 7:42 a.m.