How can I use a variable to get an Input$ in Shiny? - r

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]])

Related

Shiny: renderDataTable throws error when renaming columns in data.frame

I have a Shiny app that is creating a network visualisation. When the user clicks a node in the visualisation, a table is displayed detailing information about this node. All of this works beautifully as seen in the code below:
output$shiny_return <- renderDataTable({
if(!is.null(input$current_node_id)) {
kwic.file <- paste0(input$current_node_id, "_6.csv")
kwic.data <- read.csv(file.path(config.analysis.basedir, config.kwic.basedir, kwic.file))
output.table <- data.frame(getLetterUrlFromFile(kwic.data$file), kwic.data$left, kwic.data$keyword, kwic.data$right)
}
}, escape = F)
However, the column names in the data table aren't what I want them to be in the output so I simply want to rename the columns in the output. I tried doing this by adding one line of code after the creation of the data.frame:
colnames(output.table) <- c("Letter", "Left Context", "Keyword", "Right Context")
Now the function throws an error:
Error: 'data' must be 2-dimensional (e.g. data frame or matrix)
I have tried renaming the columns using plyr as well and that doesn't seem to work. If I remove this one line, everything is fine (I just have ugly column names). Is there something I'm missing? Should I be renaming the column names in a different manner?

Error in l$data : $ operator is invalid for atomic vectors shiny app

s[enter image description here][1]server.R
df <- read.csv("")
output$out1 <- renderUI({
params <- colnames(df)
# These are the colum names goes as a list into the selectInput..
param_list <- params
# Dynamically create dropdown box
selectInput("params", label = "Choose a variable to Observe", choices = param_list)
# print(param_list)
})
stateDataSubset <- reactive({
input$params
dataSetVariable <- input$params
})
I am trying to use the input$param which is dynamically created in another function at another reactive function, not sure why I am getting this error as similar code in the past has worked. params is there in output$out1, now I want that input in stateDataSubset function.
you need to provide proper example. Rather than just explaining users who have no idea of what you are trying to accomplish....Please provide input and seps of your code that lead to error. This explanation " created in another # function at another reactive function...." or saying " now I # want that input in stateDataSubset function." Which users have no idea will not be helpful.
And, to answer your error $ operator is invalid for atomic vectors, is self explained. You are trying to use $ to access a vector. to avoid that problem change whatever input is to a data.frame.
Proper solution in code format could be provided only if you are able to explain your problem with example.

Shiny App : getting input choices from an uploaded file

I'm trying to use Shiny to get data from a file and use this data to populate a list.
The process is pretty standard. On a server.R file, I get a vector called 'list' by reading a csv file. I want to be able to select the value from this vector with a selectInput() function. So I put an observe loop to check if the file is uploded, and if it's correct, list is created.
observe({
inFile<-input$DATA
if(!is.null(inFile)){
data = read.table(inFile$datapath, header=T,sep=";")
list=unique(data$SLE)
}else{
list=NULL
}
})
My selectInput() is like this :
output$SLE = renderUI({ selectInput('SLE1', label='Choose the item :', levels(list)) })
If I put the selectinput() in the observe loop, the select box works well, except that every time the observe loop is executed, the selectbox is reset. So, it's not a solution.
If I leave the select box out of the observe loop, list keeps the defaut value even if the data file is loaded.
I've tried to set list as a reactive value but it wasn't a success. How can I set list?
You can have the select input inside a conditional panel in UI.R itself and
use a function in place of "choices=" to retrieve the data frame as the list of choices.
One example is as below:
in ui.R:
selectInput("id", "Select Employee:", get.Employee())
Required funcion:
get.Customers <- function(input){
# YOU CAN ENTER YOUR CODE IN HERE
df_input <- input$nr
return(df_input)
}

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.

Receiving input from various actionbuttons in Shiny R

So this is somehow a follow up to my previous: Automatic GUI Generating in R Shiny wher I posted the solution to how to generate elements iteratively.
Now I am unable to recieve/check which actionbuttons have been pressed and perform some action upon press.
In general, there is a function that generates the buttons and sends it to the ui.R:
output$generateImages <- renderUI({
(...)
i <-1
for(dir in folders){
(...)
txt<-paste0("rep",i)
pp<-pathNameToImage
LL[[i]] <- list(actionButton(txt,icon=imageOutput(pp,width="100px",height="100px"),label=dir))
i<-i+1
}
return(LL)
}
in ui.R I have:
uiOutput('generateImages')
And it displays the buttons fine by accumulating them into the list called "LL".
I have tried looking for tutorials and examples but was not able to find how it is done with images, and how to later recieve input from buttons that were not created "by hand", but iteratively.
How do I access these buttons in "observe" so that I can perform a action? I have tried input$generateImages, input$LL and few others, but all of them had a value of NULL.
You'll need to access them by their unique ID. The first argument passed to actionButton is its ID. That's what you'll need to use to get it as input.
So:
LL[[i]] <- list(actionButton("SomeID"))
when you assign it, then
input[["SomeID"]]
when you want to reference it. Of course, you can use a variable instead of a hardcoded string for the ID.

Resources