Inclusion of R script in shiny application
1
0
Entering edit mode
20 months ago
mrashad ▴ 80

I am new to shiny applications. I have R script that produces some tables and PCA plots, and I work to include this code inside a shiny application. Could you guide me on how to do that? I know I should use source(R script), but how to produce the tables and PCA plots as html inside the shiny application and how to download them as pdf.

Thanks in advance.

shiny script • 1.9k views
ADD COMMENT
1
Entering edit mode

there are a number of resources online for learning to build Shiny Apps. I recommend working through the lessons at https://shiny.rstudio.com/tutorial/#get-started as a starting point.

ADD REPLY
2
Entering edit mode
20 months ago
bkleiboeker ▴ 370

I'm new to shiny also but have made a few applications in it over the last few months. A few things that have helped me a lot as a beginner:

Find a template online for an app that follows your same general structure and build on that. It's very important where your data comes from. Does the user upload it? Is it built in example data like mtcars? Will you be reading in a file which you'll put in the directory when you deploy the app? Considering this when choosing a template has helped me a lot.

You can write functions outside of ui and server and then call those functions within ui or server.

Hopefully somebody else can explain the logic behind reactives, because I sure don't understand them well enough myself yet. If you're using a static dataset (not user-uploaded), you can modify it 'as usual' anywhere in server, but anything which depends on user input must be in some reactive statement (such as a renderPlot({}) which might be useful in your case).

In my limited experience with shiny it's been a lot of trial and error, but once things start to click it's so rewarding seeing your code come to life in a beautiful interactive application!

ADD COMMENT
0
Entering edit mode

Hopefully somebody else can explain the logic behind reactives, because I sure don't understand them well enough myself yet.... anything which depends on user input must be in some reactive statement (such as a renderPlot({}) which might be useful in your case).

You answered your own question - reactives allow the code to be responsive to user input such that the code is executed and the new results are rendered in the UI in response to changes selected by the user.

ADD REPLY
0
Entering edit mode

I actually have no problem with the built in examples, but my problem actually with the user input. Unfortunately, I tried a lot but without any results

ADD REPLY
0
Entering edit mode

Where are you stuck? What sort of user input are you trying to take in?

ADD REPLY
0
Entering edit mode

In the UI I added,

plotOutput ("PCA_plots")

While in server:

output$PCA_plots <- renderPlot({ req(input$Begin)

Begin is an action button as once I click on it the function first_container works

df_lists <- first_container(input$Data, input$Metadata)  

first_container is a function, Data and metadata are the user input

})

Once I run: there is a white space always appears below the action button Begin as shown: enter image description here

Note: this function produces several numbers of PCA plots

ADD REPLY
0
Entering edit mode

It is solved: The error is that I didn't add a read function for the input file:

Count_data <- reactive({ file1 <- input$Data if(is.null(file1)){return()} read.csv(file=file1$datapath) })

meta_data <- reactive({ file2 <- input$Metadata if(is.null(file2)){return()} read.csv(file=file2$datapath) })

I thought once I upload it on the shiny application, its bath will be defined.

Thanks a lot for help :)

ADD REPLY

Login before adding your answer.

Traffic: 2582 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6