Saving & loading settings

parent 53ee89ce
read_data <- function(path, header, sep, quote) {
tryCatch(
{
df <- read.csv(path,
header,
sep,
quote)
},
error = function(e) {
## return a safeError if a parsing error occurs
stop(safeError(e))
}
)
return(df)
}
save_settings <- function(settings, path) {
json_settings = toJSON(settings)
print(path)
# if (!grepl('.*\\.json$', json_settings) ) {
# path = paste0(path,'.json')
# }
write(json_settings, path, append = FALSE)
print('downloaded')
}
read_settings <- function(path){
json_data <- fromJSON(file = path)
return(json_data)
}
test_settings_io <- function()
{
path = 'C:/Users/meechan/Documents/settings.json'
save_settings(c('a','b'), path)
read_settings(path)
}
getSettings <- function( s ) {
if ( exists("s") && !is.null(s) ) {
return(s)
} else {
return(NULL)
}
}
\ No newline at end of file
library(RJSONIO)
library(rjson)
source('./functions/io/data_io.R')
source('./functions/io/settings.R')
server <- function (input, output) {
data <- reactive({
if (is.null(input$datafile)) return(NULL)
## After the user selects and uploads a data file,
## the content of the data file will be shown.
req(input$datafile)
read_data(input$datafile$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
})
settings <- reactive({
if (is.null(input$settings_file)) return(NULL)
## After the user selects and uploads a data file,
## the content of the data file will be shown.
req(input$settings_file)
read_settings(input$settings_file$datapath)
})
output$content <- renderDT(data(), server = TRUE)
output$Paths <- renderUI({
checkboxGroupInput(inputId = "ImagePaths",
label = "Select Image columns",
choices = names(data()),
selected = getSettings(settings()) )
})
output$downloadData <- downloadHandler(
filename = function() {
return('Settings')
},
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
print(file)
save_settings(input$ImagePaths, file)
}
)
output$selected_paths = renderPrint({
s = input$ImagePaths
if (length(s)) {
cat('These variables were selected:\n\n')
cat(s, sep = ', ')
print(str(input$ImagePaths))
}
})
}
library('shiny')
ui <- navbarPage("",
tabPanel("Load",
fluidRow(
## Input: Select a file
fileInput("datafile", "Select data file (in CSV format)",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
## Horizontal line
tags$hr(),
## Input: Checkbox if file has header
checkboxInput("header", "File has header", TRUE),
## Input: Select separator
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = "\t"),
## Input: Select quotes
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'),
tags$hr()
),
fluidRow(
DTOutput("content")
),
fluidRow(title = 'settings',
fileInput("image", "Select an image file:",
multiple = FALSE,
accept = c('.png','.jpg','.tif','.jpeg')),
uiOutput("Paths"),
downloadButton('downloadData', 'Download'),
verbatimTextOutput('selected_paths'),
fileInput("settings_file", "Select settings json file:",
multiple = FALSE,
accept = c('.json'))
)
),
tabPanel("Plot")
)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment