How to work with parametrised Shiny R HTML output element - r

In my shiny app I am using multiple parametrised variables that depend on a csv configuration file that lists all the IDs needed for the shiny HTML output elements.
In my ui the process is very easy. I do as follows:
htmlOutput(outputId = paste0("Variable",y))
where y is the looping variable over the IDs
The problem is on the server side of the shiny app when i want to render this HTML element. The way how I am currently doing so is using the eval parse method. The biggest drawback of that solution is that it consumes a lot of time (in addition to a higher difficulty in reading the R code). An example of rendering the above object:
eval(parse(text=paste0("output$Variable",y,"<-renderText({return(paste(\"<div style=\\\"text-align: center;\\\">\",",result,",\"</div>\"))})")))
Is there any other more efficient way to render parametrised variable names on the server side of my Shiny App?
I tried to use get and assign functions but neither of them worked.
Thanks for your help!

Related

Inserting images in R markdown using a path variable

I am new to R and Rmd and trying to generate a report using Rmd. This report has several images inserted along with the text. I am able to insert an image by hardcoding the path of the image. I have no problems with that but I need the path as a variable because it varies with the project. Can anyone help me with the syntax for calling a variable within a path to the image?
![Relatedness check](/data/array_processing/in_progress/Project123/files/data/plots/Project123.ibd.png)
"Project123" changes based on the project. Is there a way I can declare this variable and call it to define the path?
Help please.
Images can use online R code for dynamic paths and/or alt text. (Early adopters of rmarkdown often tried this method as the default method of including R plots in the reports, using png(filepath...); plot(...); dev.off() followed by what I recommend you use.)
This will allow you to do what you need:
![something meaningful](`r filepath`)
as raw markdown (and not inside a traditional code chunk).
If you aren't familiar with inline code blocks, then know that you can put just about anything in an inline code block. This is handy for including dynamic content in a paragraph of text, for example "the variance of the sample is \r var(sample(99))``". (Often it is just a pre-created variable, if numeric it is often rounded or formated to control the display of significant figures.)

Spotfire Using Document Properties in R

I know that in order to use the document properties and tables in R, you usually use the input and output parameter tabs. Is there a way to get these values just using the script, like you can in the ironpython scripts?
Thanks
No you can't
In an R script, you can only use values of document properties by setting them as an input, and you can only edit them by setting them as an output.
You can find more information about R parameters here.

Using Markdown shiny parameter in code and not only in figure generation

What is the most simple way to use R shiny in markdown to obtain an input from the user, say a number, and use it in the rest of the analysis?
All the example I have seen so far show how to use such input in generating a specific figure, but not in retaining this value as a variable to use in other code chunks.
You can use shiny inputs across chunks. Just think of the first chunk as your ui.R where you specify the input (e.g. numericInput("INPUT_ID", ...) and the second chunk as the server.R where you use the input as input$INPUT_ID.
Reproducible example:
---
output: html_document
runtime: shiny
---
# Chunk 1:
```{r, echo = FALSE}
numericInput("n", "How many cars?", 5)
```
# Chunk 2:
```{r, echo = FALSE}
renderTable({
head(cars, input$n)
})
```
Output:
Without an example of how you want the parameter outside of the renderTable to instruct the execution of your analysis it is hard to answer your question. But basically there are only two major options which come with a couple of sub-options.
Below is an update of my former answer:
1. You want to change the structure or layout of the flexdashboard depending on either:
1.1 User input
At the moment I do not see how this could be done, which does not mean that there is no solution - I might just be not aware of it.
1.2 User information (for example contained in the session info) For some special cases a workaround using the isolate function is possible, see my answer to a related question here for example.
1.3 Your data There are ways of nesting several Rmarkdown scripts to produce child templates as subpages based on the structure of the data that you read in (and might not know in advance). Here I found a very intriguing example.
2. You want to use a user input value within your analysis.
Although not clearly visible in the Rmarkdown structure, the analysis is taking place on the server part in shiny where you can use reactive and reactiveValues as they are. In this case BigDataScientist already answered your question. Of course you can use the input$n not only in renderTable but also in any other reactive expression or a reactiveValue which you can then use in some sort of render statement to show the users of your app the results of your analysis.

Web application which runs R

I have written my R script with some functions to do some calculations. On the output of my calculations, I have created plots in shiny(4 tab panels in that as of now, which is using the data specified in my global.R). Now I am trying to automate the process and deliver it to non technical users. So something like, users feed in 2 csv files in web UI and click a button in the web UI, which will then call my r scripts to do the calculations, then my server.R script should build plots on the output of it. Any similar examples? I tried different links, but nothing seems to be relevant.
If I add my R script using source(**.R) and use the functions mentioned in there. Or Do I still have to save output in data folder and specify something in global.R or just use the output of the function in my plot construction?
If I have the fileInput option in first tab in my tabpanels, how can I make stop the user from viewing the other tabs(tabs with graphs) without loading the input csv files and clicking the "Go" button?
Can I do this whole thing in Shiny ? Or better to go for rook or some other framework which calls my r script just for calculations?

Displaying png files from R into spotfire

I want to pass data from Spotfire to R and then display the plot constructed by R.
What is the best way to do this?
I’ve figured out the trick of putting images into Spotfire. It’s not hard if you follow these directions, but it’s done in a way very different from how you guess you would do it in Spotfire, and that’s why it took me awhile to figure out.
Here’s an overview of how to do it. You create a DocumentProperty which is a binary object, you write some Spotfire code that gives a value to that Document Property, and you display that binary object using a Spotfire Property Control of the “Label” type.
The confusing parts are that you DON’T use the Spotfire “Insert Image” tool at all, and that you DON’T use the filename generated inside the R code in Spotfire at all. Once you get used to the idea that the two most obvious ways you think you would approach the problem in Spotfire are entirely useless and wrong, you can make some progress.
I’ll leave out the spiderplot specifics because the code’s pretty long.
Here’s what you do.
1) Create a document Property in Spotfire of type “Binary”, e.g., “imageThatGoesBackToSpotfire”
2) You write some R code that generates an image and writes it to a file:
# get a temporary directory name on the local machine. You wouldn’t need to do this is you were just
# going to run it on your own, but you need to do it if you intend to let anybody else run it on their own machine.
tempfilebase = tempfile()
# take the tempfilebase and prepend it to a filename.
myFilename<-“someFileName.jpg”
myFullFilename <- paste(tempfilebase,myFilename,sep="")
#open a jpeg
jpeg(filename=myFullFileName)
# generate the image, however you normally would in R
plot(input)
# close the file
dev.off
# open a connection to that file.
myConnection<-file(myFullFileName,open=”rb”)
imageThatGoesBackToSpotfire<- data.frame(r=readBin(myConnection, what="raw", n=(file.info(myFullFileName)$size)))
close(myConnection)
3) Run your R script, above. Select some columns that are the “input” to the plot, and make the R script return outputs to the “imageThatGoesBackToSpotfire” DocumentProperties.
4) Create a text area in Spotfire.
5) Insert a Property Control into the text area of type “label”. (Click on the icon that’s circled in the picture below). This opens a dialog,
You need to register a data function with inputs and outputs, and the specific PNG data needs to be returned as a binary label.
Some details: http://spotfire.tibco.com/tips/2014/02/25/dynamically-displaying-images-in-a-text-area/

Resources