R Shiny - iterate over ShinyUI output elements - r

I wonder if it is possible to iterate over ShinyUI output elements.
In my Shiny application I have a few uiOutput elements which are rendered in a very similar way. I would like to refer to them (render them) in a loop, for example with the use of their id. I wish to do something like:
for(element.id in uiOutput.ids){
output[element.id] <- renderUI({...})
}
Of course, such code results in the following error:
Error in `[<-.shinyoutput`(`*tmp*`, "some_id", value = function ():
Single-bracket indexing of shinyoutput object is not allowed.
because the correct way (I mean: the only way I know) to refer to an ShinyUI output element is:
output$some_id <- renderUI({...})
Thank you for any suggestions!!

Related

Create hyperlink for elements in datatable in r shiny

I am working with shiny on Mac. I try to make elements in df$Trail_id clickable. I see a similar one,Convert a column of text URLs into active hyperlinks in Shiny, I try the similar code as,enter image description here
But here is the return error message, Error: 'data' must be 2-dimensional(eg.data frame or matrix).
But TI_result_score is a table which I use read.table to input it before DT::renderDataTable.
No idea what is the problem.
Best
Max
The "escape" argument should be inside a datatable function, not renderDataTable. Moreover, renderDataTable is a function with arguments. So I think that the vector modified by your paste0 is what is taken for the "table" argument by the function, and not the data.frame itself. That would explain the dimension exception thrown.
This should work :
output$TI_scores <- DT::renderDataTable({
TI_result_scores$Trail_id <- paste0(...) #your code here
datatable(TI_result_scores,escape=FALSE)
})
Tell us if there is still a problem.
Regards

DataTables for R: "warning: `dim<-.vctrs_list_of()` not supported"

I'm new to DataTables but have been amazed at its power, and have managed to create a few different tables and use them in a Shiny dashboard. Currently I'm trying to implement this table, and have managed to do so after updating a bit of the code (see my own answer in the thread).
However, when inserting the final table into Shiny, like so...
shinyApp(
ui = fluidPage(
DT::dataTableOutput("table")
),
server = function(input, output) {
output$table = DT::renderDataTable({final_table})
}
)
...I get the error DataTables warning: table id=DataTables_Table_0 - 'dim<-.vctrs_list_of()' not supported. Simply running final_table in RStudio produces the DataTable exactly as intended. Using the identically-named functions from shiny instead of DT returns a blank page.
I recognise the vctrs_list_of from one of the changes I had to make to the linked answer above (which was because tidyr's nest() these days returns a nested column of class vctrs_list_of rather than list), so I guess there's something happening there.
However, using 'inspect element' I only see an Uncaught TypeError: Cannot read property 'length' of undefined JS error, which links to some obscure line in the massive DataTables JS script. The warning is not included in DataTables' documentation.
Any leads/tips would be greatly appreciated!
I finally solved this by forcing the class of the column in question from vctrs_list_of to list, simply by running class(table$data) <- "list".
(Note: it required some changes to the JavaScript code of the linked example, since the column, now being list, had a slightly different structure.)
If anyone has a more precise explanation of what's happening here, would love to hear!

How to pass a reactive integer to a Shiny Dashboard with R

I am trying to pass a simple integer to a Shiny Flexdashboard. But I can for some reason only to it staticly, but I'd rather have it reactivly;
aggregated_static <- readRDS( "../rdsdata/aggregated_static.rds")
k <- nrow(aggregated_static)
That piece of code passes the number of rows to my Shiny dash, where it can be accessed with;
item_lines = k
But, if I do it this way, it won't work, telling me I can't access reactive content from that spot;
set_aggregated <- reactiveFileReader(1000, session, "./rdsdata/set_aggregated.rds", readRDS)
k <- nrow(aggregated_static())
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Now I've tried using Isolate, creating a function enclosing that with reactive({}), but I am simply nog getting that value passed. Is there a way to do this?
If you want the value of k to be reactive, you should wrap it in reactive(), i.e.:
k <- reactive(nrow(aggregated_static()))
and then call its value as k()

R Shiny: reactivevalues from function

I stored several tables in .rds files which I would like to import using readRDS in a Shiny session. Therefore I defined the following global function:
get.station <- function(sname){
file.name <- paste(sname".rds", sep="")
return(readRDS(file.name))
}
within the server function I define the reactive value:
st.reactive <- reactiveValues(get.station(input$station.sel))
where input$station.sel comes from the ui using selectInput(...). This results in the following error message:
Operation not allowed without an active reactive context.
(You tried to do something that can only be done from inside
a reactive expression or observer.)
This error message even does not disappear if I define this function using reactive():
get.station <- reactive({
file.name <- paste(input$station.sel".rds", sep="")
return(readRDS(file.name))
})
and within the server:
st.reactive <- reactiveValues(data=get.station())
Do you have any help?
You've got the right idea, just put the function inside a reactive or observe_ function. While you can define reactiveValues in the initial call, its best, in my opinion, to create the reactive values object empty and then do all your assignments with the <- notation.
The confusion comes from the fact that, despite it's name, reactiveValues is not a reactive expression. It generates a reactiveValues object that reactive values can be stored in, but it only runs once and does not check whether its values are invalidated.
In your case, I'd do the following:
rv <- reactiveValues()
rv$st.reactive <- observe({get.station(input$station.sel)})

R and Shiny: Using output of a reactive function

Currently I have a function [degtest] which is created in the shiny server, that returns a list,
return(list(datatable=datatable, predicttable=predicttable, esttable=esttable)
I want this list to be accessible after the function has run so that I can use different parts of the list to be rendered separately.
outlist <- reactive({
if(is.null(input$file2)){return(NULL)}
if(input$d2 == 0){return(NULL)}
with(data = reactdata$degdata, degtest(reactdata$degdata[,input$selectTemp], reactdata$degdata[,input$selectPot],reactdata$degdata[,input$selectWeight], reactdata$degdata[,input$selectTime], input$Temp0))
})
input$file2 is my reactdata (reactdata$degdata and input$d2 is an action button.
I thought i'd be able to reference outlist$datatable but R says ' object of type 'closure' is not subsettable'
When you are making an object reactive, you are actually making it into a sort of function (closure), so you have to use it as outlist() rather than outlist. See this similar question. It's hard to answer your question considering you did not provide a reproducible example, but I think your solution will be something like outlist()$ObjectYouAreTryingToAccess.

Resources