Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make ShinyApp to use environmental variables when deployed on the web?

Tags:

r

shiny

For hours I've been struggling. My Shiny App is supposed to display some variables that I have in my R environment. It works all works fine but when I deploy it to the web I get errors like:

Error: object 'df1' not found

How can I add df1 and my other data frames so they can be packaged as part of the Shiny App when it is deployed?

Please help. Here's my sample code:

server.R

library(shiny)

shinyServer(function(input,output){
output$datasets <- renderTable({
switch(input$choice,

         "1" = as.data.frame(df1)          
         "2" = as.data.frame(df2) })
  }))

UI.R

shinyUI(
fluidPage(theme = "bootstrap.css",

sidebarPanel(        
  conditionalPanel(
    condition = "input.theTab == 'datasets' ",
    h3('Display Sample Data'),    
    selectInput("choice", "Selection", choices = c("Group1"=1,"Group2"=2)),

  )),

mainPanel(
  tabsetPanel(
    tabPanel( "datasets", tableOutput("datasets"), value = 'datasets'),
    id = "theTab"))
)
like image 660
Tavi Avatar asked Sep 03 '14 22:09

Tavi


People also ask

How do you load data on the shiny app?

Your title asks about importing a data frame into shiny. That can be done by storing the data frame either as a binary file using the save() function or a csv file using write. csv() and having the shiny app read it in using load() for a binary file or read. csv() for a csv file.

Can you run shiny app without R?

You can create a standalone shiny app, that runs on computers WITHOUT needing to install R nor any library.


2 Answers

In recent shiny versions you can include variables in a global.R file, and those will be available for ui and server. Take a look at the scoping rules here:

http://shiny.rstudio.com/articles/scoping.html

like image 188
ddiez Avatar answered Oct 21 '22 09:10

ddiez


Finally figured out the solution!! basically I was supposed to load my workspace at the top of the UI.R file. This way:

attach("myWorkspace.RData")
like image 41
Tavi Avatar answered Oct 21 '22 10:10

Tavi