Skip to content

Instantly share code, notes, and snippets.

@corynissen
Last active August 29, 2015 14:06
Show Gist options
  • Save corynissen/340d13bcaa55de070b35 to your computer and use it in GitHub Desktop.
Save corynissen/340d13bcaa55de070b35 to your computer and use it in GitHub Desktop.
save reactiveValues
library(shiny)
library(RCurl)
shinyServer(function(input, output, session){
urls <- reactiveValues()
if(file.exists("data.Rdata")){
load("data.Rdata")
urls$url <- df$url
urls$isbn <- df$isbn
urls$link <- df$link
urls$comment <- df$comment
}else{
urls$url <- c()
urls$isbn <- c()
urls$link <- c()
urls$comment <- c()
}
observe({
input$submit
isolate({
url <- input$url
isbn <- getISBN(url)
comment <- input$comment
link <- getImageLink(url, isbn)
urls$url <- c(urls$url, url)
urls$isbn <- c(urls$isbn, isbn)
urls$link <- c(urls$link, link)
urls$comment <- c(urls$comment, comment)
df <- as.data.frame(cbind(url=as.character(urls$url),
isbn=as.character(urls$isbn),
comment=as.character(urls$comment),
link=as.character(urls$link)), stringsAsFactors=F)
save(df, file="data.Rdata")
return(urls)
})
})
getISBN <- function(url){
tmp <- getURL(url)
a <- regexpr("ISBN-13", tmp)
isbn <- substring(tmp, a + 13, a + 26)
isbn <- gsub("-", "", isbn)
return(isbn)
}
getImage <- function(isbn){
url <- "http://content-3.powells.com/cgi-bin/imageDB.cgi?isbn="
full_url <- paste0(url, isbn)
download.file(full_url, destfile=paste0("images/", isbn, ".jpg"))
}
getImageLink <- function(url, isbn){
# input: amazon book url
# output: HTML image link for book
html <- paste0('<a href=', url, '><img src="http://content-3.powells.com/cgi-bin/imageDB.cgi?isbn=', isbn,'"></a>')
return(html)
}
output$books <- renderUI({
HTML(paste(urls$link, collapse="</br>"))
})
})
library(shiny)
shinyUI(pageWithSidebar(
titlePanel("Reading List"),
sidebarPanel(
tags$head(
tags$style(type="text/css", "label.radio { display: inline-block; }", ".radio input[type=\"radio\"] { float: none; }"),
tags$style(type="text/css", "select { max-width: 200px; }"),
tags$style(type="text/css", "textarea { max-width: 185px; }"),
tags$style(type="text/css", ".jslider { max-width: 200px; }"),
tags$style(type='text/css', ".well { max-width: 250px; }"),
tags$style(type='text/css', ".span4 { max-width: 250px; }")
),
textInput("url", "Enter URL here", value=""),
textInput("comment", "Enter comment here", value=""),
actionButton("submit", "Submit")
),
mainPanel(
uiOutput("books")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment