How to upload data for analysis in shiny - r

I would like to add to it a feature for uploading data. I tried conditional panels but no luck. I attach my code below. What I did was I included the simulated data before the ui part of shiny and constructed the rest of the code. Thanks
library(shiny)
dat # data
ui
ui <- fluidPage(
pageWithSidebar(
headerPanel('Correlation coefficient and scatter plots'),
sidebarPanel(

Are you looking for something like fileInput? See http://shiny.rstudio.com/gallery/upload-file.html

Related

How to run user input as R code in a Shiny app?

I want to create a shiny application that has an input for writing some R function or Command, reads it through the ui.R then passes it to the server.R that executes that R command to display the results.
I spent hours searching about some example but couldn't find anything, I already know how to create Shiny apps using ui and server and pass the input values to server and work with them, but I have no idea if it's possible to create a shiny app like R where you can write the commands and return the results, any example or help would be appreciated.
Letting users run code in your app is bad practice, since it comes with great security risks. However, for development you might want to check this function from the shinyjs package by Dean Attali.
Example from the link:
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(), # Set up shinyjs
runcodeUI(code = "shinyjs::alert('Hello!')")
),
server = function(input, output) {
runcodeServer()
}
)
Some examples of why it is not such a good idea to include when deploying your app:
Try the input:
shinyjs::alert(ls(globalenv()))
or
shinyjs::alert(list.files())
I was able to find an alternative solution that doesn't require shinyjs -- wanted to restate Florian's concern that in general it is not a good thing (not secure) to let users run code in your Shiny app. Here is the alternative:
library(shiny)
library(dplyr)
ui <- fluidPage(
mainPanel(
h3("Data (mtcars): "), verbatimTextOutput("displayData"),
textInput("testcode", "Try filtering the dataset in different ways: ",
"mtcars %>% filter(cyl>6)", width="600px"),
h3("Results: "), verbatimTextOutput("codeResults"))
)
server <- function(input, output) {
shinyEnv <- environment()
output$displayData <- renderPrint({ head(mtcars) }) # prepare head(mtcars) for display on the UI
# create codeInput variable to capture what the user entered; store results to codeResults
codeInput <- reactive({ input$testcode })
output$codeResults <- renderPrint({
eval(parse(text=codeInput()), envir=shinyEnv)
})
}
shinyApp(ui, server)

D3partitionROutput non-reactive to changing output

D3partitionR has some fantastic visualisations for hierarchical and sequential data, however it seems to have a major flaw in Shiny.
The D3partitionROutput function (& renderD3partitionR) don't update the plotted object when the output object is updated.
The functions work perfectly on first execution of a graph however the plotted objects can't be reactively updated.
Does anyone know of a fix or workaround as I really like this package's visualisations?
library(shiny)
library(D3partitionR)
path_in=list(list("A","B","C","D"),list("A","B","F","G"),list("A","D"),list("A","B","END"))
value_in=c(15,29,21,34)
# 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(
selectInput("type_in",
"Plot type:",
choices = c('circleTreeMap', 'partitionChart', 'treeMap')
)
),
# Show a plot of the generated distribution
mainPanel(
D3partitionROutput("part_out")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$part_out <- renderD3partitionR({
type = input$type_in
D3partitionR(data=list(path=path_in,value=value_in)
, type = type)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Update:
I found a similar bug here in Rwordcloud that was resolved by modifying the "render" function in Rwordcloud.js. I looked into renderValue in D3partitionR.js and the function doesn't take an input of 'instance' (as is done in Rwordcloud.js) so it seems it doesn't know when to delete / refresh renderValue. I'm an R guy (and have no js experience) so I don't know how the renderValue function should be changed in D3partitionR.js however I'm pretty sure this is the source of the problem.. Help!

Remove top header of Shiny application deployed on ShinyApps

I have deployed an application on ShinyApps, but my application has an extra header when deployed.
The header looks like this:
However, I would like to remove this header when the application is deployed. Is it possible to do this? I am using a sample shiny web application from the tutorials.
server.R:
library(shiny)
# Rely on the 'WorldPhones' dataset in the datasets
# package (which generally comes preloaded).
library(datasets)
# Define a server for the Shiny app
shinyServer(function(input, output) {
# Fill in the spot we created for a plot
output$phonePlot <- renderPlot({
# Render a barplot
barplot(WorldPhones[,input$region]*1000,
main=input$region,
ylab="Number of Telephones",
xlab="Year")
})
})
ui.R:
library(shiny)
# Rely on the 'WorldPhones' dataset in the datasets
# package (which generally comes preloaded).
library(datasets)
# Define the overall UI
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Telephones by region"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("region", "Region:",
choices=colnames(WorldPhones)),
hr(),
helpText("Data from AT&T (1961) The World's Telephones.")
),
# Create a spot for the barplot
mainPanel(
plotOutput("phonePlot")
)
)
)
)
You're not supposed to do that. If you use a paid version of shinyapps.io then you won't have this bar, but if you're using the free version they add it as advertising because they do need to make some money somehow.
(It is possible to remove it, but I really like RStudio and all their work so I don't want to promote ways to make them lose business, sorry...)
If you look at the different plans and pricing options, it clearly says that the free version includes their logo branding
If you want to remove it then you have to pay for it, it's added on the server side. Your other option is to set up a shiny server with Digital Ocean https://www.digitalocean.com/community/tutorials/how-to-set-up-shiny-server-on-ubuntu-14-04

gvisTables not rendering in Shiny apps

The actual issue I'm trying to solve: I'm creating a dashboard which will include data tables. I would like for numbers to be formatted with commas as thousands separators, but there is (apparently) an issue with the DT package when it's used with Shiny, in that the comma-separated formatting causes DT::renderDataTable to read in numbers as character, which affects how the numbers are sorted. (DT's number formatting functionality does not work with Shiny, it appears.)
Where I'm at so far: The only solution I've been able to find is to use googleVis instead of DT to create the tables. Now I'm running into a different issue (described below), but what I really care about is having data tables with comma-separated numbers that sort like numbers.
The GoogleVis issue: When I use gvisTable outside of Shiny apps, they render perfectly fine, but they do not render at all when using renderGvis and htmlOutput in Shiny. As an example, I'll borrow Example 4 from here.
Not using Shiny, my code looks like this:
library(datasets)
library(googleVis)
myOptions <- list(page='enable', pageSize=10, width=550)
Table <- gvisTable(Population,options=myOptions)
plot(Table)
Using Shiny, it's like this:
library(datasets)
library(googleVis)
library(shiny)
shinyApp(
ui = pageWithSidebar(
headerPanel("Example 4: pageable table"),
sidebarPanel(
checkboxInput(inputId = "pageable", label = "Pageable"),
conditionalPanel("input.pageable==true",
numericInput(inputId = "pagesize",
label = "Countries per page",10))
),
mainPanel(
htmlOutput("myTable")
)
),
server = function(input,output){
myOptions <- reactive({
list(
page=ifelse(input$pageable==TRUE,'enable','disable'),
pageSize=input$pagesize,
width=550
)
})
output$myTable <- renderGvis({
gvisTable(Population,options=myOptions())
})
}
)
Any help is much appreciated!
I solved my own problem. It turns out that RStudio's native browser has difficulty displaying googleVis exhibits through Shiny. All I needed to do was open it up in Firefox... I don't think I've ever felt so much "woot" and "ugh" at the same time before.

display different number of tabPanels in a navbarPage in Shiny, without renderUI or uiOutput

I am trying to display from 1 to 5 tabPanels in a navbarPage in Shiny.
I have 5 plots my code generates, but I'd like a user to be able to select how many they want to have access to -- to be displayed one plot in each tabPanel, naturally.
I've got an external configuration file (config.txt) that via source('config.txt'), I have access to a number_of_pages variable.
For example, number_of_tabPages <- 3
How would I set this up in UI.R?
The number of tabPanels can't be hardcoded at all in the UI file, since it depends on a value that is specified by a user, not using a control.
I've searched around and found that most of the approaches to this kind of thing
involve using uiOutput and renderUI functions, such as this similar problem, but I don't want any special control in the UI to do any selecting.
This is where things get tricky, when we are building the UI depending on values that may change. My brain is trying to wrap itself around the best method for doing this type of thing -- I feel like it isn't exactly in line with how Shiny wants to communicate with itself using a UI <--> server environment.
Any advice is greatly appreciated.
My UI.R is easy to create when it isn't dynamic:
fluidRow(
column(12,
"",
navbarPage("",tabPanel("First Tab",
plotOutput("plot1")),
tabPanel("Second Tab",
plotOutput("plot2")),
tabPanel("Third Tab",
plotOutput("plot3")),
tabPanel("Fourth Tab",
plotOutput("plot4")),
tabPanel("Fifth Tab",
plotOutput("plot5"))
)
)
)
)
Thanks!
If you don't need the user to change the number of tabPanel interactively, but just load varying numbers of them when the app is started you can use the do.call function in the navBarPage:
library(dplyr)
library(shiny)
library(ggvis)
#number of tabs needed
number_of_tabPages <- 10
#make a list of all the arguments you want to pass to the navbarPage function
tabs<-list()
#first element will be the title, empty in your example
tabs[[1]]=""
#add all the tabPanels to the list
for (i in 2:(number_of_tabPages+1)){
tabs[[i]]=tabPanel(paste0("Tab",i-1),plotOutput(paste0("plot",i-1)))
}
#do.call will call the navbarPage function with the arguments in the tabs list
shinyUI(fluidRow(
column(12,
"",
do.call(navbarPage,tabs)
)
)
)

Resources