Create hyperlink for elements in datatable in r shiny - r

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

Related

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 can I use a variable to get an Input$ in 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) {
selectInput(names(x()[i]), names(x()[i]), c("ALL", unique(as.character(x()[,i]))))
})
I am now trying to filter the data based on the input from the user. To get the input I am trying to loop through the names (names(x)[i]), which is the ID of the selectinput and get the value. But whenever I use input$names(x)[i], I get the following error:
Error: attempt to apply non-function.
I have tried to test this using an actual header (e.g. input$testHeader) and this works fine. But when I try to do the same with a variable, e.g.:
a < - "testHeader"
print(input$a).
This returns NULL. I assume it is looking for a selecinput with ID "a" and cannot find it. But I have no idead how else to try?
Any help would be great.
Thanks.
input is just a reactivevalues object so you can use [[:
print(input[[a]])

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.

How to display data frame contents inside a function

I want to see the contents of a dataframe that is created inside a function after the dataframe gets created.
Let's say the dataframe is called df in the function. If I just include a line of code like this:
df
I don't get any display to the console. I would get the dump of the dataframe contents outside a function.
Thanks
In an interactive session, when you type the name of an object, like df, R implicitly prints() the object using an appropriate method. It is as if you'd typed print(df), without you actually having to type all those extra characters.
However, there are a few places that this automatic printing is not active
As you found out, this is not active within functions,
Within loops, such a for, while etc.,
When you source a script.
In such cases, you need to explicitly print() an object.
This often catches people out with lattice and ggplot2 plots, which need to be print()ed to get them drawn on the device. but in general use, the user never has to explicitly print() these plots to get something drawn.
You're expecting it to print out because when you type the name of an object into R's shell, you have the representation of the object helpfully echoed back to you. Inside a function, this doesn't happen unless you explicitly tell R to print the information.
If print(df) doesn't work, look for buffered output and be sure it's turned off.
When you type a variable name in the console and hit enter, R silently passes that variable to print. In order to get the same effect from inside a function, you need to explicitly print. In your case:
print(df)
df <- data.frame(x = rpois(100,80))
print_me <- function(x){
print(x)
}
print_me(df)

R Shiny - iterate over ShinyUI output elements

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!!

Resources