R shiny updateCheckboxInput - r

I have a R shiny application in which I have drop down box defined in ui.R as lib whose values are c("X","Y","Z") and also a check box called spcheck which is defined as
checkboxInput("spcheck","label1",value = FALSE))
Other conditions makes this checkbox available only when input$lib=Z and checking it would make few things appear.
When users select a different library, say X, I want to make the spcheck value FALSE, So I added the following code to server.R
observe({
if (input$lib %in% c("X","Y") )
{cat("uncheck called 1 : ",input$spcheck,'\n')
updateCheckboxInput(session,"spcheck","label1,value = FALSE)
cat("uncheck called 2 : ",input$spcheck,'\n')
}
else return()
})
The text displayed at console is :
uncheck called 1 : TRUE
uncheck called 2 : TRUE
Why is it not making the spcheck value FALSE?
May be I'm missing something very trivial, but I couldn't figure it out.
Any help??

Because it takes a few milliseconds since you tell shiny to update the input until it actually happens. When you call the update method, shiny has to send a message to JavaScript to change the value of the input, and once that's done, JavaScript sends a message back to R saying that the input has a new value. But the input variable does not change instantly when you make the call, it'll only have an updated value next time a reactive statement uses the input$spcheck variable

Related

Fix Shiny Input

I'm making a shiny app that takes a numericInput(size,...) and displays a data frame of random numbers with input$size rows, and then saves it as a csv. I'm looking for some way to prevent the user of the app to change the inputed number once they have provided it. For example, if the user sees the data frame and thinks "Oh, I don't like these numbers", I want to make sure they cannot just keep entering numbers until they get a result they want (without closing and reopening the app). Is there someway to fix the first input as it is given? Thank you so much!
You can use a combination of reactiveValue and observeEvent with the parameter once = TRUE
This will allow the reactiveValue to only be set once. The user can then change the input but it will have no effect on the rest of the app
size <- reactiveVal()
observeEvent(input$size,{
size(input$size)
},
once = TRUE)
You might have to look into the parameters ignoreInit and ignoreNULL depending on how you initate your numericInput.

R Shiny select input unselected

Hello How can i get info that Select input is still unselected i try something like
server.R
output$Country<-renderUI({selectInput(inputId ="inputCountry",label ="change options",choices=data$Country) })
if(input$inputCountry=="")
or
if(is.null(input$inputCountry))
is.null should do it (depending on quite how you initialise the input)
As doctorG said, is.null is the correct choice if you want to check whether an input variable has already been "activated". There is however a better option in case you want to perform this check in a reactive environment, which is
req(input$inputCountry)
This will automatically stop the current observe or renderXXX function if the input is either "", NULL or FALSE. req can also be used inline in the following manner
output$plot <- renderPlot({hist(rnorm(req(input$n)))})
The req function also has an optional argument cancelOutput which can be quite handy in certain usecases. See ?req.

How to update the file related to a `fileInput` variable in R Shiny without user interaction?

I'm working on an app in R where the users need to choose a file from their computer, with a RShiny fileInput button. I want to modify this, so that the associated variable can be assigned (i.e. a file can be loaded) automatically by the programm, without having the user click on the button and choose the file.
The problem I'm facing is that a fileInput has 4 fields, amongst which I only can know 3. For instance, when I load the file hello.csv in the variable inFile through the normal procedure, here is what I get :
inFile$name = hello.csv
inFile$size = 8320
inFile$type = text/csv
inFile$datapath = C:\\Users\\MyName\\AppData\\Local\\Temp\\Rtmpkh8Zcb/7d5f0ff0111d440c7a66b656/0
Though I could have guessed the second and the third one knowing the file, I have no idea how the datapath field is assigned...
I've tried to declare inFile as a NULL global variable, then to assign one by one the different fields, but I'm stuck with this last one. Is there an other way to do, like a function that mimics the behaviour of a user who clicks on the file input button and choose a specified file ?
Thank you very much.
If all you're looking to do is load a file initially, you don't have to rely on Shiny functions to do that. You can just rely on R functions. Set up your app like this:
ui <- shinyUI(
fileInput("inFile", label="Choose a file", multiple=F)
)
server <- shinyServer(function(input, output, session) {
values <- reactiveValues()
dat <- reactive({
if (is.null(inFile$datapath)) {
dat <- read.csv("path/to/your.csv")
values$file_name = "your.csv"
values$file_type = "csv"
values$file_size = file.size("path/to/your.csv")
values$file_path = "path/to/your.csv"
} else {
dat <- read.csv(inFile$datapath)
values$file_name = inFile$name
values$file_size = inFile$size
values$file_type = inFile$type
values$file_path = inFile$datapath
}
})
})
shinyApp(ui=ui, server=server)
In the above code, the Shiny app will start and see that inFile$datapath is NULL and will load a predefined file of your choosing. It won't run again until inFile changes, at which point it will load the file that the user pointed to.
Hope that helps.
Update
I changed the code above to use reactiveValues to store the pieces of information that need to be used throughout the app. If you just set those and then do a find/replace for input$inFile$datapath and replace it values$file_path, your code should work just fine.
Here is how I figured it out :
I edited the original code, so that all the read.csv(...) are replaced with calls to a data.frame global variable. I also added a small button that you need to click on before you continue. This button saves what you just loaded in the Database (if you chose a file with the fileInput) and assigns the right values to the global variables that will be needed for the following operations. If you chose no file at all, it will directly assign the variables from the data found in the Database.
So I did not find a proper solution to the problem, but this is a workaround that will do the job in my case.
#brittenb I couldn't get your reactive solution to work as I wanted to, that's why I ended up doing this another way. Thanks for having taken the time to think about it though.
I'm still open to suggestions on how to update the file in a fileInput without user interaction.

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

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