shiny,uploading , Shiny: Show buttons only after file has been uploaded


Shiny: Show buttons only after file has been uploaded

Question:

Tag: shiny,uploading

I'm experimenting with Shiny and I love it. I built a little application where students upload a csv file and then choose a dependent variables and independent variables and then R computes a linear regression. It works fine. I have it uploaded at:

http://carlosq.shinyapps.io/Regresion

[You can use this file to test it if you want. "beer" is the dependent variable and the rest of the variables except "id" are the independent]

Here's server.R:

# server.R
library(shiny)

shinyServer(function(input, output) {

  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })

  output$dependent <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("dependent","Select ONE variable as dependent variable from:",items)
  })


  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE)
  })


  output$contents <- renderPrint({
    input$action
    isolate({   
      df <- filedata()
      if (is.null(df)) return(NULL)
      fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
      summary(lm(fmla,data=df))
    })   
  })

}) 

And here is the ui.R:

# ui.R
library(shiny)

shinyUI(fluidPage(
  titlePanel("Multiple Linear Regression"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      tags$hr(),
      uiOutput("dependent"),
      uiOutput("independents"),
      tags$hr(),
      actionButton("action", "Press after reading file and selecting variables")

    ),
    mainPanel(
      verbatimTextOutput('contents')
    )
  )
))

My question is: I want to make the appearance of the button "Press After Reading File and selecting variables " conditional on a succesful uploading.

I have tried to use the suggestion contained here:

Make conditionalPanel depend on files uploaded with fileInput

But I just can't make it work.

I appreaciate any help.


Answer:

This code worked for me

ui.R

 # ui.R
library(shiny)

shinyUI(fluidPage(
  titlePanel("Multiple Linear Regression"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      tags$hr(),
      uiOutput("dependent"),
      uiOutput("independents"),
      tags$hr(),
      uiOutput('ui.action') # instead of conditionalPanel
    ),
    mainPanel(
      verbatimTextOutput('contents')
    )
  )
))

server.R

# server.R
library(shiny)

shinyServer(function(input, output) {

  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })

  output$dependent <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("dependent","Select ONE variable as dependent variable from:",items)
  })


  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE)
  })


  output$contents <- renderPrint({
    input$action
    isolate({   
      df <- filedata()
      if (is.null(df)) return(NULL)
      fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
      summary(lm(fmla,data=df))
    })   
  })


  output$ui.action <- renderUI({
    if (is.null(input$file1)) return()
    actionButton("action", "Press after reading file and selecting variables")
  })

}) 

Related:


Shiny + GGplot - mouse click coordinates


ggplot2,shiny,rstudio
I have a doubt in shiny ploting a GGPlot Bar Graph. I can recognize coordinates of mouse click (x,y), but I need know a value of bar (x-axis) to refresh the graph with parameter and simulate a drill-down. Anyone can help me? library(shiny) library(ggplot2) ui <- fluidPage( plotOutput("plot", click =...

How to build a 'for' loop with input$i in R Shiny


r,loops,for-loop,shiny
In my shiny app, I build a a number of checkboxes using a for loop, like this: landelist <- c("Danmark", "Tjekkiet", "Østrig", "Belgien", "Tyskland", "Sverige", "USA", "Norge", "Island") landecheckbox <- c() for (land in landelist){ landechek <- paste0("<label class=\"checkbox inline\"><input id=\"", land, "\" type=\"checkbox\" checked><span>", land, "</span></label>") landecheckbox <- c(landechek,...

Alternate control of a sliderInput between a derived value and user selected value


r,shiny,data-visualization
I have a very simple Shiny app, wherein I have a set of data on past customers, and a set of data on 3 new customers. All my data consists only of 2 variables: age and score. The purpose is to select one of the 3 new customers, and see...

Disable textInput based on radio button selection on Shiny


r,shiny
Say I have the following Shiny App: library(shiny) ui <- fluidPage( column(3, radioButtons("radios", "", c("Enabled" = "enabled", "Disabled" = "disabled"), inline = TRUE) ), column(4, textInput("text", "", value = "Disable me")) ) server <- function(input, output) { } shinyApp(ui=ui, server=server) What's the easiest way to disable the textInput based on...

How can I use a variable to get an Input$ in Shiny?


r,variables,csv,shiny
I am new to R and I am creating a shiny application to read a csv and filter data. I am reading the csv file, then creating dropdowns with a loop using the column names and the unique values: output$dropdowns <- renderUI({ if (is.null(x())) { return(NULL) } lapply(1:ncol(x()), function(i) {...

Is there a way to center an R plot within a tabPanel in shiny?


r,shiny,shinyapps
I am working on a shiny app, and the way I have it formatted the plots are way too wide. I shortened them with the width argument so they look reasonable but I can't figure out how to center them within the tab panel (apparently plotOutput doesn't take an align...

Creating a generic function generating new objects from selected list of columns in a data frame


r,function,user-interface,shiny,shinyapps
I'm working on a simple ShinyApp that would enable end-users to interrogate the data with use of various filters. Some of the filters correspond to unique values in data frame columns which are created using the code corresponding to the example below: # Source sample data data(mtcars) # Put together...

Remove reactive expression in shiny app


r,shiny
My application works well with reactive expressions, but it's very slow. I have two functions : FixData which cleans and fix my data (more than 500k rows), this function is slow PlotData which returns a ggplot graph and is reasonably fast What I want to do first is importing my...

Custom play button for slider input in Shiny?


r,shiny
http://shiny.rstudio.com/reference/shiny/latest/sliderInput.html says that you can modify the appearance of the play button in slider input by using HTML tags or a character vector. I want my play button to be a bit bigger or use an image instead, I can't quite get it to work and can't find an example...

Strange behavior of R Shiny reactiveValues() and invalidateLater()


r,shiny,shinyapps
I have a use case where am visualizing operational data for a dashboard. I would like it to be such that the visualization is updated periodically as data is added to the database. The logic in my mind is to first check if the number of rows in the live...

Dependent inputs in Shiny application with R


r,shiny
Say I have a shiny::sliderInput: ... sliderInput("input_1", "Title_1", min = 1, max = 10, value = 5) ... Is it possible to reference min, max, and/or value in a different sliderInput? The use case for this is to make a second a input dependent on the first input. Something to...

dplyr multiple inputs from Shiny


r,shiny,dplyr
I have a Shiny app that takes input from radio button and then use that to perform filter to the data frame using dplyr in the server side. It works, but now I want to expand it to take multiple inputs to filter, and I have no idea how to...

Sleep Shiny WebApp to let it refresh… Any alternative?


r,shiny,sleep
I have a WebApp that have some renderUI({})... and some of them depend on the input of another. This makes that, briefly, a red error in the webpage appear when I select some options. Because the if() clause of some renderUI({}) depend on the input of a selectizer. The error...

how to get values from selectInput with shiny


r,shiny
I am playing around with the shiny packages for some hours now, and wanted to make a select input widget that enables me to download a certain data set from the server. So i figured out a way to get me this data frame containing all my IDs for downloading:...

R - Shiny Package - Non-numeric argument to binary operator


r,shiny
I'm pretty new to using R and I'm just testing out some things with the Shiny package. In my application I request the user to give some inputs, based on these inputs the application has to make some calculations. The input gets asked from the user in the following way...

R/Shiny WebApp logs, running in shell (Ubuntu crontab)


r,ubuntu,logging,shiny,crontab
I'm running, using crontab, an R/Shiny WebApp. The crontab expression looks like this: @reboot R -e 'shiny::runApp(...)' | tee /var/log/shiny-server.log I've also tried @reboot R -e 'shiny::runApp(...)' &> /var/log/shiny-server.log but does the same: It prints to the file the header when you initialize R from shell. But it doesn't print...

Limiting the number of users in a locally hosted R shiny app


r,shiny
I'd like to limit the number of users of my locally hosted r shiny app to one user at any one time. So ideally when a second user attempted to run the app at the same time (users access the app by typing the local IP into the address field)...

Reactively changing colour of an infobox, upon a click or hover over


javascript,html,r,shiny
I would like to use the reactiveValue, observe, observeEvent framework in shiny and shinydashboard to be able to reactively change the colour of an infoBox when clicked. I would also like it to display an image with some text in a popup box when hovering over the infoBox. As a...

R Shiny - set reactive width of a plot output


plot,shiny,reactive-programming,weight
I am filtering a tbale and a heat-map simultaneously. During filtering, the width of the table is changing such that width of particular columns remains the same. Width of the heat-map remains the same during filtering. I want the heat-map to change its weight in the same way as the...

R Shiny list2env


r,shiny
I have a shiny application in which the server.R looks like below : shinyServer(function(input, output,session) { temp<-reactive({ obj1<-read.csv(.....) v1<-c("obj2") lst<-lapply(v1,read.csv(....) list2env(lst) ... }) I'm creating an object directly in the reactive function called obj1 and also creating obj2 using list2env. Both obj1 and obj2 are not in the same environment....

Changing the color of the sliderInput in Shiny walkthrough


css,r,shiny
I know that changing the color of the sliderInput in Shiny requires to change something in the css files, I tried to look inside the function sliderInput()with the source viewer but it kinda doesn't make sense to me as I do not know much about html and CSS. I guess...

How to restrict Shiny fileInput to text files only?


shiny,rstudio
I want to restrict my file browser to displaying only the types of file I specify, e.g. .txt files only. The only relevant snippets of a code I found are like the following: fileInput("in_file", "Input file:", accept=c("txt/csv", "text/comma-separated-values,text/plain", ".csv") However, this does't filter the files showing in the browser to...

The error when publishing a shiny app


r,shiny,rstudio,shinyapps
I want to publish my shiny app. For this I created an account in https://www.shinyapps.io/ Then, in RStudio, I go to Tools->Shiny Apps->Publish Apps and paste the token, account and secret information. Finally, I get the error: Error: /v1/users/current/ 426 - shinyapps package out of date. To update visit: https://github.com/rstudio/shinyapps...

“read_excel” in a Shiny app


r,shiny,xlsx
I have a Shiny app that uses the "read.xlsx" function from package "xlsx". All works fine, but I want to change to "read_excel" from "readxl", hoping it would be faster and able to cope with large files. ui part: fileInput("inputFile","Upload file...") server part: data <- reactive({ inFile <- input$inputFile if...

“Icon” (ISOTYPE) charts in R shiny with Javascript


javascript,r,plot,statistics,shiny
I'm working on a project to build several models for data analysis and reporting using R and the amazing Shiny framework for web development. I'm getting started with R and Shiny but I've had an amazing experience so far, yet, I'd like to get some help in case someone has...

How to format currency values in valueBox shinydashboard?


r,shiny,currency,dashboard
I'm trying to write a dashboard with shinydashboard in R to display some values using renderValueBox and valueBoxOutput. These values are not hardcoded but are being scraped from another source daily. These values are currency numbers and should be reporting like $XXX,XXX.XX but instead I see XXXXXX.XX. Is there a...

Shiny - how to change the font size in select tags?


r,shiny,shinyapps
How can I change the font size in select tags? I tried with this code below but the font size does not change at all. shinyUI(fluidPage( sidebarPanel( # Change the font size. tags$style(type='text/css', "select {font-size: 32px !important} "), # Species/ pollutant options selectInput( inputId = "species", label = "Species:", choices...

How to upload/import a file in the new R shiny version 0.12 using DT package


r,datatable,upload,shiny
I have just updated shiny to version 0.12 and started to use the DT package (finding it a bit difficult to use but have to do it anyway). Basically I am trying to upload or import a file. This is my server code: shinyServer(function(input, output, session) { datasetInput <- reactive({...

Cannot produce ggplot2 in Shiny


r,ggplot2,shiny,ecdf
Below is my code. I am able to produce the ecdf plots in R studio but not when i put it all together in a shiny app like I have below: Server.R library(shiny) library(ggplot2) shinyServer(function(input, output) { datasetInput <- reactive({ mydf = data.frame( a = rnorm(100, 0, 1), b =...

Hide shiny output


r,shiny,rmarkdown
How do you hide rendered shiny output? Specifically, I have some figures/tables generated by shiny and I have a button, that when clicked should hide the figures/tables, and when clicked again should show them. This is what I have so far (below), and it works somewhat, but where it's supposed...

R shiny updateCheckboxInput


r,shiny
I have a R shiny application in which I have drop down box defined in ui.R as lib whose values are c("X","Y","Z") and also a check box called spcheck which is defined as checkboxInput("spcheck","label1",value = FALSE)) Other conditions makes this checkbox available only when input$lib=Z and checking it would make...

Shiny: Input selection through conditional panels is overruled


shiny
I would like to switch my input field from checkboxes to radio buttons, depending on which graph type is chosen. The input variables are equal in either case, but some graphs allow for multiple inputs and some not. The UI works like expected, but the radio buttons do not affect...

how to display an image to the left of the text in a titlePanel?


shiny
I would like to have an image displayed to the left of the titlePanel text. I have tried: ... titlePanel( img(src = "./mylogo.png", height = 60, width = 255), "My Title" ), ... The image gets displayed, however, the text 'My Title' does not get displayed. How can I do...

rCharts doesn't render the plot in shiny app


r,plot,shiny,rcharts
I am using following simple shiny app code snippet to plot the output in the main pannel. But it doesn't render the plot as expected. It shows nothing in the browser. But When i run only the mPlot portion, it displays the plot in RStudio correctly with all interactions. ui.R...

Shiny - change the size (padding?) of dropdown menu (select tags) smaller


html,css,r,shiny,shinyapps
How can I change the size of dropdown menu (select tags) smaller? I thought it is the padding that makes the dropdown looking 'thick'. So I change the padding to 0 to make it 'thin' and obviously that does not work, shinyUI(fluidPage( sidebarPanel( # Change the font size. tags$style(type='text/css', "...

Forecasting an Arima Model in R Returning Strange Error


r,time-series,shiny,forecasting
I'm working on building a Shiny App for forecasting time series. One component of this is using ARIMA models to forecast. The user specifies the start and end of the historical data, what p, d, and q they would like to use in the ARIMA model (if they don't want...

Convert a column of text URLs into active hyperlinks in Shiny


r,url,hyperlink,datatables,shiny
I am creating a user interface for a pathway enrichment program. The results are shown in a table as shown below. Below is a snippet showing that I am using DT::renderDataTable and DT::datatable to output the table in a tab. spia_out() is just a reactive function that runs the pathway...

Global assignment within a reactive expression in R Shiny?


r,null,shiny,variable-assignment
suppose I have a data object that is uploaded by the user: data <- reactive({ inFile <- input$file if (is.null(inFile)) return(NULL) data <- read.csv(inFile$datapath) return(data) }) And suppose I want to delete columns in the dataset. I want to set it to global assignment so that I can run the...

evaluate function in shiny and print values


r,shiny
Suppose I have a function that takes an input an returns a data frame with two values. I want to render the following text: Value 1 is df1$value1, and value 2 is df1$value2. Is there a way of doing this? This is the code that I have right now: ##...

How to set ggvis to use canvas renderer by default?


r,shiny,ggvis,shinyapps
Is it possible to make a ggvis widget use the canvas renderer by default (instead of svg)?

R shiny ggplot2 error 'Results must be all atomic, or all data frames'


r,ggplot2,shiny,shinyapps
I get inconsistent errors with my shiny app and I can't seem to figure out what is wrong. This is the most common error. Error: Results must be all atomic, or all data frames. The shiny app basically allows the user to chose 1 or more files, then reads those...

R Shiny - filter two table and heatmap simultaneously (with cells of fix width)


r,callback,filtering,shiny
I have two input tables: label and main_plot. I am able to filter the plot using controls in the sidePanel. Column in "main_plot" corresponds to columns of "label". I want to filter both tables using controls simultaneously. (e.g.: if I see only first and second column of "label" after filtering...

Leaflet map legend in R Shiny app has doesn't show colors


r,shiny,leaflet
When I try to add a legend to a leaflet map for a leaflet map (using the Leaflet for R package) incorporated into a Shiny app, the legend does not show the colors of the color palette. Instead it only shows the colors specified for the NA values, in this...

Shiny script from Ubuntu bash


r,bash,ubuntu,shiny
I wanted to run with crontab (the system daemon used to execute desired tasks at certain times) a shiny script. I first tried running sh Autorun.sh, being in the file: R shiny::runApp(...) but that didn't work I also tried writting directly Rscript shiny::runApp(...) but it also doesn't work Any ideas?...

Error in UseMethod(“xtable”)


r,shiny
I am trying to load multiple files and merge using Reduce function... Tried with several options but got same error Got 'xtable' applied to an object of class "character" server.R library(shiny) shinyServer(function(input,output) { output$data <- renderUI({ res <- lapply( 1:input$fnos, function(i) { fileInput(paste("file", i), "Load File", accept=c( 'text/csv', 'text/comma-separated-values', 'text/tab-separated-values',...

Shiny & ggvis select subset of data dynamically


r,shiny,ggvis
how can I select a subset of the data and plot it using shiny & ggvis? As the ggvis documentation states, it is not possible to swap the dataset using ggvis input_select. Besides this limitation it would be awesome if the data preprocessing & filtering would have to be performed...

How do I reference a clicked point on a ggvis plot in Shiny


function,shiny,ggvis
I wish to use the values of a clicked point for further processing but am unclear how to reference the data library(shiny) library(ggvis) library(dplyr) df <- data.frame(a=c(1,2),b=c(5,3)) runApp(list( ui = bootstrapPage( ggvisOutput("plot") ), server = function(..., session) { # function to handle click getData = function(data,location,session){ if(is.null(data)) return(NULL) # This...

Unable to appropriately assign a size to NVD3 chart


shiny,nvd3.js,rcharts,shinyapps
I am experimenting with an NVD3 chart, and though it renders correctly in shiny dashboard, the div which contains the chart overflows shiny dashboards box() container (does not fit snugly into the box). Explicitly setting height and width for the chart changes the charts size but not the containing div,...

How to embed an image in a cell a table using DT, R and Shiny


r,shiny,shiny-server
How can I embed an image in a cell that is generated using the DT package so that it is displayed in an app using shiny? My example is based of this question R shiny: How do I put local images in shiny tables The example code below doesn't display...

How to make the horizontal scrollbar visible in DT::datatable


r,datatables,scrollbar,shiny,horizontal-scrolling
Using R shiny & DT package, I am creating certain tables. The number of columns vary as per user input & is not fixed. I have included the following code snippet to include a horizontal scrollbar so that when the number of columns is large, the user can scroll through...