Shiny :: Hello world

Published by onesixx on

https://www.youtube.com/watch?v=Ido56dwDTg8

 

Basic Template

 

 
# App Template
ui <- fluidPage()

server <- function(input, output)

shinyApp(ui=ui, server=server)

ex>

# #####################
# App Template
##########################
#UI Layout ::
	# absolutePanel (fixedPanel),bootstrapPage (basicPage),column,conditionalPanel,fillPage,
	# fillRow (fillCol),fixedPage (fixedRow),fluidPage (fluidRow),headerPanel,helpText,icon,mainPanel,
	# navbarPage (navbarMenu),navlistPanel,pageWithSidebar,sidebarLayout,sidebarPanel,tabPanel,tabsetPanel,
	# titlePanel,inputPanel,flowLayout,splitLayout,verticalLayout,wellPanel,withMathJax
ui <- fluidPage(
		# Title

		# UI Inputs:: *Input() functions ,
			# actionButton (actionLink),checkboxGroupInput,checkboxInput,dateInput,dateRangeInput,fileInput,
			# numericInput,radioButtons,selectInput (selectizeInput),sliderInput (nimationOptions),
			# submitButton,textInput,passwordInput,updateCheckboxGroupInput,updateCheckboxInput,
			# updateDateInput,updateDateRangeInput,updateNumericInput,updateRadioButtons,updateSelectInput
			# (updateSelectizeInput),updateSliderInput,
			# updateTabsetPanel (updateNavbarPage, updateNavlistPanel),updateTextInput

		# UI Outputs:: *Output() functions
			# htmlOutput (uiOutput),plotOutput (imageOutput), outputOptions,tableOutput (dataTableOutput),
			# textOutput,verbatimTextOutput, downloadButton (downloadLink),Progress,
			# withProgress(incProgress, setProgress)
	)

server <- function(input, output){
	# output$yyy <- render* functions({code input$xxx})
		# renderPlot,renderText,renderPrint,renderDataTable,renderImage,renderTable,renderUI,
		# downloadHandler

	# Reactive constructs
		# reactiveValues, is.reactivevalues,reactiveValuesToList,
		# reactive (is.reactive),makeReactiveBinding, showReactLog
		# observe,observeEvent (eventReactive),
		# isolate,
		# domains (getDefaultReactiveDomain, onReactiveDomainEnded,withReactiveDomain),invalidateLater
		# reactiveFileReader,reactivePoll,reactiveTimer,
}

shinyApp(ui=ui, server=server)

 

Hello World

library(shiny)

ui <- "Hello world"

server <- function(input,output,session){ }

shinyApp(ui, server)

 

ex 1>

# Hello world
library(shiny)

ui <- fluidPage(
  h1("Hello, World!"),
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "choose a dataset:",
                  choices = ls("package:datasets"),
                  selected = "pressure")
    ),
    mainPanel(
      textOutput("dump"),
      plotOutput("plot")
    )
  )
)

server <- function(input,output,session){
  output$dump <- renderPrint({
    dataset <- get(input$dataset, "package:datasets", inherits = FALSE)
    str(dataset)
  })
}

shinyApp(ui, server)

 

ex 2>

# Hello world
library(shiny)

ui <- fluidPage(
	h1("Hello, World!"),
	sidebarLayout(
		sidebarPanel(
			selectInput("dataset", "choose a dataset:",
				choices = ls("package:datasets"),
				selected = "pressure")
		),
		mainPanel(
		    tabsetPanel(
		        tabPanel("str", verbatimTextOutput("dump")),
		        tabPanel("plot", plotOutput("plot")),
		        tabPanel("table", tableOutput("table"))
		    )
		)
	)
)

server <- function(input,output,session){
	output$dump <- renderPrint({
		dataset <- get(input$dataset, "package:datasets", inherits = FALSE)
		str(dataset)
	})
	output$plot <- renderPlot({
	    dataset <- get(input$dataset, "package:datasets", inherits = FALSE)
	    plot(dataset)
	})
	output$table <- renderTable({
	    dataset <- get(input$dataset, "package:datasets", inherits = FALSE)
	    dataset
	})
}

shinyApp(ui, server)
> runApp('ex')
Listening on http://127.0.0.1:7823

 

ex 3>

library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(
   
    # Application title
    titlePanel("Old Faithful Geyser Data"),
   
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput(
                "bins",
                "Number of bins:",
                min = 1,
                max = 50,
                value = 30)
        ),
        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   
    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
      
        # draw the histogram with the specified number of bins
        p <- ggplot(faithful, aes(waiting)) + theme_bw() +
                geom_histogram(
                    aes(y =..density..),
                    breaks=bins, 
                    col="darkgray", 
                    fill="red", 
                    alpha = .2)  + 
                #geom_density(col=2) +
                #ggtitle("Histogram") + 
                labs(x="waiting", y="Freq")
        print(p)
        
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

 

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