ShinyApp :: Layout Inputs/Outputs

Published by onesixx on

https://shiny.rstudio.com/reference/shiny/latest/
http://deanattali.com/blog/building-shiny-apps-tutorial/
http://cfss.uchicago.edu/shiny001_abc.html

 

UI Inputs 

간단히,  ui.R 에서 Input객체와 Output객체를 적절하게 배치하고, 
server.R에서는 두 객체를 받아, 작성한 로직으로 프로세싱해서, 하나의 결과를 랜더링해준다. 
(Id, Label, Value)

ui.R   랜더링하여 HTML (컴마주의) server.R      R코딩 
UI Inputs UI Inputs (Update)
textInput , 
    textAreaInput
  updateTextInput , 
    updateTextAreaInput
numericInput   updateNumericInput
dateInput , 
    dateRangeInput
  updateDateInput , 
    updateDateRangeInput
checkboxInput,
     checkboxGroupInput,
<input … type=”checkbox”> updateCheckboxInput,
   updateCheckboxGroupInput
radioButtons   updateRadioButtons
selectInput(selectizeInput)   updateSelectInput
sliderInput(animationOptions)   updateSliderInput
actionButton (actionLink)
    (submitButton)
modalButton
observeEvent and eventReactive updateActionButton
fileInput    
passwordInput    
getQueryString(getUrlHash)   updateQueryString
     updateTabsetPanel
(updateNavbarPage,
  updateNavlistPanel)

checkboxInput,   checkboxGroupInput

library(shiny)

ui <- fluidPage(
    checkboxInput("checkYN", "Good", FALSE),
    verbatimTextOutput("checkVal"),
    
    checkboxGroupInput("variable", "Variables to show:",
        c("Cylinders" = "cyl", "Transmission" = "am", "Gears" = "gear")),
    tableOutput("data")
)

server <- function(input, output, session) {
    output$checkVal  <- renderText({ input$checkYN })
    
    output$data <- renderTable({
        mtcars[, c("mpg", input$variable), drop = FALSE]
    }, rownames = TRUE)
}

shinyApp(ui, server)

 

UI Outputs, Rendering functions

 

ui.R   랜더링하여 HTML (컴마주의) server.R      R코딩  
UI Outputs 설명 Rendering functions class명
textOutput  div 또는 span renderText
renderPrint
shiny-text-output
   verbatimTextOutput pre
plotOutput 
(imageOutput) 
div 또는 span renderPlot 
renderImage 
 
tableOutput 
(dataTableOutput)
div renderTable , 
renderDataTable
shiny-html-output
htmlOutput 

(uiOutput) 
div renderUI shiny-html-output
downloadButton
(downloadLink)
div downloadHandler  
Progress
withProgress(setProgress, incProgress)
     
modalDialog 
urlModal

showModal (removeModal) 
showNotification (removeNotification) 

     
outputOptions       

 

Download Link

library(shiny)

ui <- fluidPage(
    downloadLink("downloadData", "Download")
)

server <- function(input, output) {
    dataSet <- mtcars
    output$downloadData <- downloadHandler(
        filename = function(){ paste("data-", Sys.Date(), ".csv", sep="") },
        content  = function(file){ write.csv(dataSet, file) }
    )
}

shinyApp(ui, server)

progress
 http://onesixx.com/shiny-progress/ ‎

library(shiny)

ui <- fluidPage(
    plotOutput("plot")
)

server <- function(input, output, session) {
    output$plot <- renderPlot({
        # progress <- Progress$new(session, min=1, max=15)
        #     on.exit(progress$close())
        # progress$set(message = 'Calculation in progress', detail='This may take a while...')
        # for (i in 1:15) {
        #   progress$set(value = i)
        #   Sys.sleep(0.25)
        # }
        
        withProgress(
            message = 'Calculation in progress', detail = 'This may take a while...',
            value = 0, {
                for (i in 1:15) {
                    incProgress(1/15)
                    Sys.sleep(0.25)
                }
            }
        )
        
        plot(cars)
    })
}

shinyApp(ui, server)

modalDialog, showModal

library(shiny)

ui <- fluidPage(
    actionButton("show", "Show modal dialog")
)

server <- function(input, output, session) {
    observeEvent(input$show, {
        showModal(
            modalDialog(title="Important message", "This is an important message!")
        )
    })
}

shinyApp(ui, server)

 

UI Layout함수

Page – Layout- Panel

 Page함수 Layout 함수 Panel함수
navbarPage (navbarMenu)
fluidPage (fluidRow)
fixedPage (fixedRow)
sidebarLayout titlePanel
sidebarPanelmainPanel

tabsetPanel
navlistPanel
tabPanel
bootstrapPage (basicPage)
fillPage

pageWithSidebar
flowLayout
splitLayout
verticalLayout
headerPanel
inputPanel
wellPanel
helpText

absolutePanel(fixedPanel)
conditionalPanel
    column
fillRow (fillCol)
icon
withMathJax

 

 

 

예제

http://www.htmlwidgets.org/showcase_datatables.html

htmlwidgets을 핵심내용으로 사용. 

https://gist.github.com/ChrisBeeley/6571951
http://shiny.rstudio.com/gallery/widget-gallery.html
> runGist(6571951)

### data Types – ui.R
library(shiny)

shinyUI(pageWithSidebar(
 	headerPanel("Widget values and data types"),

 	sidebarPanel(
		checkboxGroupInput(
			inputId = "checkGroup",
			label = "1. checkboxGroupInput",
			choices = list("Ice cream" = "IC", "Trifle" = "Trifle","Pistachios" = "Pist")
		),
		checkboxInput(
			inputId = "boxInput",
			label = "2. checkboxInput"
		),
		dateInput(
			inputId = "theDate",
			label = "3. dateInput"
		),
		dateRangeInput(
			inputId = "dateRange",
			label = "4. dateRangeInput"
		),
		numericInput(
			inputId = "pickNumber",
			label = "5. numericInput",
			min = 1, max = 10, value = 1
		),
		radioButtons(
			inputId = "pickRadio",
			label = "6. radioButtons",
			choices = list("Taxi" = "Taxi", "Take a walk" = "Walk")
		),
		selectInput(
			inputId = "comboBox",
			label = "7. selectInput",
			choices = list("News" = "News", "Situation comedy" = "Sitcom", "Film" = "Film")
		),
		sliderInput(
			inputId = "slider",
			label = "8. sliderInput",
			min = 1, max = 10, value = 7, step = 1
		),
		textInput(
			inputId = "comment",
			label = "9. textInput",
			value = ""
		)
	),

	mainPanel(
		h3("Output and data type"),
		tableOutput("textDisplay")
	)
))

 

##### data types and values – server.R ###
library(shiny)

shinyServer(function(input, output){
 	output$textDisplay <- renderTable({
		getMat = matrix(
			c(
				paste(input$checkGroup, collapse=','), class(input$checkGroup),
				input$boxInput, class(input$boxInput),
				as.character(as.Date(input$theDate, origin = "1970-01-01")), class(input$theDate),
				paste(as.character(as.Date(input$dateRange[1], origin = "1970-01-01")),
					  as.character(as.Date(input$dateRange[2], origin = "1970-01-01")),
					  collapse = ','), class(input$dateRange),
				input$pickNumber, class(input$pickNumber),
				input$pickRadio, class(input$pickRadio),
				input$comboBox, class(input$comboBox),
				input$slider, class(input$slider),
				input$comment, class(input$comment)
			), ncol=2, byrow = TRUE)
	colnames(getMat) = c("Value", "Class")
	getMat
 	})
})

 

Reactive programming

 

저장 : Store values reactiveValues makeReactiveBinding

reactiveVal

reactiveValuesToList
is.reactivevalues
계산 : Calculate values reactive (is.reactive)  observeEvent
(eventReactive)
실행 : Execute tasks observe 
대기 : Preventing reactivity isolate  
Checking pre-conditions
( Check for required  values)
Utility functions ::  req  
Time (as a reactive source) invalidateLater reactiveTimer 
Rate-limiting debounce (throttle)  
Live data reactiveFileReader reactivePoll
    showReactLog

domains (getDefaultReactiveDomain, withReactiveDomain, onReactiveDomainEnded)

 

 

Categories: R-Shiny

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x