Um blog sobre nada

Um conjunto de inutilidades que podem vir a ser úteis

Shiny

Posted by Diego em Janeiro 13, 2015


 

Shiny is a platform for creating interactive R programs embedded into a web page. I’ve been playing with it because it is part of the the “Developing Data Products” Coursera course.
This is a tutorial on how to create your first Shiny Application. It is a very simple one that will display n rows on the mtcars dataset.

Initially, it’s necessary to install the Shiny Package:

 

install.packages("shiny")
library(shiny)



A “shiny project” is simply a directory that needs to contain at least two files, the user interface (ui.R) where you specify how the application looks and the server (server.R) to controls what it does.

The project is run with the runApp() command. If we try to run it on an empty folder, we’ll get an error message:


> runApp()
Error in shinyAppDir(x): App dir must contain either app.R or server.R.

 


So what we need to do is to create both files on our working directory (or you can use any directory and its path as a parameter to the function). 

I’ve developed a very simple example that shows n rows of the mtcars dataset and summarizes its results.

 

This is how the ui.R file looks like:

 

shinyUI(

    pageWithSidebar(

        headerPanel("Hello Shiny – show me X rows on the mtcars dataset "),

       

        sidebarPanel(

            numericInput("amt", "Number of cars to view:", 5)

        ),

       

        mainPanel(

            verbatimTextOutput("summary"),

            tableOutput("myView")

        )

    )

And the server.R:

library(shiny)

library(datasets)

 

shinyServer(

                function(input, output) {

                                ds <- mtcars                      

 

                                output$myView <- renderTable({

                                                head(ds, n = input$amt)

                                })            

                               

                                output$summary <- renderPrint({                                          

                                                summary(head(ds, n = input$amt))

                                  })                          

                }

)

 

See that the variables amt, myView and summary are the communication variables between both “layers” (UI and Server). Calling runApp() will start the application:

> runApp()
 
Listening on http://127.0.0.1:5856

Distribution Shiny Apps:

 

A web App is not useful if you can’t distribute it. There are 2 main ways you can do that, using Shiny Server or a website like Shiny Apps.  Shiny Server runs on Linux only and I won’t be covering it here but all the information you need can be found on this link: http://www.rstudio.com/products/shiny/shiny-server/

If you want to deploy your app to the web, Shiny Apps (https://www.shinyapps.io) is a great option. Just go to the website, create an account and run the commands found here (https://www.shinyapps.io/admin/#/dashboard) on your R studio to install it.

 

 

Once you do it, all you have to do is call the deploy apss command and a browser will open with the application deployed:

 

> shinyapps::deployApp("c:\\git\\ShinyEx")

Preparing to deploy application…DONE

Uploading application bundle…DONE

Deploying application: 28469…

Waiting for task: 4160268

  building: Building image: 109550

  building: Installing packages

  building: Installing files

  building: Pushing image: 109550

  deploying: Starting instances

  rollforward: Activating new instances

  success: Stopping old instances

Application successfully deployed to http://dmenin.shinyapps.io/ShinyEx

 

This particular example can be found here: https://dmenin.shinyapps.io/ShinyEx/  

 

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão / Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão / Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão / Alterar )

Google+ photo

Está a comentar usando a sua conta Google+ Terminar Sessão / Alterar )

Connecting to %s

 
%d bloggers like this: