Shiny app download
Help users download data from your app Last Updated: 28 Jun See here for an example app with file downloads. The basic parts of a Shiny app. How to get help. App formats and launching apps. Introduction to R Markdown.
Introduction to interactive documents. Setting Output args via Render functions. Generating downloadable reports. Shiny Gadgets. Reactivity - An overview. How to understand reactivity in R. Database basics - dplyr and DBI. Using the pool package basics.
Using the pool package advanced. Using dplyr and pool to query a database. Persistent data storage in Shiny apps. Install and use If you are an R and RStudio user, you have two options to use the app. R in DOEMaker main directory. Make a design Choose a type of desgin Once you run the app, by default, a full factorial design with 3 factors will be display.
Customize The next step is set the options according to your needs: As you set different options the table on the right will automatically update the table on the right: For space considerations, not all treatments are displayed.
Download Finally, you can write a file name and download your design table, as a CSV file, just by clicking on the gray button: Your design will be on your downloads directory. These functions and their packages are: fac. FrF2 from FrF2 for fractional designs. First we need to make sure ggplot2 is loaded, so add a library ggplot2 at the top. If you run the app with this code inside your server, you should see a histogram in the app.
But if you change the input values, nothing happens yet, so the next step is to actually filter the dataset based on the inputs. Recall that we have 3 inputs: priceInput , typeInput , and countryInput. We can filter the data based on the values of these three inputs. Place this code in your server function and run the app. If you change any input, you should see the histogram update. That sounds right. Read this code and understand it.
The other output we have was called results as defined in the UI and should be a table of all the products that match the filters. Add this code to your server. Run your app, and be amazed! You can now see a table showing all the products at the BC Liquor Store that match your criteria. Exercise: Add a new output. Either a new plot, a new table, or some piece of text that changes based on the inputs.
For example, you could add a text output textOutput in the UI, renderText in the server that says how many results were found. If you choose to do this, I recommend first adding the output to the UI, then building the output in the server with static text to make sure you have the syntax correct. Only once you can see the text output in your app you should make it reflect the inputs. For example, h2 textOutput Shiny uses a concept called reactive programming.
This is what enables your outputs to react to changes in inputs. Reactivity in Shiny is complex, but as an extreme oversimplification, it means that when the value of a variable x changes, then anything that relies on x gets re-evaluated.
Notice how this is very different from what you are used to in R. Consider the following code:. What is the value of y? But in reactive programming, if x and y are reactive variables, then the value of y would be 11 because it would be updated whenever x is changed. Only reactive variables behave this way, and in Shiny all inputs are automatically reactive. This is not the official terminology, but it simply means that the variable is referenced in the code.
Consider the following sample code to create a plot with a specific number of points in a specific colour:. One very important thing to remember about reactive variables such as the input list is that they can only be used inside reactive contexts.
Shiny is very clear about what the error is: we are trying to access a reactive variable outside of a reactive context. So far we only saw one reactive variable: the input list. Try adding the following line to your server:. Now your app will run. Notice that we use priceDiff rather than priceDiff. You can think of reactivity as causing a chain reaction: when one reactive value changes, anything that depends on it will get updated.
If any of the updated values are themselves reactive variables, then any reactive contexts that depend on those variables will also get updated in turn. Try reading this section again, and I promise that with time and experience you will get more comfortable with reactivity. Once you do feel more confident with reactivity, it may be a good idea to read more advanced documentation describing reactivity, since this section greatly simplifies ideas to make them more understandable.
Exercise: Read this section again and really understand what a reactive variable means, what the 3 main reactive contexts are, how you can define reactive variables, and how a reactivity chain of events works. You may have noticed that we have the exact same code filtering the dataset in two places, once in each render function. We can solve that problem by defining a reactive variable that will hold the filtered dataset, and use that variable in the render functions.
The first step would be to create the reactive variable. The following code should be added to the server function. Now that we have our reactive variable, we can use it in the output render functions. This is how your server function should look like now. As a reminder, Shiny creates a dependency tree with all the reactive expressions to know what value depends on what other value. For example, when the price input changes, Shiny looks at what values depend on price, and sees that filtered is a reactive expression that depends on the price input, so it re-evaluates filtered.
Then, because filtered is changed, Shiny now looks to see what expressions depend on filtered , and it finds that the two render functions use filtered. So Shiny re-executes the two render functions as well. One of the output functions you can add in the UI is uiOutput. According to the naming convention eg.
Any input that you normally create in the UI is created when the app starts, and it cannot be changed.
But what if one of your inputs depends on another input? In that case, you want to be able to create an input dynamically, in the server, and you would use uiOutput. The same rules regarding building outputs apply, which means the output which is a UI element in this case is created with the function renderUI.
If you run that tiny app, you will see that whenever you change the value of the numeric input, the slider input is re-generated. This behaviour can come in handy often.
We can use this concept in our app to populate the choices for the country selector. The country selector currently only holds 3 values that we manually entered, but instead we could render the country selector in the server and use the data to determine what countries it can have.
First we need to replace the selectInput "countryInput", Then we need to create the output which will create a UI element - yeah, it can be a bit confusing at first , so add the following code to the server function:. Now if you run the app, you should be able to see all the countries that BC Liquor stores import from. You might notice that when you first run the app, each of the two outputs are throwing an error message, but the error message goes away after a second.
Once we understand why the error is happening, fixing it is simple. Inside the filtered reactive function, we should check if the country input exists, and if not then just return NULL.
Putting all these ideas together gives us the following app where you can upload a. Again, the UI is straightforward: use either downloadButton id or downloadLink id to give the user something to click to download a file.
The results are shown in Figure 9. You can customise their appearance using the same class and icon arguments as for actionButtons , as described in Section 2. Unlike other outputs, downloadButton is not paired with a render function. Instead, you use downloadHandler , which looks something like this:. The job of this function is to create the name that will be shown to the user in the download dialog box. The job of this function is to save the file in a place that Shiny knows about, so it can then send it to the user.
This is an unusual interface, but it allows Shiny to control where the file should be saved so it can be placed in a secure location while you still control the contents of that file. The following app shows off the basics of data download by allowing you to download any dataset in the datasets package as a tab separated file, Figure 9.
I recommend using. You can avoid this complexity by using tab separated files, which work the same way everywhere. Note the use of validate to only allow the user to download datasets that are data frames. A better approach would be to pre-filter the list, but this lets you see another application of validate.
0コメント