obtain another UI element value in ui.R in R shiny - r

It may be a very simple issue, but I can't seem to get it work.
In my ui.R I have a dropdown whose ID is "product", how do I use the selected value for product in another place in ui.R itself.
I tried input.product, input.product.value etc...none worked
Any help?
To give more information: I have tried the following to get a hyperlink on my web page :
server.R
output$sppath <- renderText({
link1<-paste("http://server2/projects/", input$product, "/collaboration/forms/collaborative%20documents.aspx?&SortField=Modified&SortDir=Desc",sep="")
return(link1) })
ui.R
fluidRow(h6("Sharepoint:", a("Collaboration", href=textOutput("sppath")))),
With above code, the link under "collaboration" is created as
http://shiny-server:8787/p/5620/%3Cdiv%20id=%22sppath%22%20class=%22shiny-text-output%22%3E%3C/div%3E
What am I doing wrong? Why is the sppath not resolving properly?
When I try just textOutput outside of a tag, it shows the full path.

the syntax is input$product, not input.product, and what you want is
renderUI in your server.r which uses input$product, then call uiOutput in ui.r

I'm able find the solution, here it is :
server.R
output$sppath <- renderUI({
link1<- paste("http://server2/projects/",input$product,"/collaboration/forms/collaborative%20documents.aspx?&SortField=Modified&SortDir=Desc",sep="")
h6("Sharepoint:",tags$a( href=link1,"Collaboration"))
})
ui.R
fluidRow(htmlOutput("sppath)),

Related

In Shiny, the submit button isn't running

I wrote a R script (MAIN.R) that converts PDF tables to CSV. When I run MAIN.R as an individual file, it functions well. I've tried it many times.
Currently, I'm working on a R shiny app that uses "MAIN.R" as a source and takes a pdf file as input. When I push the submit button, the output should appear in the MAIN panel. Unfortunately, the submit button does not function as intended.
May anyone please assist me with this, as I am new to Shiny?
UI.R
shinyUI(fluidPage(
titlePanel("DATASET CONVERSION"),
sidebarLayout(
fileInput("filein", label = h2("Select a file to convert.")),
submitButton("Submit")
),
mainPanel(
tableOutput("Dataset")
)
)
)
Server.R
source("MAIN.R")
shinyServer(function(input, output) {
outputdf <- reactive({ input$filein
})
output$Dataset <- renderTable({
outputdf()
})
})
Your Submit button is not currently linked to anything, so it will not do anything. If I am reading the code right, you are just taking the input dataset and storing it as the output of outputdf. Your output$Dataset then just picks up that outputdf and displays it as-is, without any work being done on it.
You use an action button like so:
## In UI.R
actionButton("execute", "Execute the Main Function")
## In Server.R
observeEvent(input$execute, {
## Do stuff here
})
Note that the actionButton has two parameters, inputID (which is how you refer to it) and text to display on top. For example, with input$filein, 'filein' is the inputID.
In Server.R, observeEvent won't do anything until it detects a change in input$execute, which happens when someone clicks the button. That is where you put your code to do stuff.
Now, in output$Dataset, you need to access the results of whatever you did in that observeEvent. One way to do that is to use a reactiveValue. This is just like a reactive, but instead of a function, it stores a data element. Initialize it as an empty dataframe, and then update it in the observeEvent. Something like this:
## In Server.R
treated_output <- reactiveValue(data.frame())
observeEvent(input$execute, {
## Run the function on the file
updated <- main_function(input$filein)
# Update your reactiveValue
treated_output(updated)
})
output$Dataset <- renderTable({
treated_output()
})
Does this make sense?

Use information about selectInput within a renderUI

I want to use some information of a selectInput in a renderUi, e.g. the length of selected items (renderUi depends on this number).
Unfortunately I haven't find a solution yet. Below a short simplified example, which should display the length of selected items. I guess there is a problem either with the return type of input or with the encoding of I/O.
Please note: I tried already renderPrint, which is working! But I need the information about the length of selectInput within a renderUi, since I want to create further ui elements based on the selected number.
Ui.R:
library(shiny)
shinyUI(fluidPage(
selectInput("first","first",choices=c("a","b"),multiple=TRUE),
uiOutput("hallo")
))
Server.R:
library(shiny)
shinyServer(function(input, output) {
output$hallo <- renderUI({
print(length(input$first))
})
})

Returning data frame value in reactive selectinput for Shiny

I'm trying to populate a dropdown for selectInput from a dataframe in a shiny application and can't seem to get it to work, here is a pared-down version:
datapr<-data.frame(type=c("Analog", "Digital", "Mixed Signal","Analog"),process=c("Bipolar","CMOS","BiCMOS","Bipolar"),funct=c("BJT","Mux","Mux","Regulator"))
If I have that dataframe to start, my shiny application calls and uses it like so:
ui.R
shinyUI({
selectInput("type",h4("Type:"),list("Analog","Digital","Mixed Signal"))
selectInput("process",h4("Process:"),"")
})
server.R
shinyServer(function(input,output,session){
observe({updateSelectInput(session,"process",choices=datapr$process[datapr$type==input$type])
})
What I'm getting out is a number instead of the actual dataframe's entry and cannot seem to use unname(), unique(), factor(), as.list() or anything straight-forward to pull out the entry as is. This used to work before the inception of SelectizeInput was added. Any help is greatly appreciated.
This worked for me:
ui.R
library(shiny)
shinyUI(fluidPage(
selectInput("type",h4("Type:"),list("Analog","Digital","Mixed Signal")),
selectInput("process",h4("Process:"),"")
))
server.R
library(shiny)
datapr<-data.frame(type=c("Analog", "Digital", "Mixed Signal","Analog"),
process=c("Bipolar","CMOS","BiCMOS","Bipolar"),
funct=c("BJT","Mux","Mux","Regulator"))
shinyServer(function(input,output,session){
observe({
updateSelectInput(session,"process",
choices=as.character(datapr$process[datapr$type==input$type]))
})
})

R Shiny Pass a vector as radioButtons choices argument

I would like to define a radio button in Shiny which normally I could do for example as:
radioButtons("choose_country", h3("Country"),
choices=c('uganda'='UG','tanzania'='TZ','kenya'='KE'))
Which gives the desired output:
However, I would like the choices part of radioButtons to be read from an
externally defined vector. i.e.
country_assg <- c('uganda'='UG','tanzania'='TZ','kenya'='KE')
I would then like to paste this vector as follows:
radioButtons("choose_country", h3("Country"),
choices=country_assg)
This does not seem to work as it seems not to break up the elements in the country_assg vector as required.
My question is how I can make country_assg to be evaluated as the choice argument inside the
radioButtons command so that I get the desired output.
I would appreciate ideas to tackle the issue.
For me, compiling the following three files does the job (all files in the same folder as always):
ui.R:
source('helpers.R')
shinyUI(fluidPage(
titlePanel("censusVis"),
radioButtons("choose_country", h3("Country"),
choices=c('uganda'='UG','tanzania'='TZ','kenya'='KE')),
radioButtons("choose_country", h3("Country"),
choices=country_assg)
)
)
server.R
library(shiny)
source("helpers.R")
shinyServer(
function(input, output) {
}
)
and helpers.R
country_assg <- c('uganda'='UG','tanzania'='TZ','kenya'='KE')
Hope this helps.
PS: Strictly speaking, you do not need to source the helpers.R in the server portion of this little app, because server.R does not require any of the definitions made in helpers.R.

Can I save the old value of a reactive object when it changes?

Note: After coming up with the answer I reworded the question to make if clearer.
Sometimes in a shiny app. I want to make use of a value selected by the user for a widget, as well as the previous value selected for that same widget. This could apply to reactive values derived from user input, where I want the old and the new value.
The problem is that if I try to save the value of a widget, then the variable containing that value has to be reactive or it will not update every time the widget changes. But, if I save the the value in a reactive context it will always give me the current value, not the previous one.
How can I save the previous value of a widget, but still have it update every time the user changes the widget?
Is there a way that does not require the use of an actionButton every time the user changes things? Avoiding an actionButton can be desirable with adding one is otherwise unnecessary and creates excess clicking for the user.
Seeing as the session flush event method seems to be broken for this purpose, here is an alternative way to do it using an observeEvent construct and a reactive variable.
library(shiny)
ui <- fluidPage(
h1("Memory"),
sidebarLayout(
sidebarPanel(
numericInput("val", "Next Value", 10)
),
mainPanel(
verbatimTextOutput("curval"),
verbatimTextOutput("lstval")
)
)
)
server <- function(input,output,session) {
rv <- reactiveValues(lstval=0,curval=0)
observeEvent(input$val, {rv$lstval <- rv$curval; rv$curval <- input$val})
curre <- reactive({req(input$val); input$val; rv$curval})
lstre <- reactive({req(input$val); input$val; rv$lstval})
output$curval <- renderPrint({sprintf("cur:%d",curre())})
output$lstval <- renderPrint({sprintf("lst:%d",lstre())})
}
options(shiny.reactlog = TRUE)
shinyApp(ui, server)
Yielding:
Update This answer was posted before the advent of the reactiveValues/observeEvent model in shiny. I think that #MikeWise 's answer is the better way to do this.
After some playing around this is what I came up with. The ui.r is nothing special
ui.r
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput(inputId="XX", label="Choose a letter",choices=letters[1:5])
),
mainPanel(
textOutput("Current"),
textOutput("old")
)
)
))
"Current" will display the current selection and "old" displays the previous selection.
In the server.r I made use of three key functions: reactiveValues, isolate and session$onFlush.
server.r
library(shiny)
server <- function(input, output,session) {
Values<-reactiveValues(old="Start")
session$onFlush(once=FALSE, function(){
isolate({ Values$old<-input$XX })
})
output$Current <- renderText({paste("Current:",input$XX)})
output$old <- renderText({ paste("Old:",Values$old) })
}
The server.r works like this.
First, Values$old is created using the reactiveValues function. I gave it the value "Start" to make it clear what was happening on load up.
Then I added a session$onFlush function. Note that I have session as an argument in my server function. This will run every time that shiny flushes the reactive system - such as when the selectizeInput is changed by the user. What is important is that it will run before input$XX gets a new value - so the value has changed at the selectizeInput but not at XX.
Inside the session$onFlush I then assign the outgoing value of XX to Values$old. This is done inside an isolate() as this will prevent any problems with input$XX gets updated with the new values. I can then use input$XX and Values$old in the renderText() functions.

Resources